0

I'm trying to click a log in button with selenium (chrome driver, --headless). The problem is that there is a element in front of the log in button. Is there a way to click the button even if an element is in the way?

My Python script looks something like this:

pirkciau_driver=webdriver.Chrome(crm_path, chrome_options=options)
pirkciau_driver.get(login_url)
elem = pirkciau_driver.find_element_by_id("email")
elem.clear()
elem.send_keys(x['pirkciau']['username'])
elem = pirkciau_driver.find_element_by_id("passwd")
elem.clear()
elem.send_keys(x['pirkciau']['password'])
elem = pirkciau_driver.find_element_by_id("SubmitLogin")
elem.click()
JeffC
  • 22,180
  • 5
  • 32
  • 55
Duma
  • 124
  • 2
  • 12
  • What *"element in front of the log in button"* should mean? Another element is overlapping target button? Do you get an exception? – Andersson Aug 06 '18 at 12:14
  • 1
    Selenium is intended to emulate user behaviour. How would a user click an element that is obscured by another? Why do you need to click that element, and how does a real user handle this case when doing it manually? – Metareven Aug 06 '18 at 12:19

3 Answers3

3

You can use

pirkciau_driver.execute_script('arguments[0].click()', elem)

to click button that's currently overlapped by another element, but it won't simulate the real user-like behavior... If you're testing something, it's better to wait until element that receives click instead of LogIn button becomes invisible with ExplicitWait and until_not(EC.visibility_of_element_located()) or until(EC.invisibility_of_element_located())

Andersson
  • 51,635
  • 17
  • 77
  • 129
2

Try using JavaScript Click as below :

pirkciau_driver=webdriver.Chrome(crm_path, chrome_options=options)
pirkciau_driver.get(login_url)
elem = pirkciau_driver.find_element_by_id("email")
elem.clear()
elem.send_keys(x['pirkciau']['username'])
elem = pirkciau_driver.find_element_by_id("passwd")
elem.clear()
elem.send_keys(x['pirkciau']['password'])
elem = pirkciau_driver.find_element_by_id("SubmitLogin")
driver.execute_script("arguments[0].click();", elem);
HemSa
  • 176
  • 6
  • 2
    Be aware that by using JS to click an element obscured by another element, you aren't testing a real user scenario. No user can click an element under another element. Best practice is to close or otherwise remove the blocking element before clicking the desired element. If something is in the way, how would a user dismiss the covering element? – JeffC Aug 06 '18 at 13:18
1

Few ways to do that:

  • Execute JavaScript:
    Running javascript in Selenium using Python
  • Maybe you can login using keyboard(sending enter key)? elem.send_keys(Keys.RETURN)
  • Try clicking on other element so modal is removed and can click on the button you need.
Adelina
  • 10,915
  • 1
  • 38
  • 46