的webdriver和C# - NoSuchElement例外 [英] WebDriver and C# - NoSuchElement exception

查看:179
本文介绍了的webdriver和C# - NoSuchElement例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的代码,选择从定列表一个选项,它通常工作,但有时失败,第二,如果NoSuchElement例外。我的印象是,如果没有找到的元素,它只是追溯到通过循环一次。我相信这个解释很简单......谁能告诉我吗?



 公共静态无效selectFromList(字符串VLIST,字符串vText, IWebDriver司机)
{
为(INT秒= 0;秒++)
{
System.Threading.Thread.Sleep(2500);
如果(SEC> = 10)Debug.Fail(超时:+ VLIST);
如果(driver.FindElement(By.Id(ConfigurationManager.AppSettings [VLIST))显示。)破;
}
新SelectElement(driver.FindElement(By.Id(ConfigurationManager.AppSettings [VLIST])))SelectByText(vText)。
}


解决方案

好,我是Java的家伙,所以我不会为你提供的代码,而是算法:




  • 此致代码(我认为)应该检查,如果元素是显示,如果没有,请等待额外的2.5秒

  • 失败的原因,有时需要超过第一2,5秒显示元素。在这种情况下,检查是否显示该元素将抛出例外



所以,基本上你应该做一些异常处理中的for循环而捕获此异常,什么也不做。在Java中的由做过尝试块。但是,因为我不知道C#你必须找出如何在这语言


进行

I have the following code for selecting an option from given list and it usually works, but sometimes it fails with NoSuchElement exception on the second if. I was under the impression that if it does not find the element it just goes back to through the loop again. I believe the explanation is pretty simple... Could anyone enlighten me?

    public static void selectFromList(String vList, String vText, IWebDriver driver)
    {
        for (int sec = 0; ; sec++)
        {
            System.Threading.Thread.Sleep(2500);
            if (sec >= 10) Debug.Fail("timeout : " + vList);
            if (driver.FindElement(By.Id(ConfigurationManager.AppSettings[vList])).Displayed) break;
        }
        new SelectElement(driver.FindElement(By.Id(ConfigurationManager.AppSettings[vList]))).SelectByText(vText);
    }

解决方案

well, I am Java guy, so I will not provide you the code, but rather the algorithm:

  • Yours code (I think) should check, if the element is displayed and if not, wait extra 2,5 secs
  • The reason it fails is, that sometimes it takes more than first 2,5 secs to display the element. In that case, check for if the element is displayed will throw exception

So, basically you should do some exception handling in the for loop and catch this exception and do nothing. In Java its done by try and catch block. But because I dont know C# you will have to find out how its done in this language

这篇关于的webdriver和C# - NoSuchElement例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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