Selenium webdriver 在selenium web驱动程序的特定错误页面上进行屏幕截图

Selenium webdriver 在selenium web驱动程序的特定错误页面上进行屏幕截图,selenium-webdriver,Selenium Webdriver,我的情况是,我在网站上的许多按钮点击中都会看到相同的错误页面。 我想截图,这同一个错误页面,与该错误页面发生在网站的链接参考。 所以我想截图每当这个错误页面出现在任何点击。 您能建议我如何为此编写函数,以及如何在SeleniumWebDriver中的其他函数中调用该函数吗。 请分享一些代码示例 到目前为止,我只写为: File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copy

我的情况是,我在网站上的许多按钮点击中都会看到相同的错误页面。 我想截图,这同一个错误页面,与该错误页面发生在网站的链接参考。 所以我想截图每当这个错误页面出现在任何点击。 您能建议我如何为此编写函数,以及如何在SeleniumWebDriver中的其他函数中调用该函数吗。 请分享一些代码示例

到目前为止,我只写为:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  FileUtils.copyFile(scrFile, new File("D:\\Home\\Ruchi\\failure.png"));

但在这种情况下,我需要在每次失败发生后编写这些代码行。

使用try-and-catch块

try{
  // Put your script here
}
catch(Exception ex)
{
            File scrn=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

            // extracting date for folder name.
            SimpleDateFormat sdfDate1 = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
            Date now1 = new Date();
            String strDate1 = sdfDate1.format(now1);

            // extracting date and time for snapshot file
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//dd/MM/yyyy
            Date now = new Date();
            String strDate = sdfDate.format(now);

            String filefolder="./Snap/"+strDate1+"/";  // create a folder as snap in  your project directory

            // Creating folders and files
            File f = new File(filefolder+strDate+".jpeg");

            FileUtils.copyFile(scrn, new File(f.getPath()));
}
如果您的脚本失败,那么程序将跳转到catch块,然后代码将为您拍摄屏幕截图


希望它能对您有所帮助:)

请提供您想要使用的代码行。我有一些示例代码/场景可能会对您有所帮助。如果需要,请告诉我。SWNfindElement1(“ctl00\u CenterCPH\u btnUpload”,“上传批量供应商”);文件scrFile1=((TakesScreenshot)driver.getScreenshotAs(OutputType.File);copyFile(scrFile1,新文件(“D:\\Home\\Ruchi\\failure(upload bulk vendors.png));就像在这段代码中,点击这个按钮后,我得到了错误页面,所以同样有很多链接和按钮,所以我想得到每个事件的屏幕截图,带有不同的png文件名和发生的事件。你可以从我的个人资料中得到我的邮件并发送你的步骤。然后我可以说或给出答案。@Sagar007请与社区分享您的答案-并使用评论/聊天讨论-而不是离线。我们都想看到。但是在这里,png文件将在每个实例上被覆盖。错误生成时,我想得到不同文件名的屏幕截图,以及它发生的位置。确切地说,我知道。。为此,您可以使用时间和日期。。我已经更新了我的答案。。它将根据日期保存图片。。时间和空间是有用的,非常感谢。但是我还想知道在哪个页面或链接上发生异常jsut在我的代码系统中添加这个代码.out.println(driver.getCurrentUrl());它将为您提供已发生异常的当前页面