Skip to main content
 首页 » 编程设计

python之selenium.common.exceptions.ElementNotVisibleException : Message: element not visible while trying to access an element with Python + Selenium

2025年02月15日6yxwkf

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

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

谁能指导我如何正确输入电子邮件地址、密码,然后单击“登录”。

请您参考如下方法:

这个错误信息...

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

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


元素不可见异常

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


原因

ElementNotVisibleException 的一个积极影响是 WebElement 在 HTML 中存在 并且在尝试click() 或 read 从 View 中隐藏的元素的属性。


解决方案

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

  • 如果下一步是读取所需元素的任何属性,那么您需要归纳WebDriverWait结合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(),那么您需要引入 WebDriverWait结合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() 
    

这个用例

您构造为 //h1[@class="sign-in-input"] 的 xpath 不匹配任何节点。我们需要创建唯一的 xpath 来定位代表 Email Address、Password 和 Sign 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()