簡體   English   中英

捕獲警報的屏幕截圖

[英]Capture screenshot of alert

彈出后,我使用下面的代碼截取應用程序的屏幕截圖。

Alert alert = driver.switchTo().alert();
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("G:\\Screens\\sc1.jpg"));
String alertMsg = alert.getText();
System.out.println(alertMsg);
alert.accept();

但它正在拋出這個例外

線程“main”中的異常org.openqa.selenium.UnhandledAlertException:存在模式對話框:評估名稱已存在。

但是如果刪除屏幕截圖程序,代碼工作正常。

您必須在截取屏幕截圖之前處理警報...您可以在此處閱讀更多信息https://code.google.com/p/selenium/issues/detail?id=4412

您始終可以使用Robot獲取整個頁面的屏幕截圖。 我剛試了一下,這段代碼正在運行:

WebDriver driver;
@Before
public void init() throws Exception {
    driver = new FirefoxDriver();
    driver.get("http://www.tizag.com/javascriptT/javascriptalert.php");
}

@Test
public void bla() throws AWTException, IOException {
    WebElement element = driver.findElement(By.xpath("//input[@type=\"button\"]"));
    // Trigger the alert
    element.click();
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(image, "png", new File("c:\\localdev\\bla.png"));
    driver.switchTo().alert().accept();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM