C# 元素可以';找不到硒

C# 元素可以';找不到硒,c#,selenium,winappdriver,C#,Selenium,Winappdriver,我正在使用selenium运行我创建的测试。我现在想开始错误处理。我希望能够获取找不到的元素,并在错误日志中说类似于“element start not found”。如果不能做到这一点,我想打印发生异常的整行代码 我不知道该怎么做。目前,我只是将每个测试包装在一个try-catch中,并说找不到元素,但是这是非常广泛的,并没有缩小它找不到的元素的范围 我的会话设置 private string excelId = CommonMethods.ExcelOfficeVersion(); priv

我正在使用selenium运行我创建的测试。我现在想开始错误处理。我希望能够获取找不到的元素,并在错误日志中说类似于
“element start not found”
。如果不能做到这一点,我想打印发生异常的整行代码

我不知道该怎么做。目前,我只是将每个测试包装在一个try-catch中,并说
找不到元素
,但是这是非常广泛的,并没有缩小它找不到的元素的范围

我的会话设置

private string excelId = CommonMethods.ExcelOfficeVersion();
private const string AppDriverUrl = "http://127.0.0.1:4723";
private static WindowsDriver<WindowsElement> excelSession;

System.Web.Caching.Cache cache = new System.Web.Caching.Cache();
xl.Workbook WB;
public static bool skipTearDown = false;
WindowsElement create;
WindowsElement blankWorkBook;

public static DesiredCapabilities appCapabilities = new DesiredCapabilities();
[TestInitialize]

public void SetUp()
{

    try
    {


        appCapabilities.SetCapability("app", excelId);

        var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);

        var capabilities = new DesiredCapabilities();
        capabilities.SetCapability("app", "Root");
        excelSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);

        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        CommonMethods.keyCheck(excelSession);
        //blankWorkBook = excelSession.FindElementByName("Blank workbook");

        //cache.Insert("Create", create);

        CommonMethods.checkCreateExcel();

    }
    catch (Exception)
    {

        CommonMethods.ExceptionHandler("WinApp Driver failed to load Excel", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
    }

}
另一个典型测试

public void newExcelWorkbook()
{
    try
    {
        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        excelSession.FindElementByName("Blank workbook").Click();
        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        excelSession.FindElementByName("Create").Click();
        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        excelSession.FindElementByName("New").Click();
        CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the New button");
        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        excelSession.FindElementByName("E  Sample Data").Click();
        CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the E Sample Data button");
        Thread.Sleep(4000);
        excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    }
    catch (Exception)
    {
        CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
    }
    TearDown();

}

按照当前的方式,您可以将
catch(Exception)
替换为
catch(NoSuchElementException)
,这是在找不到元素时引发的特定于硒的异常。然而,我通常建议不要为了可能找不到元素而将整个测试包装在try-catch中

需要注意的一点(您可能已经错过了)是,
FindElement
在找不到元素时会抛出
NoSuchElementException
,并会告诉您找不到的元素的选择器。那么在你的情况下,如果

excelSession.FindElementByName("Blank workbook").Click();
找不到名为
空白工作簿
的元素,它将在异常详细信息中使用选择器抛出
NoTouchElementException


还有一件事需要注意:当您为驱动程序设置隐式等待持续时间时,它将在驱动程序对象的生命周期内保持不变—您可以在测试开始时设置一次,而不再触摸它。如果您需要在整个测试过程中等待不同的时间段,您可以通过
WebDriverWait
类使用显式等待。

感谢您的回复。我是按元素名搜索的,所以它没有给我那个异常,只是在那一行给了我一个InvalidOperationException。不是在我的测试中-你是否包装了驱动程序对象并按名称实现了自己的搜索,而不是使用selenium对象?@CraigGallagher对不起,我真正想问的是,你的
excelSession
对象到底是什么?我在我的问题中添加了一些代码,以显示我的典型测试,这样你就可以看到我在做什么it@CraigGallagher仍然没有真正解释什么是excelSession-它是WebDriver对象本身还是您正在包装它。我只能假设您已经实现了自己的按名称查找,因为
FindElementByName
不是任何Selenium驱动程序对象的方法。在我找到
excelSession
的确切内容或您显示
FindElementByName
的定义之前,我将无法提供帮助。
excelSession.FindElementByName("Blank workbook").Click();