user836112
user836112

Reputation: 383

Bring browser from back to front? (Selenium Web Driver, Java)

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

Upvotes: 3

Views: 11744

Answers (3)

Mykhailo Kovalevskyi
Mykhailo Kovalevskyi

Reputation: 109

Robot from java.awt; could help you:

Robot.mouseMove(webDriver.manage().window().getPosition().getX(),webDriver.manage().window().getPosition().getY());
Robot.mousePress(InputEvent.BUTTON1_MASK);
Robot.mouseRelease(InputEvent.BUTTON1_MASK);

Limitations:

  • if you have more than one monitor, you need to calculate absolute coordinates. As webDriver.manage().window().getPosition().getX() give you offset for the currently used display.
  • window should be visible

Upvotes: 0

Joshua
Joshua

Reputation: 3920

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

Upvotes: 2

CBRRacer
CBRRacer

Reputation: 4659

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.

Upvotes: 3

Related Questions