Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring the Firefox Browser to Front using selenium Java (Mac OSX)

I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.

((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();

The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.

Please suggest if there any other method for doing the same in firefox.

like image 668
Jojo John Avatar asked Oct 03 '13 19:10

Jojo John


People also ask

How do I start Firefox in Selenium on Mac?

Visit the link − https://www.selenium.dev/downloads/ and go to the Browser segment. Click on the Documentation link below Firefox. In the Supported platforms page, click on geckodriver releases link. Then click on the link corresponding to Mac OS.

How do I switch between browsers in Selenium?

We can switch different browser tabs using Selenium webdriver in Python using the method switch_to. window. By default, the webdriver has access to the parent window. Once another browser tab is opened, the switch_to.


2 Answers

Store the window handle first in a variable, and then use it to go back to the window later on.

//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();

//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')"); 
this.webDriver.switchTo().alert().accept();

//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);

Additionally, you can try to maximise the window after switching to it, which should also activate it.

this.webDriver.manage().window().maximize();
like image 138
Faiz Avatar answered Sep 21 '22 05:09

Faiz


Try switching using the window name:

driver.switchTo().window("windowName");

Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:

for (String handle : driver.getWindowHandles()) {
  driver.switchTo().window(handle);
}

Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp

like image 23
RAlex Avatar answered Sep 22 '22 05:09

RAlex