0

I'm trying to perform a Keys.ARROW_DOWN in selenium but it doesn't want to work, the code open the context menu, but the key arrow_down don't work, example of what I'm doing:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
driver.get('http://www.google.com.br')
time.sleep(1)
actions = ActionChains(driver)
actions.context_click().send_keys(Keys.ARROW_DOWN).perform()

chromedriver version 83

Someone can give me a light please of what I'm doing wrong? Thanks for the help!

edufgimenez
  • 60
  • 1
  • 3
  • 9
  • What is your end goal with this? Context menus can be a pain in my experience, but there may be a different way to do whatever you need doing, without involving the context menu. – 0buz Jun 11 '20 at 10:30
  • more details necessary here. Is the page providing right-click options or is the browser displaying its menu? – pcalkins Jun 11 '20 at 18:28

3 Answers3

1

Keys.ARROW_DOWN within Context Menu

Context Menu initiated through context_click() is generally invoked on a WebElement e.g. a link.

Invoking context_click() on a element opens a browser native context menu which is a browser native operation and can't be managed by Selenium by design.


Conclusion

Using Selenium you won't be able to interact with browser native context menu items using send_keys(Keys.ARROW_DOWN), send_keys(Keys.DOWN), etc.


Reference

You can find a relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

You can achieve with the help of pyautogui as shown in the below code

import time

import pyautogui
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys




options = webdriver.ChromeOptions()
options.add_argument( "user-data-dir=C:\\Users\\Sangeeta-Laptop\\AppData\\Local\\Google\\Chrome\\User Data\\Guest Profile");
cdriver = "C:\\Users\\Sangeeta-Laptop\\Downloads\\chromedriver_win32 (4)\\chromedriver"
driver = webdriver.Chrome(executable_path=cdriver, chrome_options=options)
driver.get('http://www.google.com.br')
time.sleep(1)
actions = ActionChains(driver)
actions.context_click().perform()
time.sleep(1)
pyautogui.press("down");

But as 0buz said in the comment, there are multiple ways to achieve your requirement. So please tell us what are you trying to achieve in detail and maybe we all will help you to resolve your issue :)

UnknownBeast
  • 979
  • 1
  • 6
  • 13
  • The question was _Keys.ARROW-DOWN in a context menu_ using **selenium** but not `pyautogui` :) – undetected Selenium Jun 11 '20 at 11:47
  • @DebanjanB I really appreciated that you replied but if you will see the question my friend then it is ambiguous. User is not performing the context_click on any element. It is not something like you can do the file upload using selenium. you may need to have third party tools to achieve that. So the bottom line is just by looking at the script, I thought that it is the requirement of that user :) – UnknownBeast Jun 11 '20 at 15:39
0

When you perform keys.ARROW_DOWN you should enter values into field then you can perform action.

Justin Lambert
  • 940
  • 1
  • 7
  • 13