1

I am working on a bot capable of login into a webpage (postmark.com). To do that, I am using selenium and python. As it is right now my code is capable of accessing the webpage, clicking on the login tap, inserting the user name and password; however, when it comes to clicking on the Login tap (to access the account) I get the following error

Traceback (most recent call last):
  File "/home/pi/Documents/Bot_Poshmark.py", line 20, in <module>
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

The bizarre think about this, is that sometimes (like 2 or 3) the same piece of code that I wrote, can complete all the steps. Here is my code (I am using a Raspberry Pi 4 for this)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.poshmark.com") #Open webpage
Log_Field=(By.XPATH, "//a[contains(text(),'Log in')]")
Email= (By.XPATH, "//input[@placeholder='Username or Email']")
Pass= (By.XPATH, "//input[@placeholder='Password']")
Log= (By.XPATH, "//button[@class='btn btn--primary']")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log_Field)).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Email)).send_keys("xxx@xx.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Pass)).send_keys("123456")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()

Do anyone have an idea on why this is happening? Thank you

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Perro
  • 348
  • 5
  • 14

1 Answers1

0

To send a character sequence to the Username or Email and Password field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://poshmark.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login_form_username_email"))).send_keys("xxx@xx.com")
    driver.find_element_by_css_selector("input#login_form_password").send_keys("123456")
    driver.find_element_by_css_selector("button.btn.blue.btn-primary").click()
    
  • Using XPATH:

    driver.get("https://poshmark.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Log in']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login_form_username_email']"))).send_keys("xxx@xx.com")
    driver.find_element_by_xpath("//input[@id='login_form_password']").send_keys("123456")
    driver.find_element_by_xpath("//button[@class='btn blue btn-primary']").click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

This usecase

In this usecase, the line:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()

was unable to identify the desired element within the desired time frame hence you faced TimeoutException.

However, from TimeoutException it will be tough to dig out the actual result of the failure.


Solution

As a solution to know about the exact cause of the failure, you need to remove the WebDriverWait and replace the line of code with either:

  • find_element_by_class_name(name)
  • find_element_by_css_selector(css_selector)
  • find_element_by_id(id)
  • find_element_by_link_text(link_text)
  • find_element_by_name(name)
  • find_element_by_partial_link_text(partial_link_text)
  • find_element_by_tag_name(tag_name)
  • find_element_by_xpath(xpath)

If required you can slow down the search inducing waits through time.sleep(secs) while debugging.


References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your response. The issue was solved using your advise. However I have a question regarding the xpath of the last object "//button[@class='btn blue btn-primary']" you added a different xpath. I used the inspector to double check this and the xpath that I get is "//button[@class='btn btn--primary']". Would mind telling me ho you arrived to yours? – Perro Aug 04 '20 at 21:18
  • @Perro My bad, I was able to narrow down to this issue much after I published my answer. I missed updating the answer. Your _xpath_ was a bit off. As you have used _xpath_, `btn btn--primary` wasn't the exact value but it was **`btn blue btn-primary`** – undetected Selenium Aug 04 '20 at 21:21
  • On another note, I'm aware there are some differences observed in the HTML as depicted by `Right Click` -> `Inspect` vs `ctrl`+`shift`+`I`. I would suggest the later approach. See the **Alternate Strategies** section in [Why XPath does not highlighted the yellow mark in Chrome84?](https://stackoverflow.com/questions/62945647/why-xpath-does-not-highlighted-the-yellow-mark-in-chrome84/63073849#63073849) – undetected Selenium Aug 04 '20 at 21:26
  • 1
    Once again thank you so much for your response and for the references. – Perro Aug 04 '20 at 21:28
  • I am having a new issue, with another portion of my code. Now I am getting the following error `TypeError: _init_() takes 2 positional arguments but 3 were given` In order to understand the error, I read the two references suggested by you; however, I am not sure what is the nature of this error. If you don't mind would you mind telling me your thoughts and comments about it. Thank you – Perro Aug 12 '20 at 04:32