2015-03-31 136 views
0

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

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++; 
     } 

如何使这个元素不存在,代码不会停止。 任何机构都可以帮助我?

回答

0

你应该检查,看看是否元素存在,在这种情况下,检查,看看是否该元素的大小大于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

找到一个元素的方法,我不知道为什么,但是这不是为我工作 – 2015-04-01 08:24:22

2

好,首先不要使用任何Thread.Sleeps。改用Implicit和Explicit等待。其次,尽量不要使用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

我做到了这样,它的工作

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

要当心与FindElements,测试可能会很长,如果你有巨大的网页来执行。 当我必须使用FindElements来搜索一个元素时,我使用了一个FindElement,它可以帮助我确定在哪里我必须用FindElements找到所研究的元素。就我而言,每次我直接使用时,执行时间会缩短2秒FindElements

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); 
     }