0

My Python Selenium can't click target element, instead seem click element behind target element? I try to click, or enter text in 'drop-down meun', but I find that I am result in clicking element behind this 'drop-down. I know this is element behind it, because there is advistering area behind, and the result top-up showing the same advistering material. Here is my code:

# info for login
my_email = 'my_email'
my_passcode = 'my_passcode'
email_url = r'https://www.gmx.com/#.1559516-header-navlogin2-1'

# start driver and open url
driver = webdriver.Chrome(chrome_path)
driver.get(email_url)

# input email account
xpath = r'//*[@id="login-email"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_email).perform()

# input passcode and hit 'enter' to login
xpath = r'//input[@id="login-password"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_passcode).send_keys(Keys.ENTER).perform()

It happens to me for some other site when the site appear to have 'two layers' (not sure if I am using the right word). I can process anything on top layer, and only result in activate anything behind it. Many thank when provide solution!!

4
  • does it happen when you try to click the frist or the second element? Commented Aug 28, 2019 at 9:49
  • also try to change xpath = r'//*[@id="login-email"]' to xpath = r'//input[@id="login-email"]' Commented Aug 28, 2019 at 10:00
  • What do you meann by first and second element? I have tried both xpath, meet same issue. Commented Aug 28, 2019 at 10:49
  • try to use something like this to click something: def klk(elem, driver): action = webdriver.common.action_chains.ActionChains(driver) driver.execute_script("arguments[0].scrollIntoView();", elem) action.move_to_element(elem).click().perform() log.debug('element clicked') Commented Aug 28, 2019 at 11:21

1 Answer 1

1
  1. You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
  2. You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
  3. I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:

    xpath = r'//button[contains(@class,"login-submit")]'
    

    and then just call click() function on the resulting variable

Example suggested code:

  1. You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
  2. You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
  3. I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:

    xpath = r'//button[contains(@class,"login-submit")]'
    

    and then just call click() function on the resulting variable

Example suggested code:

# input email account
xpath = r'//*[@id="login-email"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_email)

# input passcode and hit 'enter' to login
xpath = r'//input[@id="login-password"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_passcode)

xpath = r'//button[contains(@class,"login-submit")]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.click()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank, it is working now. Other than just solution, Could you explain a bit how it work behind the code and logic? I have other code do this way like here does: 1.wait for xpath > 2. redefine xpath's element (I know not necessary now) > 3. ActionChains that do click() or send_keys. Most site still work on this way, but few site like this example doesn't work. What extra action I have added in my old way that me make result in not reaching the webelements (webelements of user and pass blank area). Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.