Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Navigate Context Menus (Selenium, Python)

I am aware that Selenium apparently doesn't support navigating context menus. But I have also seen in several other threads that there is a work-around by using action chains. Using context_click() followed by arrow key commands to navigate through the menus.

All examples I've seen have used Java, and when I translated to Python, only the context_click() command would register. Strangely enough, I wouldn't get an error either. Other sources have said that the context menus Selenium produces are only system level, and thus, Selenium cannot touch them, only create.

So my question is, has anyone been able to successfully navigate and chose options from context menus through Selenium? Python examples are preferred, but I'll take any advice or answers I can get.

Edit:

Code:

driver.get('https://www.google.com/')
actionChains = ActionChains(driver)
actionChains.context_click().send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()

Context:

This is just a test script that I have been running to test this situation. In my personal project, I need to navigate the context menu to access a chrome extension. Since selenium can only interact within the web page I can't have it click on the button for the Chrome extension that is displayed by the browser. So this is the work-around I have been attempting.

Research:

https://testingrepository.com/how-to-right-click-using-selenium-webdriver/ - This source tells that seleniums context menus are only system level. In Java examples they also use a .build() command. As far as my knowledge, this command is not available to Python.

Select an Option from the Right-Click Menu in Selenium Webdriver - Java - Thread suggesting that arrow key commands should work. However, all examples use Java and the .build() command as well

https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/common/action_chains.py - shows that ActionChains() are Pythons's version of a .build() command. Might be common knowledge to some. I did not know this prior.

How to perform right click using Selenium ChromeDriver? - very similar question to mine. While one user suggests that the menu cannot be interacted with, another suggests the actionChains work-around will work.

like image 293
Wheat-Thin Avatar asked Aug 21 '17 04:08

Wheat-Thin


1 Answers

Thin,

I had the same problem and wonder nobody answered this already... It wasn't possible for me to solve it with selenium, cause selenium would navigate within the page. My solution:

import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(element).context_click().perform()
wsh.SendKeys("{DOWN}") # send the keys you want
like image 57
Falco Blumschein Avatar answered Oct 23 '22 11:10

Falco Blumschein