Selenium/Python - hover and click on element

ぃ、小莉子 提交于 2021-02-07 02:48:29

问题


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

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

Any help would be greatly appreciated.


回答1:


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.




回答2:


here another useful link How to mouseover in python Webdriver

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

from selenium.webdriver.common.action_chains import ActionChains

thank you



来源:https://stackoverflow.com/questions/19933914/selenium-python-hover-and-click-on-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!