gpt4 book ai didi

java - Ashot 没有截取正确元素的屏幕截图

转载 作者:行者123 更新时间:2023-11-30 05:46:35 24 4
gpt4 key购买 nike

我正在尝试截取一个网页中给出的表格的屏幕截图。和我在代码中提供的相同元素 xpath 但是 Ashot 代码正在捕获其他位置的屏幕截图。

我还尝试过其他截屏代码,

Screenshot screenshot = new AShot().takeScreenshot(driver,driver.findElement(By.xpath(webElementXpath)));

但它给了我错误,我可以通过阅读此链接来修复该错误:https://github.com/pazone/ashot/issues/93然后我使用下面的代码:

WebElement myWebElement = driver.findElement(By.xpath("//center/table/tbody/*"));
Screenshot fpScreenshot = new AShot()
.coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver,myWebElement);

ImageIO.write(fpScreenshot.getImage(),"PNG",new File("/Users/sanatkumar/eclipse-workspace/com.ScreenshotUtility/Screenshots/error.png"));

请帮忙,因为此代码为我提供了网页随机部分的屏幕截图。我也 try catch 其他元素,但我再次没有获得正确的屏幕截图:

请注意,我的表格在网页上不完全可见,我必须手动向下滚动才能查看完整表格。我需要编写其他代码才能获得表格的完整屏幕截图吗?

我的网站也是基于角度的,我正在尝试使用 selenium java 实现自动化。我这样做的原因是因为在 Protractor 中我找不到任何像 Ashot 这样的 API。如果有人知道请告诉我。

最佳答案

通过添加 ShootingStrategy,我能够仅捕获此页面底部具有 id =“post-form”属性的表单元素。

来自 https://github.com/pazone/ashot 的文档

Different WebDrivers take screenshots differently. Some WebDrivers provide a screenshot of the entire page while others handle the viewport only.

...

There are built-in strategies in ShootingStrategies for different use cases.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import javax.imageio.ImageIO;
import java.io.File;

public class Main
{
public static void main(String args[]) throws Exception
{
System.setProperty("webdriver.gecko.driver", "./geckodriver");
System.setProperty("webdriver.firefox.bin", "/usr/bin/firefox");
WebDriver driver = new FirefoxDriver();
driver.get("https://stackoverflow.com/questions/54724963/ashot-is-not-taking-screenshot-of-correct-element");
Thread.sleep(2000);
WebElement webElement = driver.findElement(By.id("post-form"));
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver,webElement);
ImageIO.write(screenshot.getImage(),"PNG",new File("/home/dan/ElementScreenshot.png"));
Thread.sleep(2000);
driver.quit();
}
}

输出: enter image description here

关于java - Ashot 没有截取正确元素的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54724963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com