selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用Python + Selenium访问元素时元素不可见 [英] selenium.common.exceptions.ElementNotVisibleException: Message: element not visible while trying to access an element with Python + Selenium

查看:268
本文介绍了selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用Python + Selenium访问元素时元素不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to enter username and password in the following website: 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()

这给出了以下例外情况:

This gave following exception:

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

然后我尝试使用Java脚本:

Then I tried to use java script:

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

没有错误,但没有文本发送到电子邮件地址"字段.

There are no errors but no text is sent to "Email Address" field.

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

Can someone please guide me on the correct way to enter email address, password and then click "Sign-in".

感谢您的帮助

推荐答案

此错误消息...

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

...表示元素不可见 > HTML DOM ,而 WebDriver 实例正试图找到它.

...implies that the desired element was not visible within the HTML DOM while the WebDriver instance was trying to find it.

DOM树中存在某个元素时,会引发> ElementNotVisibleException ,但不是可见,因此无法与之交互.

ElementNotVisibleException is thrown when an element is present on the DOM Tree, but it is not visible, and so is not able to be interacted with.

ElementNotVisibleException 的一个可能原因是 WebElement 在HTML中存在,并且在尝试click()read从视图中隐藏的元素的属性.

One possitive take away from ElementNotVisibleException is the fact that the WebElement is present within the HTML and this exception is commonly encountered when trying to click() or read an attribute of an element that is hidden from view.

由于 ElementNotVisibleException 确保 WebElement 在HTML中存在,因此按照下面的详细步骤,前面的解决方案将是两方面的下方:

As ElementNotVisibleException ensures that the WebElement is present within the HTML so the solution ahead would be two folds as per the next steps as detailed below:

  • 如果下一步是阅读所需元素的任何属性,则需要引入

  • If you next step is to read any attribute of the desired element, then you need to induce WebDriverWait in-conjunction with expected_conditions clause set to visibility_of_element_located as follows:

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() ,则需要引入 element_to_be_clickable 如下:

  • If you next step is to invoke click() on the desired element, then you need to induce WebDriverWait in-conjunction with expected_conditions clause set to element_to_be_clickable as follows:

    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 .下面的代码块将帮助您实现相同的目标:

    The xpath you constructed as //h1[@class="sign-in-input"] doesn't match any node. We need to create unique xpath to locate the elements representing Email Address, Password and Sign In button inducing WebDriverWait. The below code block will help you to achieve the same:

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

    这篇关于selenium.common.exceptions.ElementNotVisibleException:消息:尝试使用Python + Selenium访问元素时元素不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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