[英]Unable to capture text inside new window in Java Selenium Headless browser
[英]Unable to capture screenshot in Chrome Headless mode while switched in new tab
我有以下配置:
硒 3.8.0
爪哇 8
Chrome 浏览器 60
Chromedriver v 2.31 64 位
我正在 Chrome 无头模式下运行我的测试。 问题是,浏览器在切换到新选项卡并尝试捕获快照时没有响应。
记录以下错误:
[727.930][SEVERE]:从渲染器接收消息超时:600.000
[727.934][警告]:截图失败,正在重试
导致问题的代码:
if(myprofile.postalAddress.size()>0)
{
myprofile.getGetAddressMapIcon().get(0).click();
LogWriter.logger.info("Address Clicked");
CommonMethods.switchWindow(driver);
TakeScreenshot.passedScreenShot("GoogleMap");
driver.close();
CommonMethods.switchToParentWindow(driver);
LogWriter.logger.info("Map Window closed");
}
SwitchWindow
方法:
public static void switchWindow(WebDriver driver) throws IOException
{
parentWindow = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
LogWriter.logger.info("-----");
}
LogWriter.logger.info("Window Title : " + driver.getTitle());
}
TakeScreenshot
方法:
public static void passedScreenShot(String testname) throws IOException
{
File sourceFile = ((TakesScreenshot) DriverSetup.driver).getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");
destDir = System.getProperty("user.home")+"/AutomationTemp/Reports/Screenshots/PassedTest";
new File(destDir).mkdirs();
String destFile = dateFormat.format(new Date())+"RCON" + "_" + testname +".png";
try
{
Files.copy(sourceFile, new File(destDir + "/" + destFile));
}
catch (IOException e)
{
e.printStackTrace();
}
}
虽然我在 GUI 模式下运行相同,但一切正常。 有人可以帮助这里有什么问题吗?
问题出在您的switchWindow(WebDriver driver)函数中,如下所示:
在switchWindow(WebDriver driver) 中,您尝试switchTo().window(winHandle)
而不验证winHandle
,如下所示:
for (String winHandle : driver.getWindowHandles())
driver.switchTo().window(winHandle);
在这里,您没有验证winHandle
是否已选择parentWindow
或windowHandle
的child window
。
因此,解决方案是验证winHandle
不能是parentWindow
,如下所示:
public static void switchWindow(WebDriver driver) throws IOException
{
parentWindow = driver.getWindowHandle();
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
for (String winHandle : driver.getWindowHandles())
{
if (!parentWindow.equalsIgnoreCase(winHandle))
driver.switchTo().window(winHandle);
//you can do anything on the new tab/window
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.