【问题标题】:How to Capture Full page screenshot of using selenium WebDriver如何捕获使用 selenium WebDriver 的整页屏幕截图
【发布时间】:2023-01-23 16:19:44
【问题描述】:

我已经为使用硒的响应测试创建了硒整页屏幕截图测试。 但是,如果我要针对 SharePoint 网站运行,那么它不会截取整页屏幕截图,但如果我会提供任何特定的公共网站来捕获屏幕截图。

这是代码sn-p:

public class resTest {
@Test(groups = {"Test1"})
    public void ResTest() throws InterruptedException, IOException {
        System.setProperty("webdriver.gecko.driver", "C:\\browserdriver\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get(getUrl());
        
        driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

        //Enter email ID
        WebElement enterEmail = wait.until(ExpectedConditions.elementToBeClickable(By.name("loginfmt")));
        enterEmail.sendKeys(getUsername());

        WebElement clickNext = wait.until(ExpectedConditions.elementToBeClickable(By.className("win-button")));
        clickNext.click();

        //Enter Password
        WebElement SubmitPass = wait.until(ExpectedConditions.elementToBeClickable(By.name("passwd")));
        SubmitPass.sendKeys(getPassword());

        //Click on Sign In button
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Sign in']")));
        element.click();

        WebElement afterNext = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='No']")));
        afterNext.click();

        Thread.sleep(9000);

        File src = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
        FileHandler.copy(src, new File(System.getProperty("user.dir")+"/Res2/screen.png"));

    }
}

【问题讨论】:

    标签: selenium selenium-webdriver sharepoint sharepoint-online


    【解决方案1】:

    在无头浏览器上,我使用以下命令获取整个页面的屏幕

    full_width  = driver.execute_script('return document.body.parentNode.scrollWidth')
    full_height = driver.execute_script('return document.body.parentNode.scrollHeight')
    driver.set_window_size(full_width, full_height)
    driver.save_screenshot(path)
    

    无头浏览器窗口的优点是您可以将其调整为您想要的任何大小,即使它大于您的屏幕分辨率。例如,如果您的屏幕为 1920x1080,您将无头浏览器窗口的大小调整为 1000x5000 并截取屏幕截图,则图像将为 1000x5000 像素。

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 1970-01-01
      • 2012-11-29
      • 2014-07-15
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2019-03-11
      相关资源
      最近更新 更多