我正在尝试使用pythonselenium登录到网页。我找到了一个元素并将其启用,但是当我尝试向其发送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)
那么,我该怎么办?
为了使用户名字段可见,您需要将光标移动到登录链接:
.... 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') ...