Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#捕获从未处理的函数返回的异常?_C#_Selenium - Fatal编程技术网

C#捕获从未处理的函数返回的异常?

C#捕获从未处理的函数返回的异常?,c#,selenium,C#,Selenium,我目前在一个循环中遇到了一个问题,当我调用- 代码示例: public static void Search() { var Lines = File.ReadLines(@"in.txt").Take(1000).ToList(); var query = string.Join(Environment.NewLine, Lines); WebDriverWait wait = new WebDriverWait(driver, T

我目前在一个循环中遇到了一个问题,当我调用-

代码示例:

public static void Search()
    {

        var Lines = File.ReadLines(@"in.txt").Take(1000).ToList();

        var query = string.Join(Environment.NewLine, Lines);

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(300));
        IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("search")));

        if (driver.FindElements(By.Name("search")).Count != 0)
        {
            driver.FindElement(By.Name("search")).SendKeys(query);

            System.Threading.Thread.Sleep(2000);

            driver.FindElement(By.Name("submitbtn")).Click();

            System.Threading.Thread.Sleep(2000);

            Console.WriteLine("Getting Results");

            if (driver.FindElements(By.CssSelector("tbody")).Count != 0)
            {
                Results();
            }
        }
    }

    public static void Results()
    {
        try
        {

            WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
            IWebElement element1 = wait1.Until(ExpectedConditions.ElementToBeClickable(By.TagName("result")));


            if (driver.FindElements(By.TagName("result")).Count != 0)
            {

                var element2 = driver.FindElements(By.CssSelector("text"));

                foreach (var text in element2)
                {
                    var data = text.GetAttribute("outerHTML");

                    System.IO.File.AppendAllText("output.txt", data + Environment.NewLine);

                }

                driver.Navigate().Refresh();

                Search();

            }
            else
            {
                Console.WriteLine("No results");

                driver.Navigate().Refresh();

                Search();
            }

        }
        catch
        {
            Console.WriteLine("Failed to get results");
            driver.Close();
            Load();
        }
    }
但返回到
Search()

第二次尝试,在结果中调用

    Search();
成功处理
结果()后

在第二个搜索过程中,Results()catch异常抛出,即

   Failed to get results

但是Results()函数未在处理中:正如我调用Search()一样,因此我的结论是我从未从结果中退出,它仍在处理中,在30秒未找到元素后,它会抛出异常,但如何再次调用Search()进行第二次尝试&exit Results()?

我认为这两句话引发了一个例外:

WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
IWebElement element1 = wait1.Until(ExpectedConditions.ElementToBeClickable(By.TagName("result")));
从我的角度来看,您不需要它们,因为您已经:

if (driver.FindElements(By.TagName("result")).Count != 0)
如果您不确定结果是否会很快出现在
DOM
中,您可以添加暂停或环绕
try/catch
块,如下所示:

try {
    WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
    IWebElement element1 = wait1.Until(ExpectedConditions.ElementToBeClickable(By.TagName("result")));
}catch {
    Console.WriteLine("No results");
}
if (driver.FindElements(By.TagName("result")).Count != 0)

PS:我不确定您是否需要
ExpectedConditions.element可单击,因为您没有单击该元素。您可以使用
ExpectedConditions.ElementToVisible
例如。

是的,我确实假设wait1.until元素存在时引发异常,但在我继续处理其余请求之前,该元素必须是可单击/可见的。代码,它在我第一次调用该函数时工作正常,但当我返回Search()时;显然,元素“result”不再可见/可点击,因此在30秒后,它会抛出一个异常。。但当我回到搜索()时,我想;根据结果();它将退出results();?所以解决方案是,我假设是成功地结束results函数,然后返回到search,但是我该怎么做呢?但是我建议你返回search(),问题是即使我返回search(),results()仍然会查找元素结果,如果它不退出,那么看起来其他人也有类似的问题,似乎selenium/c#在完成“SendKeys”字符串之前执行函数中的下一个进程..:/在继续下一个任务之前,还有很多关于sendkeys没有找到输入的问题