GDI +中发生一般错误(屏幕截图SaveAsFile,ExternalException) [英] A generic error occurred in GDI+ (screenshot SaveAsFile, ExternalException)

查看:89
本文介绍了GDI +中发生一般错误(屏幕截图SaveAsFile,ExternalException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图浏览可能有我答案的问题,但没有一个.

Hello I tried looking through questions that may have my answer but none of them did.

我正在进行Web自动化测试,并尝试对发生错误的时间进行截图并将其保存到另一个文件夹中的文件中.

I am doing web automation testing and trying to take a screenshot of when an error occurs and save it to a file in another folder.

我的程序在页面上搜索元素时有30秒的超时.如果30秒后未找到任何元素,则它将获取SS,重新加载页面,然后重试.

My program has a 30 second timeout when searching for elements on the page. If after 30 seconds no element is found, it takes a SS, reloads the page, and tries again.

第一个屏幕截图可以正常工作.但是在第二次运行时,它尝试将另一个屏幕截图保存到该文件夹​​,并且我得到了这个似乎难以理解的错误,它似乎是由一百种不同的东西引起的,所以我不确定是什么问题.

The first screenshot works fine. But on the second run through, it tries to save another screenshot to the folder and I get this incredibly vague error that is seemingly caused by a hundred different things, so I'm not quite sure what the problem is.

这是我的代码:

public void takeScreenShot()
    {
        string ssPath = _persistencePath += "\\Errors";

        string currTime = DateTime.Now.ToString(@"MMM-ddd-d-HH.mm");

        Screenshot ss = ((ITakesScreenshot)_driver).GetScreenshot();

        try
        {
            ss.SaveAsFile(ssPath + "\\ERROR-" + currTime + ".png", System.Drawing.Imaging.ImageFormat.Png);
            ssCount = 0;
        }
        catch (System.Runtime.InteropServices.ExternalException)
        {
            ssCount++;

            //error occurs here
            ss.SaveAsFile(ssPath + "\\ERROR-" + currTime + "(" + ssCount + ")" + ".png", System.Drawing.Imaging.ImageFormat.Png);
        }

我最初以为问题是它试图保存一个同名文件,因为如果错误发生在同一分钟内,则文件名是相同的.因此,这就是为什么我添加了该catch块,以试图在同一分钟内更改名称的原因.但这并不能解决问题.

I initially thought the issue was that it was trying to save a file of the same name, because if the error happens during the same minute then the file name is the same. So that's why I added that catch block, in an attempt to change the name if it occurs in the same minute. But that didn't fix it.

我再次尝试搜索并且找不到答案.任何帮助将不胜感激.

Again I tried searching all over and couldn't find an answer. Any help is greatly appreciated.

推荐答案

好吧,如果有人好奇,我就解决了.原来我只是个白痴.

Well, if anyone's curious I solved it. Turns out I'm just an idiot.

string ssPath = _persistencePath += "\\Errors";

此行在第二次运行时将另一个\ Error附加到目标路径.从而使路径无效,因为\ Errors \ Errors不存在.

this line was appending another \Errors to the target path on the second run though. thus invalidating the path, because \Errors\Errors didn't exist.

感谢所有发表评论/尝试提供帮助的人!

Thanks to everyone who commented/tried to help!

这篇关于GDI +中发生一般错误(屏幕截图SaveAsFile,ExternalException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆