if the browser window is in bg, then this line doesn\'t work.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webd
To answer your original question, you need to use the maximize window method for the browser to regain focus and be placed in foreground. it should be something like this in Python:
browser.maximize_window()
Out of box, selenium does not expose driver process id or Browser hwnd but it is possible. Below is the logic to get hwnd
its not possible to post full code here, the full working solution (C#) to bring browser in front is on my blog
http://www.pixytech.com/rajnish/2016/09/selenium-webdriver-get-browser-hwnd/
Now I don't do python, but how I got around this in Groovy was the following:
browser.js."alert()"
webdriver.switchTo().alert().accept()
I called the javascript alert
function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept()
. Hopefully there is something similar in Python.
Hope this helps!
Tested and works in Python 36
driver1 = webdriver.Chrome()
driver1.get("http://www.python.org")
pyName = driver1.title
pyHandle = driver1.window_handles[0]
driver2 = webdriver.Chrome()
driver2.get("http://www.ruby-lang.org/en")
rubyName = driver2.title
rubyHandle = driver2.window_handles[0]
#driver 2 at this time will be in the foreground
driver1.switch_to.window(pyHandle)
driver1.switch_to_window(driver1.current_window_handle)
Edit: switch_to_window is deprecated, use switch_to.window
This is a complete hack, but it worked for me using IE, selenium remote driver in Windows 7.
driver.get("about:blank")
driver.minimize_window()
driver.maximize_window()
driver.minimize_window()
driver.maximize_window()
I send a blank page, then minimize and maximize twice. It seems to result in the newly created IE having foreground focus.
This worked for me to bring the browser window to the foreground. I tested using chromedriver on Selenium 3.6.0 on Python 3.
from selenium import webdriver
driver = webdriver.Chrome()
driver.switch_to.window(driver.current_window_handle)
Related answer in Java - https://sqa.stackexchange.com/questions/16428/how-can-i-bring-chrome-browser-to-focus-when-running-a-selenium-test-using-chrom