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

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

问题描述

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

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

这给出了以下异常:

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 DOMWebDriver 实例试图找到它.

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

ElementNotVisibleExceptionDOM 树 上存在元素时抛出,但不是可见,因此无法与之交互.

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.

As 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(),那么您需要引入 WebDriverWait 结合使用 expected_conditions 子句设置为 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天全站免登陆