java - chromedriver 尝试获取特定元素的屏幕截图时出错
问题描述
我收到了这个错误
(y + height) is outside of Raste
当我尝试获取需要向下滚动以查看全部内容的元素的屏幕截图时
如果这是问题所在,我如何在这段代码中添加向下滚动
public void getAllInfo() throws IOException {
String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));
Login webpage = new Login();
WebDriver pagee = webpage.driver;
pagee.navigate().to("https://www4.inscription.tn/ORegMx/servlet/ServletInfoEtud?action=toInfoPerson&Idsession="+cookie);
// Get entire page screenshot
WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[2]/td[1]/div[1]"));
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = taswira.getLocation();
// Get width and height of the element
int eleWidth = taswira.getSize().getWidth();
int eleHeight = taswira.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\info.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在使用此代码
static ChromeOptions options = new ChromeOptions();
static WebDriver driver = new ChromeDriver(options.setHeadless(true).addArguments("--window-size=1920,1080"));
chromedriver v2.45
phantomjs 在获取此元素的屏幕截图时工作正常
其他屏幕截图工作正常,因为它们不需要向下滚动即可查看全部内容
解决方案
推荐阅读
- reactjs - 我如何将已经有信息的功能组件呈现给另一个组件?
- docker - docker keycloak 扩展日志在哪里
- angular - 有没有办法使用更复杂的对象数据源在列而不是行上创建 Angular 动态材料表?
- git - VCS (Git) 与 Bayou 分布式系统之间有什么区别?
- django - Django 内容类型表数据迁移,同时使用旧数据库从 1.6 升级到 2.2
- devops - Docker 容器设置完成,但无法使用 aws 实例从浏览器访问 tomcat 和 webapp
- c++ - 我们可以阻止 std::list 整理内存吗?
- flutter - 智能卡读卡器有什么实现方式吗?
- python - 如何将聊天机器人与预训练的问答模型集成?
- redux-saga - Redux 传奇。如何使用 addEventListener 执行操作?