问题
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