gpt4 book ai didi

java - 使用 RemoteWebDriver 截取屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:37 24 4
gpt4 key购买 nike

有没有办法获得带标题的 Selenium 屏幕截图?我已经尝试过下面的代码,但屏幕截图没有标题。我有一个测试用例,需要单击链接并确保该操作必须进入新选项卡,因此作为证据,我必须附加捕获有两个选项卡。

public static void main (String args[]) throws IOException {
DesiredCapabilities dc = new DesiredCapabilities();
RemoteWebDriver driver;

URL url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
dc.setCapability(CapabilityType.PLATFORM, "MAC");

driver = new RemoteWebDriver(url, dc);
driver.manage().window().maximize();
driver.get("https://google.com");

new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));

File getImage = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(getImage, new File("/Users/path/screenshot.jpg"));

driver.quit();
}

当前结果 Current result

预期结果 Expected result

最佳答案

不,你不能,Selenium 中的屏幕截图功能会获取渲染的 DOM 的图像。浏览器镶边(即由运行浏览器的操作系统呈现的浏览器的 UI 组件)不是 DOM 的一部分,因此 Selenium 不知道它。

下一个问题是为什么你想要在图像中使用浏览器镶边?如果您只是想找出显示的 URL(正如您的问题所暗示的那样),您可以使用命令 driver.getCurrentUrl(); 代替。

关于java - 使用 RemoteWebDriver 截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659930/

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