0

Currently, I'm using selenium to automate web application interactions, but in order to save execution time I need to open several tabs and switch between them. I've figured out how to use ctypes to issue all of the key combinations I need to do this I just don't know how to 'focus' or activate the window.

From what I've gathered, after opening a new tab and switching to it you have to execute

browser.switch_to_window(browser.current_window_handle)

which switches selenium's focus to the active tab. My issue however is activating the actual browser window within the OS, which is outside the scope of selenium.

My primary constraint is that I cannot use AutoIT under any circumstances. This of it as an "I made a bet" type situation. Additionally, I would prefer to stick to using vanilla libraries. Every package I have to install has a negative impact on the project so please use built-ins and libraries that ship with Python if possible.

Lastly, I don't intend to copy any code from here so if you can and are willing to help, please do so in a way to aid my understanding rather than just solve my problem.

I appreciate any guidance you can offer me, this is my first post on stackoverflow! Hopefully I've provided a decent amount of information.

Relevant info:
-using windows 7
-using firefox (target window for activation)
-using python 3
-can use robot framework
-using selenium (part of robot framework)
-using ctypes (ships with python 3)
-can import as many vanilla modules as needed,
  only constraint is I don't want to install additional

And just in case any of you know an easier way to do it, this is how I'm handling key combinations because they didn't work properly in selenium:

import ctypes

user32 = ctypes.windll.user32

#CTRL+T
sleep(1)
user32.keybd_event(0x11,0,0,0)
sleep(1)
user32.keybd_event(0x54,0,0,0)
sleep(1)
user32.keybd_event(0x54,0,2,0)
sleep(1)
user32.keybd_event(0x11,0,2,0)

#CTRL+TAB
sleep(1)
user32.keybd_event(0x11,0,0,0)
sleep(1)
user32.keybd_event(0x09,0,0,0)
sleep(1)
user32.keybd_event(0x09,0,2,0)
sleep(1)
user32.keybd_event(0x11,0,2,0)

#CTRL+W
sleep(1)
user32.keybd_event(0x11,0,0,0)
sleep(1)
user32.keybd_event(0x57,0,0,0)
sleep(1)
user32.keybd_event(0x57,0,2,0)
sleep(1)
user32.keybd_event(0x11,0,2,0)

#CTRL+1
sleep(1)
user32.keybd_event(0x11,0,0,0)
sleep(1)
user32.keybd_event(0x31,0,0,0)
sleep(1)
user32.keybd_event(0x31,0,2,0)
sleep(1)
user32.keybd_event(0x11,0,2,0)

#CTRL+2
sleep(1)
user32.keybd_event(0x11,0,0,0)
sleep(1)
user32.keybd_event(0x32,0,0,0)
sleep(1)
user32.keybd_event(0x32,0,2,0)
sleep(1)
user32.keybd_event(0x11,0,2,0)

Thanks! :)

userNo99
  • 1
  • 1
  • Use [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx). – IInspectable Nov 02 '17 at 07:46
  • Why are you using ctypes instead of using the selenium API to open new windows? Whether you open a tab or a new window, the behavior is identical from a testing perspective. https://stackoverflow.com/q/17325629/7432 – Bryan Oakley Nov 02 '17 at 10:56
  • I set it up to work through Selenium but for some reason .send_keys(Keys.CONTROL + 'T') specifically wouldn't work. Selenium's window switching doesn't actually activate the window within the OS which is what I need. My workaround is to collect the window handle and poll for its status while waiting for the user to click on the correct window. This works, but it's not ideal since it requires the user to interact. I will look into UI Automation, thanks IInspectable. – userNo99 Nov 02 '17 at 21:33

0 Answers0