Clicking a drop down menu (trigged by mouse hover) with selenium

Question:

I’m trying to click (with selenium) a drop down menu that only show it’s options if the mouse hovers it. Moreover, each option is a javascript.

I tried to use Select (from webdriver) and the following code, but had no success:

options = Options()
#options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-certificate-errors')

capabilities = options.to_capabilities()
capabilities['acceptInsecureCerts'] = True

driver = webdriver.Chrome('chromedriver',options=options, desired_capabilities=capabilities)
driver.get(url)
time.sleep(3)
driver.switch_to.frame(1)
button = driver.find_element(By.NAME,'link5')

The action consists of clicking at the drop down menu and select one of the options.

Thank you for the help!

Asked By: O. Mortosa

||

Answers:

Try the below code:

button = driver.find_element(By.NAME,'link5')
hover = ActionChains(driver).move_to_element(button)
hover.perform()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='menuItemText2']"))).click()

Explanation: To achieve this, your automation script should imitate human actions. i.e. first, mouse hover the menu Relatórios (In selenium, this can be achieved using ActionChains class as in the code above). After this click on the menu item once its visible(using visibility_of_element_located method) as in code above.

Answered By: Shawn