Selenium/Python - 悬停并单击元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19933914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:00:22  来源:igfitidea点击:

Selenium/Python - hover and click on element

pythonfirefoxhoverselenium-webdriver

提问by vbiqvitovs

I'm running into an issue with my Selenium script on Python. In the javascript web application that I'm interacting with, an element I need to click doesn't exist until I hover over it. I've looked and found various answers on how to hover, but the sequence needs to include the clicking of a new element during the hover event. Here is the code I am currently working with. The element is renamed from add to add1 when a hover occurs, once add1 exists; I should be able to click/send.keys to execute said element.

我在 Python 上的 Selenium 脚本遇到了问题。在我与之交互的 javascript Web 应用程序中,我需要单击的元素不存在,直到我将鼠标悬停在它上面。我已经查看并找到了有关如何悬停的各种答案,但该序列需要包括在悬停事件期间单击新元素。这是我目前正在使用的代码。一旦 add1 存在,当悬停发生时,元素从 add 重命名为 add1;我应该能够点击/send.keys 来执行所述元素。

...
driver = webdriver.Firefox()
from selenium.webdriver.common.action_chains import ActionChains
...
add = driver.find_element_by_css_selector('input.add')
Hover = ActionChains(driver).move_to_element(add)
Hover.perform()
SearchButton = driver.find_element_by_css_selector('input.add1')
SearchButton.click()

I'm new with Python and with programming in general, but I can't figure out how to sequence this correctly.

我是 Python 和编程新手,但我不知道如何正确排序。

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by TDHM

Following had worked for me, please give a try:

以下对我有用,请试一试:

add = driver.find_element_by_css_selector('input.add')
SearchButton = driver.find_element_by_css_selector('input.add1')

Hover = ActionChains(driver).move_to_element(add).move_to_element(SearchButton)
Hover.click().build().perform()

I'm not sure about above Python code. But you can use above logic.

我不确定上面的 Python 代码。但是您可以使用上述逻辑。

回答by sohom

here another useful link How to mouseover in python Webdriver

这里是另一个有用的链接 How to mouseover in python Webdriver

@TDHM you should mention this below line to make it works

@TDHM 你应该在下面提到这一点以使其工作

from selenium.webdriver.common.action_chains import ActionChains

thank you

谢谢你