Selenium/Python - 悬停并单击元素

2022-01-22 00:00:00 python selenium-webdriver firefox hover

问题描述

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

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.

...
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()

我是 Python 和一般编程的新手,但我不知道如何正确排序.

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

任何帮助将不胜感激.


解决方案

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

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()

我不确定上面的 Python 代码.但是你可以使用上面的逻辑.

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

相关文章