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.
We can perform mouseover action in Selenium webdriver in Python by using the ActionChains class. We have to create an object of this class and then apply suitable methods on it. In order to move the mouse to an element, we shall use the move_to_element method and pass the element locator as a parameter.
The first step here would be to locate the main menu (AKA parent menu). Once that is done, the second step is to locate the desired element (child element) from the available options in the sub-menu. The final step would be to click on that child element.
You need to move the cursor to the mainMenuBTN (which is visible not the element that becomes visible when you hover the mouse over it ) and subMenuBTN is then displayed which you need to click.
A click is the most fundamental user action performed by anyone accessing the internet. It allows the user to navigate web pages or perform particular tasks by interacting with links, buttons, and other web elements.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With