selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用 Python + Selenium 访问元素时元素不可见

2024-02-21

我尝试在以下网站中输入用户名和密码:https://www.thegreatcoursesplus.com/sign-in https://www.thegreatcoursesplus.com/sign-in

driver = webdriver.Chrome()
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
driver.find_element_by_xpath('//h1[@class="sign-in-input"]').click()

这给出了以下异常:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

然后我尝试使用javascript:

driver.execute_script("document.getElementsByClassName('sign-in-input')[0].click()")
cmd = "document.getElementsByClassName('label-focus')[0].value = '[email protected] /cdn-cgi/l/email-protection'"
driver.execute_script(cmd)

没有错误,但没有文本发送到“电子邮件地址”字段。

有人可以指导我输入电子邮件地址、密码然后单击“登录”的正确方法吗?


这个错误信息...

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

...意味着想要的元素不可见HTML DOM https://www.w3schools.com/js/js_htmldom.asp网络驱动程序实例试图找到它。


元素不可见异常

元素不可见异常 https://seleniumhq.github.io/selenium/docs/api/py/common/selenium.common.exceptions.html#selenium.common.exceptions.ElementNotVisibleException当元素存在于DOM Tree https://javascript.info/dom-nodes,但它不可见,因此无法与之交互。


Reason

一项积极的收获元素不可见异常事实是网页元素 is present在 HTML 中,尝试时通常会遇到此异常click() or read从视图中隐藏的元素的属性。


Solution

As 元素不可见异常确保网页元素 is present在 HTML 中,因此前面的解决方案将按照接下来的步骤进行两次折叠,如下详述:

  • 如果您下一步是read所需元素的任何属性,那么您需要归纳WebDriver等待 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait和这个结合预期条件 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions子句设置为元素位置可见性 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.visibility_of_element_located如下:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    my_value = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "element_xpath"))).get_attribute("innerHTML")
    
  • 如果您下一步是调用click()在所需的元素上,那么你需要诱导WebDriver等待 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait和这个结合预期条件 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions子句设置为元素可点击 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.element_to_be_clickable如下:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click()
    

这个用例

The xpath你构建为//h1[@class="sign-in-input"]不匹配任何节点。我们需要创造独特的xpath定位代表的元素Email Address, Password and Sign In按钮诱导WebDriver等待。下面的代码块将帮助您实现相同的目标:

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

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='modal']//input[@name='email']"))).send_keys("[email protected] /cdn-cgi/l/email-protection")
driver.find_element_by_xpath("//div[@id='modal']//input[@name='password']").send_keys("password")
driver.find_element_by_xpath("//div[@id='modal']//button[@class='color-site sign-in-button']").click()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用 Python + Selenium 访问元素时元素不可见 的相关文章

随机推荐