Nina
Nina

Reputation: 160

selenium c# to find if one out of two elements is present

i am writing a test to find element A or element B. I am writing these lines, not sure how do i structure it, as i want to use OR

Assert.IsTrue((WebDriver.FindElement(ByXpath("elementA")))||(WebDriver.FindElement(ByXpath("elementB"))));

I am also thinking of using if...elseif, but there is no action, I just want the test presence of one or other.

if (IsElementPresent(ByXpath("elementA")))
            {
                //what to do here?
            }
            else if (IsElementPresent(ByXpath("elementB")))
            {
                //????
            }

Upvotes: 0

Views: 233

Answers (2)

Dazed
Dazed

Reputation: 1551

You could do something like this also...

bool ele = Driver.IsElementPresent(By.Xpath("path"));
bool ele2 = Driver.IsElementPresent(By.Xpath("path"));

Then you could assert this way as an or operator

Assert.IsTrue(ele | ele2);

Upvotes: 1

OrdinaryKing
OrdinaryKing

Reputation: 461

You could add it in a try catch

try
{
assert.IsTrue(<YourElementA>);
}
catch(Exception)
{
assert.IsTrue(<YourElementb>);
}

Upvotes: 1

Related Questions