简体   繁体   English

在新标签页中切换时无法在 Chrome Headless 模式下捕获屏幕截图

[英]Unable to capture screenshot in Chrome Headless mode while switched in new tab

I have below Configuration :我有以下配置:

Selenium 3.8.0硒 3.8.0

Java 8爪哇 8

Chrome Browser 60 Chrome 浏览器 60

Chromedriver v 2.31 64bit Chromedriver v 2.31 64 位

I'm running my test in chrome headless mode.我正在 Chrome 无头模式下运行我的测试。 The issues is, the browser get unresponsive while it switch to new tab and try to capture the snap.问题是,浏览器在切换到新选项卡并尝试捕获快照时没有响应。

Following Error recorded :记录以下错误:

[727.930][SEVERE]: Timed out receiving message from renderer: 600.000 [727.930][SEVERE]:从渲染器接收消息超时:600.000

[727.934][WARNING]: screenshot failed, retrying [727.934][警告]:截图失败,正在重试

The code where it causing the issue :导致问题的代码:

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 Method : 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 Method : 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();
    }
} 

While I'm running the same in GUI mode then all all works fine.虽然我在 GUI 模式下运行相同,但一切正常。 Can someone help what is wrong here ?有人可以帮助这里有什么问题吗?

The issue is within your switchWindow(WebDriver driver) function as follows :问题出在您的switchWindow(WebDriver driver)函数中,如下所示:

Within switchWindow(WebDriver driver) you are trying to switchTo().window(winHandle) without validating the winHandle as follows :switchWindow(WebDriver driver) 中,您尝试switchTo().window(winHandle)而不验证winHandle ,如下所示:

for (String winHandle : driver.getWindowHandles()) 
    driver.switchTo().window(winHandle);

Here you are not validating whether winHandle have picked up parentWindow or windowHandle of child window .在这里,您没有验证winHandle是否已选择parentWindowwindowHandlechild window

Solution :解决方案 :

So the solution will be to validate that the winHandle must not be the parentWindow as follows:因此,解决方案是验证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.

相关问题 无法在 Java Selenium 无头浏览器中的新 window 中捕获文本 - Unable to capture text inside new window in Java Selenium Headless browser Selenium webdriver 无法在 Chrome 无头模式下定位元素 - Selenium webdriver is unable to locate element in chrome headless mode 无法捕获硒中的屏幕截图 - Unable to capture screenshot in selenium 在无头模式下使用 Chrome 驱动程序配置时,在 WebDriverSampler 脚本中遇到预期条件失败错误 - Facing expected condition failed error in WebDriverSampler script while using Chrome driver config in headless mode Chrome 浏览器 Headless 问题:某些特定页面未在 Headless 模式下呈现 - Chrome Browser Headless problem : Some specific pages are not rendering in Headless mode 无法在Chrome无头模式下运行测试 - Fail to run tests in Chrome headless mode 在无头模式下通过 RemoteWebDriver 和 chrome 下载文件 - Download file through RemoteWebDriver and chrome in headless mode 无法在无头模式下检测弹出窗口 - Unable to detect pop ups in headless mode 无法在新标签Chrome中找到元素-使用Java的Selenium Webdriver - unable to locate element in new tab chrome - Selenium Webdriver with java Selenium (Java) - Chrome Headless - 无法上传文件 - Selenium (Java) - Chrome Headless - Unable to upload the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM