0

I have a code that basically triggers a browser shortcut using actionchains. I'm using Chrome driver 2.27 and running python 3.6 through Jupyter notebook(though that shouldn't matter). The problem is that if the code runs along with the creation of the webdriver instance it works and shows the download bar.

Instead if I run the code to create the webdriver, open the new window, minimize it and then try running the code to trigger the shortcut it doesn't work. I'm guessing it has something to do with the driver losing focus because I manually peek at the new window created.

Code to create the webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
chromedriver= "chromedriver_v2.27.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.google.com")  

Code to trigger the browser shortcut

ActionChains(driver).key_down(Keys.CONTROL).send_keys('j').key_up(Keys.CONTROL).perform()
Raunak Thomas
  • 1,393
  • 1
  • 12
  • 28

1 Answers1

0

ActionChains won't work if the browser window is minimized. As a workaround you can call driver.maximize() before you execute the Action chain to send keyboard shortcut to the window.

driver.maximize()
ActionChains(driver).key_down(Keys.CONTROL).send_keys('j').key_up(Keys.CONTROL).perform()
GPT14
  • 809
  • 6
  • 13
  • Well unfortunately I need to use v2.27 of the chromedriver due to [this](https://stackoverflow.com/a/43664464/3625350). and any version before v3.33 has an issue with `driver.maximize()` as mentioned [here](https://stackoverflow.com/a/47221479/3625350). So I can't use this because I do not have admin rights to my laptop. – Raunak Thomas Jun 06 '18 at 20:04