2016-05-12 90 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']")