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() 
2
  • What "element in front of the log in button" should mean? Another element is overlapping target button? Do you get an exception? Commented Aug 6, 2018 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? Commented Aug 6, 2018 at 12:19

3 Answers 3

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())

Sign up to request clarification or add additional context in comments.

3 Comments

Nice to learn a new thing until_not. Is this a newly released approach @sir Andersson?.
No. It's an old approach. I'm quite sure that I've already used it 2-3 years ago...
the only solution so far
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); 

1 Comment

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?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.