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

2024-10-06 10:34:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

请有人告诉我输入电子邮件地址、密码的正确方法,然后单击“登录”


Tags: inhttpscmdcom密码inputexecutewww
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

…意味着所需的元素在HTML DOM中不可见,而WebDriver实例正在尝试查找它


ElementNotVisibleException

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


理由

元素NotVisibleException的一个可能的优点是WebElement在HTML中是存在的,当试图click()read隐藏的元素属性时,通常会遇到此异常


解决方案

AsElementNotVisibleException确保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(),则需要结合expected_conditions子句将WebDriverWait归纳为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的元素。下面的代码块将帮助您实现相同的目标:

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

有两个问题:

  • 第一个是计时,表单出现需要一些时间。您可以使用显式等待来解决它
  • 第二个是<input>标记中的字段id,而不是<h1>标记中的字段id,并且有许多字段与此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()

相关问题 更多 >