我正在尝试在以下网站中输入用户名和密码: 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)
没有错误,但没有文本发送到“电子邮件地址”字段
请有人告诉我输入电子邮件地址、密码的正确方法,然后单击“登录”
用户名使用:
密码使用:
此错误消息
…意味着所需的元素在HTML DOM中不可见,而WebDriver实例正在尝试查找它
ElementNotVisibleException
ElementNotVisibleException在DOM Tree上存在元素时抛出,但该元素不可见,因此无法与之交互
理由
元素NotVisibleException的一个可能的优点是WebElement在HTML中是存在的,当试图
click()
或read
隐藏的元素属性时,通常会遇到此异常解决方案
AsElementNotVisibleException确保WebElement在HTML中存在,因此前面的解决方案将是两倍,如下所述:
如果下一步是读取所需元素的任何属性,则需要将WebDriverWait与expected_conditions子句一起归纳为visibility_of_element_located,如下所示:
如果下一步是在所需元素上调用
click()
,则需要结合expected_conditions子句将WebDriverWait归纳为element_to_be_clickable,如下所示:这个用例
构造为
//h1[@class="sign-in-input"]
的xpath与任何节点都不匹配。我们需要创建唯一的xpath来定位表示Email Address
、Password
和Sign In
的元素。下面的代码块将帮助您实现相同的目标:有两个问题:
<input>
标记中的字段id,而不是<h1>
标记中的字段id,并且有许多字段与此xpath匹配。我建议您定位包含字段的表单,并使用它来定位每个字段对于计时问题,可以使用显式等待
相关问题 更多 >
编程相关推荐