当前位置: 首页 > 面试题库 >

在pythonselenium中选择元素

蓬宾白
2023-03-14
问题内容

我正在尝试使用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')
...


 类似资料:
  • 问题内容: 在我们的应用程序中,我们解析一个网页并将其加载到iFrame中的另一个页面中。该加载页面中的所有元素都有其令牌标识。我需要通过那些tokenid -s选择元素。意思是-我单击主页上的元素,然后在iFrame中选择页面中的相应元素。在jQuery的帮助下,我通过以下方式进行操作: 但是,通过此功能,我只能选择当前页面中的元素,而不能选择iFrame中的元素。谁能告诉我如何在加载的iFra

  • 如何只选择父级而不选择子级? List summaryLinks=summary.findelements(by.xpath(“”));

  • 问题内容: 我的网站上有一个选择控件。我正在使用页面对象与页面进行交互。如果我这样做(在我的课程下的前两行和我的方法中) 它以空指针失败。我也尝试了没有。 现在,如果我在我的方法中执行此操作,则一切正常,然后选择正确的项目 这是该控件的实际网页摘要(已编辑以保护无辜者) 让我说我可以解决我的问题, 但是 我不明白为什么“ 正常 ”路径无法正常工作。 问题答案: 那是因为该类具有以下构造函数: 见J

  • 问题内容: 如何从这样的列表中选择仅父级的链接元素? 所以在css中,但不是 谢谢 问题答案: 但是,如果您要专门针对最外层的ul,则需要在根ul上设置一个类: 然后是: 确保您只有root li元素的另一种方法: 它看起来很笨拙,但可以解决问题

  • 问题内容: 如何使用选择器在CSS中选择元素的上述元素 在这里,我想使用class ,以便可以使用CSS选择器获取上述元素。 问题答案: 纯CSS不可能做到这一点…

  • 本文向大家介绍Jsoup 使用CSS选择器选择元素,包括了Jsoup 使用CSS选择器选择元素的使用技巧和注意事项,需要的朋友参考一下 示例 您可以在此处找到支持的选择器的详细概述。