python硒元素的选择

2024-10-01 11:28:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pythonselenium登录一个网页。我找到了一个元素,它被启用了,但是当我试图向它发送_keys()时,我得到了一个错误。错误输出的主要内容(我认为)是

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

我的代码是

^{pr2}$

输出是

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)

那么,我需要做什么?在


Tags: inpyexecutevarseleniumlinecommandat
3条回答

可以使用显式等待:

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"))
)

...

要使用户名字段可见,需要将光标移到登录链接:

....

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')
...

我知道这个问题已经解决了, 我陷入了类似的问题和同样的错误
我已经解决了,只要让我的脚本睡眠2秒,然后恢复它只是连接速度问题

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

别忘了导入时间模块

^{pr2}$

希望以后对任何人都有帮助:D

相关问题 更多 >