首页 文章

Selenium c#自动化测试

提问于
浏览
0

我用硒c#进行了自动化测试,并且有一个问题 . 我的测试在表单中写了一些信息,然后提交,如果在提交包含一些信息的div后有信息“Formoje yra klaidu”,它必须从表单写入文件电子邮件,但问题是当电子邮件不是时,这个div是不可见的错了,我的测试只是在Iwebelement通过xpath找到元素的地方停止,因为该元素不可见 . 这是一些代码

for (int i = 0; i < array.Length; i++)
        {

        IWebElement PasirinktiParkinga = driver.FindElement(By.CssSelector("#zone_16 > td:nth-child(5) > a:nth-child(1)"));
        PasirinktiParkinga.Click();

        IWebElement Vardas = driver.FindElement(By.Id("firstname1"));
        Vardas.Clear();
        Vardas.SendKeys("Vardas");

        IWebElement Pavarde = driver.FindElement(By.Id("lastname1"));
        Pavarde.Clear();
        Pavarde.SendKeys("Pavarde");

        IWebElement AutoNumeris = driver.FindElement(By.Id("vehicle_number1"));
        AutoNumeris.Clear();
        AutoNumeris.SendKeys("ASD123");

        IWebElement Pastas = driver.FindElement(By.Id("email1"));
        Pastas.Clear();
        Pastas.SendKeys(array[i]);

        IWebElement Taisykles = driver.FindElement(By.CssSelector("div.checks:nth-child(5) > div:nth-child(1) > label:nth-child(2)"));
        Taisykles.Click();

        IWebElement uzsakyti = driver.FindElement(By.CssSelector(".submit-zone > input:nth-child(1)"));
        uzsakyti.Click();

        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));


            IWebElement MessageRed = driver.FindElement(By.XPath("//*[@id='step_2']/div[3]")); //This line  is were i wan't to find this div but i must write it so that if there isn't there - just do the for cicle 
            if (MessageRed.Text.Contains("Formoje yra klaidų."))
            {
                failure += array[i] + "\n";

                System.IO.File.WriteAllText(@"C:\Users\jarek\Desktop\Failureemail\failure.txt", failure);
            }




        IWebElement unipark = driver.FindElement(By.CssSelector(".logo > a:nth-child(1)"));
        unipark.Click();

        i++;
        }

如果这个元素不存在,如何使代码不停止 . 有谁能够帮我 ???

5 回答

  • 0

    好吧,首先不要使用任何Thread.Sleeps . 请改用隐式和显式等待 . 其次,尽量不要使用xpath(很难维护,理解它) . 如果您需要验证存在的元素,您可以通过下一步方式进行验证,例如

    var elements = driver.FindElements(By.XPath("//*[@id='step_2']/div[3]"));
       if(elements.Count() > 0)
          //   do everything you want
       else
          //continue doing smth
    

    或者您可以尝试捕获ElementNotFound异常......这完全取决于 .

  • 0

    小心FindElements,如果你有大页面,测试可能会很长 . 当我必须使用FindElements搜索元素时,我使用FindElement来帮助我找到我必须在FindElements中找到研究元素的范围 . 就我而言,每次直接使用FindElements时,我的执行时间减少了2秒

  • 2

    您应该检查元素是否存在,在这种情况下检查并查看元素的大小是否大于0.这是我在Java中的方法:

    if (driver.FindElement(By.XPath("//*[@id='step_2']/div[3]")).size() > 0)
    {
       //perform your action now
    }
    
    else
    {
      //perform action if the element is not present
    }
    
  • 0

    我是这样做的,它有效

    if (driver.FindElements(By.XPath("//*[@id='step_2']/div[3]")).Count != 0)
    
  • 0

    使用隐式等待 . 这允许您输入webdriver在最初未找到时等待元素的值(以秒为单位) . 此示例设置为2秒 .

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2)
    

    你也可以使用try {} catch {} .

    此外,如果要清理代码,可以编写查找元素的函数,然后将该id名称传递给函数 . 它将使事情更清晰,更容易阅读 .

    这是我通过ID查找元素的方法

    static void ClickElement_ByID(string elementName)
        {
            try
            {
                IWebElement test = driver.FindElement(By.Id(""+elementName+""));
                Console.WriteLine("Found: "+elementName);
                test.Click();
            }
            catch (Exception e)
    
            {
                Console.WriteLine(e);
            }
    

相关问题