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

时间:2017-07-18 06:27:24

标签: python selenium selenium-webdriver webdriver webdriverwait

我想在以下网站输入用户名和密码: 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

然后我尝试使用java脚本:

driver.execute_script("document.getElementsByClassName('sign-in-input')[0].click()")
cmd = "document.getElementsByClassName('label-focus')[0].value = 'abc@abc.com'"
driver.execute_script(cmd)

没有错误,但没有文字发送到"电子邮件地址"领域。

有人可以指导我输入电子邮件地址,密码的正确方法,然后点击"登录"。

感谢您提供帮助

3 个答案:

答案 0 :(得分:2)

此错误消息... selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见 ...表示当WebDriver实例试图找到它时,HTML DOM中不可见所需的元素。 ElementNotVisibleException 当DOM树上存在元素时抛出ElementNotVisibleException,但它不可见,因此无法与之交互。 原因 一个可能消除ElementNotVisibleException的事实是WebElement存在于HTML中,并且在尝试单击()或读取从视图中隐藏的元素的属性时通常会遇到此异常。 解 由于ElementNotVisibleException确保WebElement存在于HTML中,因此前面的解决方案将按照以下详细步骤进行两次折叠: 如果下一步是读取所需元素的任何属性,则需要将WebDriverWait与设置为visibility_of_element_located的expected_conditions子句结合使用,如下所示: 来自selenium.webdriver.support.ui导入WebDriverWait 来自selenium.webdriver.common.by导入 从selenium.webdriver.support导入expected_conditions作为EC my_value = WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH," element_xpath")))。get_attribute(" innerHTML") 如果您下一步是在所需元素上调用click(),那么您需要将WebDriverWait与设置为element_to_be_clickable的expected_conditions子句结合使用,如下所示: 来自selenium.webdriver.support.ui导入WebDriverWait 来自selenium.webdriver.common.by导入 从selenium.webdriver.support导入expected_conditions作为EC WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH," element_xpath")))。click() 这个用例 您构造为// h1 [@class =" sign-in-input"]的xpath与任何节点都不匹配。我们需要创建唯一的xpath来定位代表电子邮件地址,密码和登录按钮的元素,从而导致WebDriverWait。下面的代码块将帮助您实现相同的目标: 来自selenium import webdriver 来自selenium.webdriver.chrome.options导入选项 来自selenium.webdriver.support.ui导入WebDriverWait 来自selenium.webdriver.common.by导入 从selenium.webdriver.support导入expected_conditions作为EC options =选项() options.add_argument("起动最大化&#34) options.add_argument("禁用-信息栏&#34) options.add_argument(" - 禁用的扩展&#34) 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'] //输入[@name =' email& #39;]")))send_keys(" abc@abc.com") driver.find_element_by_xpath(" // DIV [@id ='模态'] //输入[@name ='密码']&#34)。send_keys(&# 34;密码&#34) driver.find_element_by_xpath(" // div [@id =' modal'] //按钮[@class =' color-site sign-in-button']&#34 ;)点击()。

答案 1 :(得分:0)

用于用户名:

driver.find_element_by_xpath("//input(@type='email')").click()
driver.find_element_by_xpath("//input(@type='email')").send_keys( "username" )
密码使用

driver.find_element_by_xpath("//input(@type='password')").click()
driver.find_element_by_xpath("//input(@type='password')").send_keys( "password" )

答案 2 :(得分:0)

有两个问题:

  • 第一个是计时,表单显示需要一些时间。您可以使用显式等待来解决它。
  • 第二个是<input>标记中的字段ID,而不是<h1>标记,并且有许多字段与此xpath匹配。我建议您找到包含字段的表单并使用它来查找每个字段

对于计时问题,您可以使用显式等待

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.Chrome()
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
form = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="modal-body"]//form')))

form.find_element_by_name('email').send_keys(email)
form.find_element_by_name('password').send_keys(password)
form.find_element_by_name('sign-in-button').click()
相关问题