Maha Waqar
Maha Waqar

Reputation: 643

Selenium python click an element that appears on hover

My situation is that there is a grid of elements and on the hover of each element a button appears which you can click on then. enter image description here

See the difference between class="grid x0 y0" and class="grid x1 y0". id="ccSelectDesignButton" only appears on hover. I want to click on first element.

The code I am using is:

temp = driver.find_element_by_xpath("//div[@id='gridContainer']//div[@class='grid x0 y0']")
hat= ActionChains(driver).move_to_element(temp).click()
time.sleep(10)
button = driver.find_element_by_xpath("//a[@id='ccSelectDesignButton']")
hat.click(button).perform()

But every time I get this error:

 Unable to locate element: {"method":"xpath","selector":"//a[@id='ccSelectDesignButton']"}

I tried many other ways to locate it as well but still, I am unable to find and click it. Can anyone give me a better idea on how to deal with this?

Upvotes: 1

Views: 1248

Answers (2)

Vova
Vova

Reputation: 3543

Try to click element with javascript, like that:

driver.execute_script('$("a#ccSelectDesignButton").click();')

Upvotes: 0

KunduK
KunduK

Reputation: 33384

I think for mouse-over event you need to use perform() and once done you can able to select desire button element and click on that.

temp = driver.find_element_by_xpath("//div[@id='gridContainer']//div[@class='grid x0 y0']")
ActionChains(driver).move_to_element(temp).perform()
time.sleep(2)
button = driver.find_element_by_xpath("//a[@id='ccSelectDesignButton']")
ActionChains(driver).move_to_element(button).click(button).perform()

If this not resolved to your query please share the link.

Upvotes: 1

Related Questions