Hovering over element using Selenium

Viewed 80

I'm trying to automate some stuff on https://translate.google.com. The problem is that I fail when it comes to the point when I need to press the language swap button.

I'm defining the swapping button as swap_button = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ow23"]/div/span/button')))

When I try to click it using swap_button.click() nothing seems to happen until I hover over the button manually.

I've also tried using ActionChains(driver).move_to_element(swap_button).perform() to simulate the hover, though even with time.sleep(5) it doesn't seem to make any difference.

As an alternative for swap_button.click() I tried using swap_button.send_keys(Key.ENTER) but the problem was still remaining.

I do not know if I am just not able to get it working or if Google has done something to prevent things like web-automation from happening.

Any kind of help is appreciated.

2 Answers

Try waiting until the button is clickable and fix its locator:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aCQag .VfPpkd-Bz112c-LgbsSe.yHy1rc.eT1oJ.q7sDqe.lRTpdf.U2dVxe")))
elem = driver.find_element_by_css_selector(".aCQag .VfPpkd-Bz112c-LgbsSe.yHy1rc.eT1oJ.q7sDqe.lRTpdf.U2dVxe")
elem.click()

I am not sure but i think it should be :

wait(your_driver_name, some_time).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ow23"]/div/span/button')))
Related