消息:元素在访问标签python时不可交互 [英] Message: element not interactable on accessing a tag python

查看:43
本文介绍了消息:元素在访问标签python时不可交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问url上的登录按钮,如下面的代码所示.我已经验证了网址以及href的内容.它们都与使用检查元素开发工具显示的内容一致.

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.

请帮助我解决这个问题

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

推荐答案

要单击文本为登录的元素,您需要诱导定位器策略:

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:

  • 使用 CSS_SELECTOR :

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

  • 使用 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
    

  • 这篇关于消息:元素在访问标签python时不可交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆