2

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

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

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

4

2 回答 2

0

您可以通过这种方式进行屏幕截图

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\career.jpg"));
于 2013-05-23T18:20:18.000 回答
0

供参考的是有关 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();
于 2013-05-24T15:41:54.123 回答