Reputation: 91
I have made automated test with selenium c# and have a probelm. My test writes some info in form and then submits, if after submiting div that contains some info has info "Formoje yra klaidu", it must write to file email from form, but the problem is that this div is not visible when email isn't wrong and my test just stops on place where Iwebelement finds element by xpath because the element isn't visible. Here's some of the code
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++;
}
How to make that if this element isn't there, code don't stop. Can any body help me ???
Upvotes: 0
Views: 629
Reputation: 478
Use an Implicit Wait. This allows you to enter a value in seconds that webdriver will wait for an element if it isn't found initially. This example is set for 2 seconds.
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2)
You could also use a try{} catch{}.
Also if you want to clean up your code you could write functions for finding elements and then just pass that id name into the function. It will make things a lot clearer and easier to read.
Here is my method for finding an element by 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);
}
Upvotes: 0
Reputation: 1
Be carefull with FindElements, the test can be very long to execute if you have huge pages. When I must use the FindElements to search an element, I use a FindElement that can help me to scope where I must find the researched element with FindElements. In my case, my executing time is reduced of 2 seconds everytime I use directly FindElements
Upvotes: 0
Reputation: 91
I did it like this and it worked
if (driver.FindElements(By.XPath("//*[@id='step_2']/div[3]")).Count != 0)
Upvotes: 0
Reputation: 1269
Well, first of all don't use any Thread.Sleeps at all. Use Implicit and Explicit waits instead. Secondly, try to not use xpath (very difficult to maintain, understand it). And if you need to verify elements existance you can do it in next way e.g.
var elements = driver.FindElements(By.XPath("//*[@id='step_2']/div[3]"));
if(elements.Count() > 0)
// do everything you want
else
//continue doing smth
or you can try catch ElementNotFound exception... it's all depends.
Upvotes: 2
Reputation: 1021
You should check to see if the element exists, in this case check and see if the size of the element is greater than 0. This is how I could do it in 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
}
Upvotes: 0