Jay Dee
Jay Dee

Reputation: 1

Why does selenium produce target element is not interactable and could not be clicked error when there is an explicit wait

I am working with Selenium, I have looked on SO and have seen many posts but I am unable to implement them in a way which works. I am using the below code but the error message I am getting is:

Message: The target element is not interactable and could not be clicked

I thought it was because the commands there was no 'slack' between commands so I inserted an implicit wait, however I am still getting the same error. Any thoughts would be appreciated.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Safari()
driver.get("https://securities.stanford.edu/filings-case.html?id=107866")
button = driver.find_element_by_link_text('Log In')
button.click()
driver.implicitly_wait(10)
username = driver.find_element_by_id("login_email")
password = driver.find_element_by_id("login_pass")
username.send_keys("EMAIL")
password.send_keys("PASSWORD")
driver.implicitly_wait(10)
driver.find_element_by_link_text('Log In').click()

Upvotes: 0

Views: 311

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193068

The WebElement is a <button> element but not a <a> element. Hence you can't use link_text('Log In').

securities.stanford

You can use the following Locator Strategies

  • CSS_SELECTOR:

    driver.get("https://securities.stanford.edu/filings-case.html?id=107866")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hidden-tablet a[href='#myModalLogin']>strong"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login_email"))).send_keys("[email protected]")
    driver.find_element(By.CSS_SELECTOR, "input#login_pass").send_keys("[email protected]")
    driver.find_element(By.CSS_SELECTOR, "button[onclick='return submitLogin();']").click()
    
  • XPATH:

    driver.get("https://securities.stanford.edu/filings-case.html?id=107866")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'hidden-tablet')]//a[@href='#myModalLogin']/strong"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login_email']"))).send_keys("[email protected]")
    driver.find_element(By.XPATH, "//input[@id='login_pass']").send_keys("[email protected]")
    driver.find_element(By.XPATH, "//button[@onclick='return submitLogin();']").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
    
  • Browser Snapshot:

securities_stanford_edu

Upvotes: 1

Prophet
Prophet

Reputation: 33351

There are 7 elements on that page with text Log In, this is why the following command button = driver.find_element_by_link_text('Log In') will get the first element matching this text while this is not the eolement you are looking for. So, you need to use better locator to match the correct element.
Also, you are defining driver.implicitly_wait(10) AFTER that command.
Also, it's preferably to use explicit waits of expected conditions rather than implicitly waits.
Also, if you are using implicitly wait there is no need to define it several times. This parameter is set per driver session and in most cases no need to re-set it to another value during the session.
This should work better:

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

driver = webdriver.Safari()
wait = WebDriverWait(driver, 20)

driver.get("https://securities.stanford.edu/filings-case.html?id=107866")

wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@href="#myModalLogin"]//strong[text()='Log In']"))).click()

username = wait.until(EC.visibility_of_element_located((By.ID, "login_email")))
password = wait.until(EC.visibility_of_element_located((By.ID, "login_pass")))

username.send_keys("EMAIL")
password.send_keys("PASSWORD")

driver.find_element_by_link_text('Log In').click()

Upvotes: 1

Related Questions