问题描述
我正在用硒2编写一个测试脚本,该脚本带有一个弹出窗口的屏幕快照.弹出窗口是pdf.
I'm writing a testscript in selenium 2 that takes a screenshot of a popup. The popup window is a pdf.
点击链接后,我正在使用代码
After clicking the link, I'm using the code
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
但是,要截取屏幕截图,只截取主页而不是弹出窗口.是否可以使用硒2,将焦点更改为新的弹出窗口,截取屏幕截图,然后关闭弹出窗口并切换回主窗口?
To take a screenshot, however, that only takes a shot of the main page and not the popup window. Is there a way to have selenium 2, change focus to the new popup, take the screenshot, and then close the popup and switch back to the main window?
推荐答案
您必须使用以下方法切换驱动程序的焦点:
You have to switch the focus of the driver with something like this:
String mainWindow = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(mainWindow)) {
driver.switchTo().window(handle)
//put your screenshot call here
driver.close();
driver.switchTo().window(mainWindow);
}
}
如果您拥有更多其他窗口,则当然会截取所有其他窗口的屏幕截图.然后,您需要知道确切的窗口句柄,然后切换到该窗口句柄.
This of course will take a screenshot of all other windows if you got more of them. Then you need to know the exact window handle and just switcht to that.
这篇关于将焦点切换到弹出窗口并截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!