Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium/Python - hover and click on element

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.

like image 673
vbiqvitovs Avatar asked Nov 12 '13 15:11

vbiqvitovs


People also ask

How do you mouse hover and click in Selenium Python?

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.

How do you mouse hover and click in Selenium?

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.

How do you hover over and click on an invisible element using Selenium Webdriver in Python?

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.

What is click () method in Selenium?

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.


1 Answers

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.

like image 101
Alpha Avatar answered Oct 04 '22 10:10

Alpha