使用selenium和python登录网站

时间:2016-05-12 16:34:19

标签: python selenium autologin

我正在尝试使用selenium登录此网站: 但它说密码和登录不可见。我环顾四周,看到有人说要等,但等待似乎没有帮助。这是我的代码:

    # 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

2 个答案:

答案 0 :(得分:1)

修改你的xpath:

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

答案 1 :(得分:1)

您的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']")