admin管理员组

文章数量:1313753

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

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

Share Improve this question asked Dec 8, 2011 at 22:16 user836112user836112 3833 gold badges7 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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

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

本文标签: javascriptBring browser from back to front (Selenium Web DriverJava)Stack Overflow