【问题标题】:How to click an element visible after hovering with selenium?如何单击硒悬停后可见的元素?
【发布时间】:2013-05-25 17:18:24
【问题描述】:

我想单击悬停后可见的按钮。它的html是:

<span class="info"></span>

我使用了这个代码:

import selenium.webdriver as webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = "http://example.com"

driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_class_name("info")
hov = ActionChains(driver).move_to_element(element)
hov.perform()
element.click()

但它不起作用。我收到与最后一行代码有关的错误element.click():

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

有什么建议吗?

【问题讨论】:

  • 错误究竟是什么?
  • @Arran,我编辑了这个问题。

标签: python selenium selenium-webdriver hover


【解决方案1】:

我敢打赌,你应该等待元素直到它变得可见。

三个选项:

  • 致电time.sleep(n)
  • 使用WebDriverWait 就像建议的herehere

我会选择第二个选项。

更新:

在这个特定的网站上,通过 selenium 悬停根本不起作用,所以唯一的选择是通过 execute_script 使用 js 点击按钮:

driver.execute_script('$("span.info").click();')

希望对您有所帮助。

【讨论】:

  • 感谢您的建议,但实际上我已经尝试过time.sleep,但似乎并非如此。
  • 好的,WebDriverWait 应该可以了,如果您遇到问题,请告诉我。
  • 很抱歉回答迟了,但需要仔细阅读您的链接。我仍然认为我的问题有所不同,因为我的案例中的元素/按钮始终可见/可访问。所以我可以在页面加载时立即将鼠标悬停在它上面。两者之间没有 ajax/js 代码执行。我只是无法使用find_element_byxpath 正确本地化元素。
  • 知道了,您能否提供该页面的链接,以便我为您找到解决方法?
  • 我很乐意。出于“安全”原因,我认为最好不要发布它。我们可以通过电子邮件联系吗?
猜你喜欢
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多