我正在使用三个火狐驱动程序实例进行自动化.我需要将当前活动的firefox浏览器放到前面,因为我正在使用一些机器人类进行一些操作.我曾尝试过java中的谷歌浏览器的java脚本警报(相同的操作),它的工作正常.在windows中使用user32 lib.在firefox mac的情况下,它在后台显示警报,但网页没有出现在前面.
<code>((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
</code>
上面的代码我用于Mac中的chrome.相同的代码正在工作并显示firefox的警报,但窗口没有出现在前面.
请建议是否有任何其他方法在Firefox中执行相同的操作.
解决方法:
首先将窗口句柄存储在变量中,然后使用它稍后返回窗口.
<code>//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);
</code>
此外,您可以尝试在切换到窗口后最大化窗口,这也应该激活窗口.
<code>this.webDriver.manage().window().maximize(); </code>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!