【问题标题】:selenium.common.exceptions.ElementNotVisibleException: Message: element not visible while trying to access an element with Python + Seleniumselenium.common.exceptions.ElementNotVisibleException:消息:尝试使用 Python + Selenium 访问元素时元素不可见
【发布时间】:2017-12-22 20:48:05
【问题描述】:

我正在尝试在以下网站中输入用户名和密码: 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)

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

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

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    此错误消息...

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

    ...暗示所需的元素在HTML DOM 中不可见,而WebDriver 实例正试图找到它。


    ElementNotVisibleException

    DOM Tree 上存在元素时抛出ElementNotVisibleException,但该元素不可见,因此无法与之交互。


    原因

    ElementNotVisibleException 的一个积极结论是 WebElement 在 HTML 中存在,并且在尝试click()read 隐藏在视图之外的元素的属性。


    解决方案

    由于 ElementNotVisibleException 确保 WebElement 在 HTML 中 存在,因此根据后续步骤,前面的解决方案将分为两部分下面:

    • 如果下一步是读取所需元素的任何属性,则需要将WebDriverWaitexpected_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(),那么您需要将WebDriverWaitexpected_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()
      

    这个用例

    您构造为//h1[@class="sign-in-input"]xpath 不匹配任何节点。我们需要创建唯一的xpath 来定位代表Email AddressPasswordSign In 按钮的元素诱导WebDriverWait。下面的代码块将帮助您实现相同的目标:

    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("abc@abc.com")
    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()
    

    【讨论】:

    • 你说,As ElementNotVisibleException ensures that the WebElement is present within the HTMLElementNotVisibleException 不能确保元素存在,它会在元素不存在时被抛出。
    • @JeffC 这些是 Selenium 的基本原理和基本原理,我们已经在许多线程中讨论了所有这些基本问题。如果您仍有疑问,请根据您的要求提出一个新问题。 StackOverflow 贡献者将很乐意为您提供帮助。
    • @DebanjanB 每次我提出可证明是错误的事情时,您都会说同样的话。为什么?你假装我不知道我在说什么并不能证明你是对的。我陈述了一个事实……你能证明别的吗?我的说法不正确怎么办?
    • @JeffC 这不仅关乎你...不知道... 而且1)你失去提问的权利反映了你的错误行为2)6,553 否决票表明您仍然不信任社区 3) 您个人资料中的 SOreadytohelp 只是洗眼。
    • @DebanjanB 1. 为什么你一直声称我失去了提问的权利?我没有失去任何特权......我只是没有任何问题。这很讽刺,因为你刚刚结束为期一周的禁令。 2. 我投反对票是因为答案没有用(根据工具提示),包括传播错误信息,比如这个答案。 3. 我经常在这里(可能每天一个小时或更长时间),所以我看到了很多问题和答案,我已经回答并评论了数千个问题和答案(请参阅我的个人资料统计信息)。我认为您对 SOreadytohelp 的评论不言自明。
    【解决方案2】:

    有两个问题:

    • 第一个是计时,表单出现需要一些时间。您可以使用显式等待来解决它。
    • 第二个是<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()
    

    【讨论】:

    • 太棒了。有效。除了我需要更改最后一行: form.find_element_by_xpath("//div[@id='modal']//button[@class='color-site sign-in-button']").click()
    【解决方案3】:

    用于用户名:

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

    【讨论】:

    • 仍然看到同样的错误:selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2018-12-26
    • 2019-04-27
    • 2018-03-25
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多