Selenium w/Python3 class_status = like.get_attribute("class") AttributeError: 'str' object has no attribute 'get_attribute' facing this issue - python-3.x

Selenium w/Python3 class_status = like.get_attribute("class") AttributeError: 'str' object has no attribute 'get_attribute' facing this issue
I am facing this issue when I call the first function is called but the number 2 function is not working
class Like():
def __init__(self,video_xpath,like_refiq):
self.video_xpath = video_xpath
self.like_refiq=like_refiq
# self.dislike=dislike
def like_dislike(self):
like =self.video_xpath
time.sleep(5)
# count = like.text
class_status = like.get_attribute("class")
if class_status == "selected":
like.click()
time.sleep(3)
# alredy_like_cls_status = like.get_attribute("class")
alredy_count = like.text
return print(f"creation un{alredy_count} {self.like_refiq} count")
elif class_status == "":
like.click()
time.sleep(3)
# non_like_cls_status = like.get_attribute("class")
non_count = like.text
return print(f"creation {non_count} {self.like_refiq} count")
else:
assert False
class Refiq(Like):
pass
like_path=driver.find_element_by_xpath("//w-creation-detail-popup//li[1]//a[1]")
Like_dislike=Like(video_xpath=like_path,like_refiq="like")
Like_dislike.like_dislike()
# like dislike function call
refq_unfrefiq=Refiq(video_xpath="//w-creation-detail-popup//li[2]//a[1]",like_refiq="refiq")
refq_unfrefiq.like_dislike()

You have assigned the like variable a string by like =self.video_xpath.
So, it's correct, click is a sting and you can't apply .get_attribute("class") on string object.
click is not a webelement object.

Related

Getting TypeError: 'str' object is not callable in below code. I am not calling any inbuilt str

Below is my Appium code to invoke the Settings
def invoke android(self)
caps = {
"platformName": "Android",
"platformVersion": "11.0",
"deviceName": "Pixel 5 Emulator",
"appPackage": "com.android.settings",
"appActivity": "com.android.settings.Settings"
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
time.sleep(3)
links = driver.find_elements(AppiumBy.CLASS_NAME('android.widget.TextView'))
for link in links:
print(link.text)
if link.text == "Network & internet":
link.click()
time.sleep(4)
driver.quit()
**Getting error in below code:**
links = driver.find_elements(AppiumBy.CLASS_NAME('android.widget.TextView'))
E TypeError: 'str' object is not callable
From the docs found here, your usage of CLASS_NAME is wrong. CLASS_NAME is not a function. The usage you are after looks more like the following:
links = driver.find_elements(AppiumBy.CLASS_NAME, 'android.widget.TextView')

AttributeError: 'NoneType' object has no attribute 'data' in displaying linked list

I'm trying to display linked list elements in the form of a list but keep getting this error:
AttributeError: 'NoneType' object has no attribute 'data'
class Node:
def __init__(self,data=None,next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self,data):
node = Node(data,self.head)
self.head = node
def display(self):
elements = []
currNode = self.head
while currNode:
currNode = currNode.next
elements.append(currNode.data)
print(elements)
if __name__ == "__main__":
ll = LinkedList()
ll.insert_at_beginning(1)
ll.insert_at_beginning(2)
ll.insert_at_beginning(3)
ll.display()
Can anyone explain the error here?
After the while loop, append data first then go to the next. You're getting the error because if currNode.next is null then it's showing the object has no attribute 'data'. So, append first then go to next. If currNode.next is null then the loop will stop.
while currNode:
elements.append(currNode.data)
currNode = currNode.next

I get 'AttributeError: 'TestClass' object has no attribute 'get'' during the test case execution

I'm trying to create a test case using pytest.
I use pytest fixture for my test_case. But every time I execute the code i get an error:
def test_case(webdriver_definition):
driver = webdriver_definition
baseURL = 'https://www.di.fm/'
> driver.get(baseURL)
E AttributeError: 'TestClass' object has no attribute 'get'
My test class
class TestClass:
#pytest.fixture(autouse=True)
def webdriver_definition(self):
driver = webdriver.Chrome(executable_path="D:/Selenium webdriver/chromedriver_win32/chromedriver.exe")
driver.implicitly_wait(2)
driver.maximize_window()
print('=================Driver initialized===================')
return driver
def test_case(webdriver_definition):
driver = webdriver_definition
baseURL = 'https://www.di.fm/'
driver.get(baseURL)
home_button = sd.find_element("//nav[#id='side-nav']/ul//span[.='Home']")
if home_button:
print(f'Element {home_button} was found\n')
else:
print(f'Element {home_button} was not found')
home_button.click()
time.sleep(2)
driver.quit()
This is because Python expect self as first parameter in test_case(), so webdriver_definition was treated as self, which is TestClass
class TestClass:
#pytest.fixture(autouse=True)
def webdriver_definition(self):
driver = webdriver.Chrome(executable_path="D:/Selenium webdriver/chromedriver_win32/chromedriver.exe")
# ...
return driver
def test_case(self, webdriver_definition):
driver = webdriver_definition
baseURL = 'https://www.di.fm/'
driver.get(baseURL)

Python will not open a module within my .py file

I was working on some code which retrieves a line in a text file ("save[#]) with the format:
"[name],[boolean or integer value]"
(The aim is to be able to retrieve it for save states of a game)
The issue is that whenever I try to return a value from my module I get the following:
Traceback (most recent call last):
File "//IHS-FS-001.ihs.local/Cohort2020$/2ELGAG1/python/srctg/test.py", line 5, in <module>
retrieve()
File "//IHS-FS-001.ihs.local/Cohort2020$/2ELGAG1/python/srctg/test.py", line 3, in retrieve
if retrieve.check("test", 1) == True:
AttributeError: 'function' object has no attribute 'check'
The test attribute is a testing module set up to test the code for the game:
import retrieve.py
def retrieve():
if retrieve.check("test", 1) == True:
return True
retrieve()
The retrieve attribute itself is set up like so:
import error
def check(i_name, save):
save = str(save)
save_n = "save" + save + ".txt"
save_f = open(save_n, "r")
list = save_f.readlines()
for item in range(len(list)):
list[item] = list[item].strip()
list[item] = list[item].split(",")
list[item][1] = list[item][1]
for item in range(len(list)):
if i_name == list[item][0]:
i_return = list[item][1]
if bool_check(i_return) == True:
i_return = bool(i_return)
elif int_check(i_return) == True:
i_return = int(i_return)
else:
print(error.code("001"))
return "error"
return i_return
def int_check(value):
while True:
try:
value = int(value)
return True
break
except ValueError:
return False
break
def bool_check(value):
while True:
try:
value = bool(value)
return True
break
except ValueError:
return False
break
Don't include the .py in the import. This tries to import a module named py inside the package named retrieve, which is probably not what you meant.
import retrieve as retrieve_module
def retrieve():
if retrieve_module.check("test", 1) == True:
return True
Also, don't write a function with the same name as the module you just imported. Change the name of one or the other. That's why it can't find the .check attribute. It's looking inside the retrieve function instead of inside the retrieve module because you overwrote it in the global namespace (by executing the function definition) before you called the function.

How to handle object null in python

I am using flask. When I do not pass StartingSequenceNumber to flask app then How can I handle null object.
class Meta():
def __init__(self, j):
self.__dict__ = json.loads(j)
in bootstrap.py
meta = Meta(request.get_data().decode())
if meta.StartingSequenceNumber is not None:
# do something
Error : AttributeError: 'Meta' object has no attribute 'StartingSequenceNumber'
You could use the hasattr() built-in function (https://docs.python.org/3/library/functions.html#hasattr) which will return True if the object has the attribute :
if hasattr(object, 'attribute'):
# do smthg
else:
# do smthg else
But it would be better to used try & except blocks and throw an AttributeError
try:
doSmthg()
except AttributeError:
# do smthg else

Resources