Message: element not interactable on accessing a tag python

女生的网名这么多〃 提交于 2021-02-17 04:57:29

问题


I am trying to access the sign in button on the url as shown in the code below. I have verified the content of the url as well as the href. They are both consistent with what appears using inspect element dev tool.

But on clicking the extracted element I get the error:

Message: element not interactable 

I have no idea why is this occurring.

Kindly help me solve this issue

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait

# setup the browser
browser = webdriver.Chrome('./chromedriver')
browser.get('https://libraries.usc.edu/')
browser.maximize_window()

# access the relevant a tag after inspecting it in dev tool inspect element
a_tag_elt = WebDriverWait(browser, 10).until(lambda browser :
                                             browser.find_element_by_css_selector('div.site-header__signin a'))

# sanity check by printing out the details
print(type(a_tag_elt))
print(a_tag_elt.get_attribute('href'), a_tag_elt.get_attribute('innerHTML'))

# produces Message: element not interactable error
a_tag_elt.click()

# quit the browser
browser.quit()

回答1:


To click on the element with text as Sign In you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.main-navigation__navbar span"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='main-navigation__navbar ']//span"))).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
    



回答2:


To click on Sign In link Induce WebDriverWait() and element_to_be_clickable() and following css selector.

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.main-navigation__navbar>.main-navigation__navbar-text"))).click()

Or following xpath.

WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Sign In']"))).click()

You need to import below libraries.

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


来源:https://stackoverflow.com/questions/63792931/message-element-not-interactable-on-accessing-a-tag-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!