2015-03-31 146 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); 
     }