Skip to content Skip to sidebar Skip to footer

How To Bring Selenium Browser To The Front?

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.webdriver.common.by impo

Solution 1:

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 importwebdriverdriver= 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

Solution 2:

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()

Solution 3:

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!

Solution 4:

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.

Solution 5:

Out of box, selenium does not expose driver process id or Browser hwnd but it is possible. Below is the logic to get hwnd

  • When driver is initialized, get the url for hub and extract the port number
  • From port number, find the process id which is using this port for listening, ie. PID of driver
  • After navigation, from all instances of iexplore find the parent PID matches the pid of driver, i.e browser pid.
  • Get the Hwnd of browser pid once browser hwnd is found , you can use win32 api to bring selenium to foreground.

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/

Post a Comment for "How To Bring Selenium Browser To The Front?"