Python Selenium Sometimes Clicks and Sometimes Doesn't

Viewed 115

I am using Python and Selenium to open different pages on PredictIt. So far I have this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
browser = webdriver.Chrome(options = chrome_options)
browser.get('https://www.predictit.org/account/')

#Input username into PredictIt
username = browser.find_element_by_id("username")
username.clear()
username.send_keys([MY USERNAME])

#Input password into PredictIt
password = browser.find_element_by_id("password")
password.clear()
password.send_keys([MY PASSWORD])

#Click login button
browser.find_element_by_xpath('//*[@id="app"]/div[3]/div/div/div/div[2]/div/form/div/div[6]/button[@type="submit"]').click()
browser.get_cookies()          #get_cookies() allows it to stay logged in
browser.find_element_by_xpath('//*[@id="nav-markets"]').click()

The last line of code routes it to the "Markets" page. The issue I have is that this sometimes works and sometimes doesn't. I would say about 60% of the time, it clicks through and brings me to the page. The other 40% of the time, it stays on the current page after login and produces the error:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a href="/markets" class="main-nav__link" id="nav-markets">...</a> is not clickable at point (270, 24). Other element would receive the click: <div class="modal__wrapper">...</div>
  (Session info: chrome=86.0.4240.198)

Would anyone have a solution to why this might be?

2 Answers

I would guess that's because there are no delay in your code so everything appends at the same time. You may need to add a bit of delay after submitting the form so that the page can load. As a rule of thumb, I always add a bit of delay before actions such as submitting a form or loading a page using time.sleep(), 0.5s should be enough.

Import the time module and try adding time.sleep(0.5) before the last line.

After you sign in I would induce a webdriver wait for the element to popup since the element might not be loaded in or currently interactable.

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

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='nav-markets']"))).click()
Related