IWebElement masterCard = driver.FindElement(By.XPath("//img[@title='Mastercard']")); IWebElement visa = driver.FindElement(By.XPath("//img[@title='Visa']"));
if (masterCard.Displayed) { driver.FindElement(By.XPath("//img[@title='Mastercard']")).Click(); } else if (visa.Displayed) { driver.FindElement(By.XPath("//img[@title='Visa']")).Click(); } else { Console.WriteLine("Don't have card"); }
Hi,
Hi,
There are several solution for this:
First: The FindElement throws a NoSuchElementException. You can catch this with a try/catch and continue with your code. Basically wrapping your ifs with try/catch.
Second: You can use a single xpath since your goal is to click a card regardless.
try
{
driver.FindElement(By.XPath("//img[@title='Mastercard'] | //img[@title='Visa']")).Click();
}
catch (NoSuchElementException)
{
Console.WriteLine("Don't have a card");
}
Third: You can try the Darrel approach and use the .Count() function for FindElements.
PS: It is really of no use to declare a variable and not reuse it in the if sentences. Instead of using this:
if (masterCard.Displayed)
{
driver.FindElement(By.XPath("//img[@title='Mastercard']")).Click();
}
Use:
if (masterCard.Displayed)
{
masterCard.Click();
}
//Austin
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to
webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.
I would say the warning goes both ways. Using a solution that does not wait can cause issues as well. I would rather wait those 30 seconds and get a correct fail instead of getting errors because you did not wait.
If waiting is a issue for the OP then handle that accordingly by setting timeout or use the WebdriverWait class. I also assumed the OP knew about this and did not feel the need to inform about waiting in my reply.
//Austin
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to
webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
int masterCard = driver.FindElements(By.XPath("//img[@title='Mastercard']")).Count; int visa = driver.FindElements(By.XPath("//img[@title='Visa']")).Count;
if (masterCard > 0) { Console.WriteLine("Mastercard exist"); } else if (visa > 0) { Console.WriteLine("Visa exist"); } else { Console.WriteLine("Don' have cards"); }
var masterCardElements = driver.FindElements(By.XPath("//img[@title='Mastercard']"));
var visaElements = driver.FindElements(By.XPath("//img[@title='Visa']"));
if (masterCardElements.Any() && masterCardElements[0].Displayed)
{
Console.WriteLine("Mastercard exist");
}
else if (visaElements.Any() && visaElements[0].Displayed)