我尝试使用python selenium登录网页。我找到了一个元素并且它已启用,但是当我尝试send_keys()时它会出错。错误输出中的主要内容(我认为)是
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/fxdriver@googlecode.com/components/command-processor.js:8959:12)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/fxdriver@googlecode.com/components/command-processor.js:11618:15)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:11)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)
at DelayedCommand.prototype.execute/< (file:///var/folders/5b/ym07nh6d74gcn_773ynwqkth0000gn/T/tmpN1MV8l/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)
那么,我需要做什么?
答案 0 :(得分: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')
...
答案 1 :(得分: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"))
)
...
答案 2 :(得分:0)
我知道这个问题已经解决了, 我陷入了类似的问题和同样的错误 我修复它只是让我的脚本睡眠2秒然后恢复它只是连接速度问题
php -i | grep '\.ini'
不要忘记导入时间模块
...
time.sleep(2)
...
希望将来帮助任何人:D
答案 3 :(得分:0)
我有类似的问题,selenium无法关注并打开登录模式。相反,它专注于下一个元素。这是我使用的定位器:
elem = browser.find_element_by_xpath("//nav[2]/ul/li[3]/a").click()
我刚用[2]改变了[3],它能够找到元素并打开模态:
elem = browser.find_element_by_xpath("//nav[2]/ul/li[2]/a").click()