"typeerror: 'str' Object Is Not Callable" Using Webdriverwait For Link_text In Selenium Through Python
Solution 1:
Your research was in the right direction.
In your first usecase, the line within try
was successful:
driver.find_element_by_link_text("Familien").click()
So the line within except
was never called:
WebDriverWait(driver, 12).until("Familien").click()
Hence you don't see the error.
In your second use-case, the line within try
wasn't successful:
driver.find_element_by_link_text("Familien").click()
So the line within except
was called:
WebDriverWait(driver, 12).until("Mehr erfahren").click()
which results into the error:
TypeError: 'str'object is not callable
Deep Dive
WebDriverWait is invoked inconjunction with expected_conditions.
You can find a couple of detailed discussions in:
Now, element_to_be_clickable()
should be called within a tuple
as it is not a function but a class, where the initializer expects just 1 argument beyond the implicitself:
classelement_to_be_clickable(object):
""" An Expectation for checking an element is visible and enabled such that you can click it."""def__init__(self, locator):
self.locator = locator
def__call__(self, driver):
element = visibility_of_element_located(self.locator)(driver)
if element and element.is_enabled():
return element
else:
returnFalse
So instead of:
WebDriverWait(driver, 12).until("Mehr erfahren").click()
You need to (add an extra parentheses):
WebDriverWait(driver, 12).until((EC.element_to_be_clickable(By.LINK_TEXT, "Mehr erfahren"))).click()
You can find a detailed discussions in init() takes 2 positional arguments but 3 were given using WebDriverWait and expected_conditions as element_to_be_clickable with Selenium Python
Solution 2:
I think I found your problem here. From the source
defuntil(self, method, message=''):
...
until
takes a method (i.e. something callable). So, when driver.find_element_by_link_text("Mehr erfahren").click()
raises an exception, it will attempt to call ...until("Mehr erfahren")...
. Since until
expects a method it will try to call the string you gave it - raising the error that's been driving you crazy :)
Solution 3:
I think until expects a conditional (and WebDriverWait waits until that condition is true). You're just passing a string as your argument instead of a condition. You need a condition that checks if that element exists on the page.
Solution 4:
You can use this css selector: a[href*="familien"]
This can be used for both your URLs.
driver.get('yourURL')
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href*="familien"]'))).click()
except NoSuchElementException:
# handle if the element not existprint('element not exist')
See documentation here to know how to use WebDriverWait
Post a Comment for ""typeerror: 'str' Object Is Not Callable" Using Webdriverwait For Link_text In Selenium Through Python"