gpt4 book ai didi

java - 单击并按住 Web 元素时截取屏幕截图

转载 作者:行者123 更新时间:2023-11-29 09:02:08 27 4
gpt4 key购买 nike

我有一个带有焦点皮肤的 Web 元素,它仅在单击并按住时显示。怎么可能点击元素并按住,并在发布前捕获屏幕截图?到目前为止我尝试过的代码是:

Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.id("btn1"));
act.clickAndHold(ele);
<capture screenshot>
act.release(ele);

这确实截取了屏幕截图,但上面的代码 clickAndHold 没有保持对该元素的点击。我怎么做?

最佳答案

供引用here is the documentation on Actions .

我认为您在操作中遗漏了一些步骤:

WebElement ele = driver.findElement(By.id("btn1"));     
Actions builder = new Actions(driver);
builder.clickAndHold(ele);
// Any other actions you want to build onto this
// eg. builder.moveToElement(ele)
// .release(ele)
// .etc...
// Now build and get the Action
Action action = builder.build();
// Perform the action(s)
action.perform();
// Take your screenshot
// Build the release action
builder = new Actions(driver);
builder.release(ele);
// Get the built action and perform
action = builder.build();
action.perform();

这可能会变得复杂和令人厌烦。您可能需要考虑创建一个扩展 Actions 的新类,添加一个您可以调用的截屏方法,然后您的代码可以简化为如下内容:

WebElement elm = driver.findElement(By.id("btn1"));
Actions builder = new Actions(driver);
builder.clickAndHold(elm).takeScreenshot().release(elm);
builder.build().perform();

我没有编译器或 IDE 来测试它,但它应该可以工作。

以下是最终对 OP 起作用的方法:

WebElement elm = driver.findElement(By.id("btn1"));
Actions builder = new Actions(driver);
Action act = builder.clickAndHold(elm).build();
act.perform();
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\Img\\screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
act = builder.release(elm).build();
act.perform();

关于java - 单击并按住 Web 元素时截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16720561/

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