Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...