2014-12-02 125 views
1

我試圖用python硒登錄到網頁。我發現了一個元素,它被啓用,但是當我嘗試send_keys()給它時,我得到一個錯誤。最主要的(我認爲)在錯誤輸出在python selenium中選擇元素

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with  

我的代碼是

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import contextlib 

with contextlib.closing(webdriver.Firefox()) as driver: 
    driver.get('http://www.etoro.com/au') 
    elem = driver.find_element_by_class_name('inputUsername') 
    print 'enabled:', elem.is_enabled() 
    print 'selected:', elem.is_selected() 
    elem.send_keys('myusername') 

,輸出是

enabled: True 
selected: False 
Traceback (most recent call last): 
    File "3_trying_again.py", line 10, in <module> 
elem.send_keys('ianafterglow') 
    File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 303, in send_keys 
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing}) 
    File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 385, in _execute 
return self._parent.execute(command, params) 
    File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute 
self.error_handler.check_response(response) 
    File "/Users/ian/miniconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response 
raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 
Stacktrace: 
at fxdriver.preconditions.visible (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:8959:12) 
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11618:15) 
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11635:11) 
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11640:7) 
at DelayedCommand.prototype.execute/< (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/[email protected]/components/command-processor.js:11582:5) 

所以,我需要做什麼?

+0

試試這個ELEM =驅動程序。 find_element_by_class_name('inputUsername') action = webdriver.ActionChains(driver) action.click(elm).perform(); – 2014-12-02 08:20:48

+0

在action.click(elem).perform()行上出現錯誤selenium.common.exceptions.ElementNotVisibleException:消息:元素當前不可見,因此可能不會與 – user2472657 2014-12-03 06:01:06

回答

2

爲了讓用戶名字段是可見的,你需要移動光標到登錄鏈接:

.... 

driver.get('http://www.etoro.com/au') 
action = webdriver.ActionChains(driver) 
action.move_to_element(driver.find_element_by_xpath(
    './/a[@class="top-link"]/span[text()="Login"]' 
)) 
action.perform() 
# TODO Need to wait until the `inputUsername` field is visible 
elem = driver.find_element_by_class_name('inputUsername') 
... 
+0

交互''.// a [ @learn =「top_link」]/span [text()=「Login」]'with selenium.common.exceptions.NoSuchElementException:Message:Unable to locate element:{「method」:「xpath」,「selector」:「 .// a [@class = \「top_link \」]/span [text()= \「Login \」]「} – user2472657 2014-12-03 06:06:03

+0

@ user2472657,我無法重現您的問題。代碼工作沒有錯誤。僅供參考,下面是我運行的確切代碼:http://codepad.org/DfdPxwNG – falsetru 2014-12-03 06:12:26

+0

問題是頂部鏈接中的下劃線與連字符。多麼尷尬!謝謝。 – user2472657 2014-12-04 01:43:10

0

您可以使用明確的等待:

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASSNAME, "inputUsername")) 
) 

... 
+0

建議的修復代碼成功運行,但程序以與selenium.common.exceptions.ElementNotVisibleException中的elem.send_keys('myusername')行最初相同的方式失敗:消息:元素當前不可見,因此可能不是與之交互 – user2472657 2014-12-03 06:09:02

0

我知道,這個問題就解決了, 我被困在類似的問題和相同的錯誤
我已經修復它只是讓我的腳本睡2秒,然後恢復它只是連接速度問題

... 
time.sleep(2) 
... 

不要忘了導入時間模塊

import time 

的願望,以幫助大家在今後的:d

0

我有類似的問題,硒無法集中精力,打開登錄模式。相反,它將重點放在下一個要素上。這是我用的定位:

elem = browser.find_element_by_xpath("//nav[2]/ul/li[3]/a").click() 

我只是改變[3] [2],它是能夠找到的元素並打開模式:

elem = browser.find_element_by_xpath("//nav[2]/ul/li[2]/a").click()