I have written a Python script which logs into my E-Mail account and sends some messages automatically.
After having tested the code which worked, I wanted to simplify it (add one-liners, reduce number of local variables...). After the changes it was not working as I have expected. The full error message was:
<bound method WebElement.click of
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="fa50a977-d210-
4c7f-a836-080014cb9209", element="2548584a-e638-47fd-b40b-615516d0e9c6")>>
I am just going to post the beginning of the my script, to the point where the first error occured. When I understand how to avoid the first error, I can fix the rest of the code.
This code snippet of the beginning of my code worked:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.get('https://protonmail.com/')
loginButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.btn-ghost:nth-child(1)")))
loginButton.click()
I changed it to the following code which does not work:
browser = webdriver.Firefox()
browser.get('https://protonmail.com/')
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.btn-ghost:nth-child(1)"))).click()
Obviously, I violated some basic Python programming rule here. Can anyone explain to me, what is wrong with the second code snippet and why this type of error occurs?
CodePudding user response:
As mentioned by pcalkins in a comment this is a typo here.
You are using .click method instead of .click() method.
So instead of
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.btn-ghost:nth-child(1)"))).click
It should be
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.btn-ghost:nth-child(1)"))).click()
I would like to advice you to use visibility_of_element_located expected conditions each time this would be relevant rather than presence_of_element_located. Since presence_of_element_located may return you an element while it is still not completely loaded, only existing on the page while visibility_of_element_located will wait for the element visibility that is more mature element state when it is already visible, clickable etc.
