2016-05-12 91 views
0

我想使用硒登录到此网站: 但它表示密码和登录不可见。我环顾四周,看到有人说要等待,但等待似乎没有帮助。这里是我的代码:使用硒与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 
+1

这可能会有所帮助:http://stackoverflow.com/questions/27927964/selenium-element-not-可见,例外 –

+0

嗯,它有点混乱,如何为我的特殊情况做到这一点。我能够解决使用标签来解决它。 – Corncobpipe

回答

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']") 
+0

谢谢!我会确保使用这个! – Corncobpipe

1

修改您的XPath:

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