Bring browser from back to front? (Selenium Web Driver, Java)
Solution 1
You should be able to do this with
driver.SwitchTo().Window("//name of the window");
and that will bring whatever window you want into focus.
Solution 2
Now I don't use selenium, but I use Geb which is a wrapper around selenium so calling the javascript may be different for you, but this is how I did it (should be similar)
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()
.
user836112
Updated on June 27, 2022Comments
-
user836112 almost 2 years
Is there any way I can bring browser from the back to the top(front)??
The situation is that there are two browsers, firefox and chrome. So, I instantiated two drivers, new FirefoxDriver() and new ChromeDriver(), let's call them fdriver and cdriver.
What I want is when the program is using Firefox, the firefox browser should be on the top. And so does Chrome. But, I am stuck how to bring the browser to the top when they are on the back.
I already tried,
Javascript: self.focus() and window.focus(). / WebDriverBackedSelenium to make driver back to selenium and use windowMaximize and windowFocus
Any idea is appreciated, thanks
-
user836112 over 12 yearsI think it will only works for two windows from same driver. Does it works for two different drivers? I tested it by doing this: 1. new Firefoxdriver(); 2. new InternetExplorerDriver(); 3. fdriver.switchTo().Window(fdriver.getWindowHandles().iterator().next());
-
user836112 over 12 yearsIf, both windows are from fdriver, the switchTo() will work. Please correct me if I am doing it wrong way. Thanks
-
CBRRacer over 12 years@user836112 you are correct it wouldn't do that. I think the better way to handle that is to look into Selenium Grid to run multiple tests on multiple machines. That way you can instantiate a single WebDriver per virtual machine
-
user836112 over 12 yearsWhat I can think about is that before I instantiate another driver, just make the current driver quit. If that case, you can make sure you can keep the driver on the front.
-
CBRRacer over 12 yearsyes if you aren't making calls to each on e at the same time then close or quite the current driver and instantiate the next on and that would make sure that the driver (WebDriver) won't be confused as to which driver to use and what page or DOM it is supposed to be looking at.
-
user836112 over 12 yearsActually, I passed driver as argument. So, ideally, the program will switch different browser to run test and keep using the same Firefox or Chrome. I don't want to close browser since I would like to keep session alive.