繁体   English   中英

使用Selenium和python登录网站

[英]Logging into website using selenium with python

我正在尝试使用硒登录该网站:但是它说密码和登录名不可见。 我环顾四周,发现有人说要等待,但是等待似乎无济于事。 这是我的代码:

    # importing libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re, time, csv


driver = webdriver.Firefox()

driver.get("https://platform.openquake.org/account/login/")
driver.switch_to
driver.maximize_window
time.sleep(10)

username = driver.find_element_by_xpath("//input[@name='username']")
username.send_keys("hi there")

错误消息是:

ElementNotVisibleException: Element is not currently visible and so may not be interacted with

修改您的xpath:

username = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_username']")

您的XPATH实际上匹配两个元素。 非复数驱动程序方法( find_element_by_XXX )返回它们找到匹配的第一个元素,在这种情况下不是您想要的。

在这种情况下,一个好的调试工具是使用复数形式( find_elements_by_XXX ),然后查看匹配的元素数量。

在这种情况下,您应该执行Tanu建议的操作,并使用限制性更强的XPATH:

username = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_username']")
password = driver.find_element_by_xpath("//div[@class='controls']/input[@id='id_password']")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM