加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何在使用selenium悬停后单击可见的元素?

发布时间:2020-12-16 23:19:25 所属栏目:Python 来源:网络整理
导读:我想点击悬停后可见的按钮.它的 HTML是: span class="info"/span 我用过这段代码: import selenium.webdriver as webdriverfrom selenium.webdriver.common.action_chains import ActionChainsurl = "http://example.com"driver = webdriver.Firefox()driv
我想点击悬停后可见的按钮.它的 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'

有什么建议吗?

解决方法

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

三种选择:

> call time.sleep(n)
>使用WebDriverWait,如建议here和here

我会选择第二个选项.

UPD:

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

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

希望有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读