【问题标题】:Selenium hovering over an element硒悬停在元素上
【发布时间】:2012-09-18 16:06:18
【问题描述】:

如何将鼠标悬停在某个元素上一段时间,组合框子菜单中有隐藏的项目,除非我在子菜单上悬停 1-5 秒,否则它不会显示。基本上在python中,我如何设置将鼠标悬停在元素上的时间?

到目前为止:

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

firefox = webdriver.Firefox()
firefox.get('')
element_to_hover_over = firefox.find_element_by_id("")

hover = ActionChains(firefox).move_to_element(element_to_hover_over)
hover.perform()

【问题讨论】:

  • 可以显示页面源代码吗?

标签: python selenium hover


【解决方案1】:

我想你正在寻找类似time.sleep的东西

# Pause for 5 seconds
import time
time.sleep(5)

虽然如果您使用 1-5 秒作为超时条件,您也可以使用 Selenium 的 WebDriverWait 功能(请参阅 docs)在 1-5 秒后等待您希望发生的任何事情。

【讨论】:

  • 它可以工作,但不稳定,有时会悬停,有时不会,还有其他想法吗?
  • @user1582983 嗯,不确定。那一定是悬停动作中的错误。根据我的经验,Selenium 对于悬停之类的操作可能相当古怪。如果有办法避免完全依赖此功能,我会推荐它。
  • 你能看看我正在抓取的网站吗?这是网站,mbsdisclosure.fanniemae.com/PoolTalk2/index.html, > Advanced Search > #then the combo box I am looking for the sub menu option for Preliminary Mega > Preliminary Mega: Fannie Mae/Ginnie Mae backed Adjustable Rate
【解决方案2】:

我建议寻找一种创造性的方式来解决它。通常,悬停菜单通过 javascript 的 sn-ps 激活或停用可见性,并且选项的实际编码在它们出现在网页上之前在功能上就在那里。如果您使用 selenium 刮取隐藏文本的组合框元素,然后使用 selenium 的内置 select 函数来操作组合框,您可能会找到一种方法来操作组合框,而无需求助于实际的鼠标操作(一个错误且不是很操作网页的可靠方法)。

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2020-02-03
    • 2011-06-02
    • 2011-10-10
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多