如何防止C#和Selenium退出代码0

问题描述

while (1 == 1)
{
    int questions = 0;
    Thread.Sleep(500);
    
    try
    {
        driver.FindElement(By.XPath("//*[@id='root']/div/main/div[2]/div/div[1]/button[1]"));

        questions++;
    }
    catch (Exception e)
    {
        return;
    }
    
    try
    {
        driver.FindElement(By.XPath("//*[@id='root']/div/main/div[2]/div/div[1]/button[2]"));

        questions++;
    }
    catch (Exception e)
    {
        return;
    }
    
    try
    {
        driver.FindElement(By.XPath("//*[@id='root']/div/main/div[2]/div/div[1]/button[3]"));

        questions++;
    }
    catch (Exception e)
    {
        return;
    }
    
    try
    {
        driver.FindElement(By.XPath("//*[@id='root']/div/main/div[2]/div/div[1]/button[4]"));

        questions++;
    }
    catch (Exception e)
    {
        return;
    }

    if (questions == 0)
    {
        return;
    }
    else if (questions == 1)
    {
        MessageBox.Show("1","1");
        driver.FindElement(By.XPath("//*[@id='root']/div/main/div[2]/div/div[1]/button[1]")).Click();
    }
    else if (questions == 2)
    {
        MessageBox.Show("2","2");
        String[] av2 = { "//*[@id='root']/div/main/div[2]/div/div[1]/button[1]","//*[@id='root']/div/main/div[2]/div/div[1]/button[2]"};
        Random random = new Random();
        int a = random.Next(av2.Length);
        driver.FindElement(By.XPath(av2[a])).Click();
    }
    else if (questions == 3)
    {
        MessageBox.Show("3","3");
        String[] av3 =
        {
            "//*[@id='root']/div/main/div[2]/div/div[1]/button[1]","//*[@id='root']/div/main/div[2]/div/div[1]/button[2]","//*[@id='root']/div/main/div[2]/div/div[1]/button[3]"
        };
        Random random = new Random();
        int a = random.Next(av3.Length);
        driver.FindElement(By.XPath(av3[a])).Click();
    }
    else if (questions == 4)
    {
        MessageBox.Show("4","4");
        String[] av4 =
        {
            "//*[@id='root']/div/main/div[2]/div/div[1]/button[1]","//*[@id='root']/div/main/div[2]/div/div[1]/button[3]","//*[@id='root']/div/main/div[2]/div/div[1]/button[4]"
        };
        Random random = new Random();
        int a = random.Next(av4.Length);
        driver.FindElement(By.XPath(av4[a])).Click();
    }
    
    Console.WriteLine(questions + " <");
}

我一点儿都没有经验,也不知道为什么该类一直以代码0退出。我已经尝试了多种方法,但是都没有用。因此,我想做的是每0.5秒用这段C#代码检查带有硒的xpath。但目前,它以退出代码0退出。我该如何解决

解决方法

return语句(如果放在Main函数中)将停止程序的执行。将代码移到函数中,并添加一些逻辑以防止控制台应用程序关闭

using System;

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            DoYourThings();

            Console.WriteLine("Press any key to close application...");
            Console.ReadKey(); // application hangs here until user clicks any button,// so you have time to examine console window
        }

        static void DoYourThings()
        {
            while (true)
            {
                try 
                {
                    // some code
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message); // output exception message
                    return; // exits DoYourThings function
                }
            }
        }
    }
}
,

似乎您想从多个XPath中选择一个随机项(如果找到)并单击它。问题在于,您正在调用return;,而不是忽略异常(不执行任何操作),这将退出方法。

相反,您可以仅从return块内部删除catch语句,这样就可以解决您的问题。

此外,如果我们将一些结果存储在列表中,然后循环遍历这些列表(并从列表中选择一个随机元素),您似乎还有很多重复的代码可以简化。另外,您只需初始化一次Random的实例。

例如:

// Only initialize random one time
var random = new Random();

// Store our XPaths in a list so we can loop over them
var buttonXPaths = new List<string>
{
    $"//*[@id='root']/div/main/div[2]/div/div[1]/button[1]",$"//*[@id='root']/div/main/div[2]/div/div[1]/button[2]",$"//*[@id='root']/div/main/div[2]/div/div[1]/button[3]",$"//*[@id='root']/div/main/div[2]/div/div[1]/button[4]",};
        
// Endless loop
while (true)
{
    // Sleep for half a second
    Thread.Sleep(500);

    // Create a list to hold any WebElements we find
    var elements = new List<WebElement>();

    // For each XPath,try to find the element and add it to our list
    foreach (var buttonXPath in buttonXPaths)
    {
        try
        {
            elements.Add(driver.FindElement(buttonXPath));
        }
        catch
        {
            // Ignore any exceptions. The next line isn't necessary,but I
            // included it so you can see the syntax for explicitly skipping 
            // a loop iteration and starting it over again (you were using 'return')
            continue;
        }
    }

    // If we found any elements. This is the same as: if (elements.Count > 0)
    if (elements.Any())
    {
        // Display the number of elements found in a message box
        MessageBox.Show(elements.Count.ToString(),elements.Count.ToString());

        // Choose a random element from our list
        WebElement randomElement = elements[random.Next(elements.Count)];

        // Click it
        randomElement.Click();

        // This was in your code,not sure why
        Console.WriteLine($"{elements.Count} <");
    }
}