如何使用带有 Java 的 Selenium WebDriver 获取可滚动网页的整页屏幕截图?
问题描述:
现有代码只截取可见屏幕的屏幕截图.我正在使用 Chromedriver
.
Existing code which takes screenshot of only visible screen. I am using the Chromedriver
.
System.setProperty("webdriver.chrome.driver", "D:/chromedriver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.bbc.com");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png"));
driver.close();
答
请找到下面的代码,您可以滚动并截取任意数量的屏幕截图.请注意 elements
Webelement.存储它们并相对滚动.您可以根据需要的屏幕截图数量滚动.
Please Find the below code, you can scroll and take screenshots as many as you want. Note the elements
Webelement. Store them and scroll relatively.
You can Scroll depending upon how many screenshots you want.
driver.get("http://www.bbc.com");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:/chromedriver/scr3.png"));
WebElement elements = driver.findElement(By.xpath(".//*[@id='page']/section[6]/div/div/div[1]/ul/li[3]/div/div[2]/h3/a"));
Thread.sleep(3000L);
JavascriptExecutor js = (JavascriptExecutor) driver;
int yPosition = elements.getLocation().getY();
js.executeScript("window.scroll (0, " + yPosition + ") ");
Thread.sleep(3000L);
File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile1, new File("D:/chromedriver/scr4.png"));
driver.close();