[英]Take screenshot while clicking and holding a web element
我有一個帶有焦點皮膚的Web元素,僅在單擊並按住時顯示。 在發布之前,如何單擊並按住並捕獲屏幕截圖? 到目前為止,我嘗試過的代碼是:
Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.id("btn1"));
act.clickAndHold(ele);
<capture screenshot>
act.release(ele);
確實會截取屏幕截圖,但是上面的代碼clickAndHold並未保留對該元素的點擊。 我怎么做?
您可以通過這種方式進行屏幕截圖
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\career.jpg"));
作為參考, 這里是有關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的新類,該類添加了可以調用的take屏幕截圖方法,然后您的代碼可以簡化為:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.