g35ddd
g35ddd

Reputation: 13

Selenium - Xpath locate elements with different IDs

I am trying to locate element which may have different ID at times. Here is the example :

id = 'greenbay_packers"
id = "Sf_49ers" 

Now, is there a way do to some kind of OR operation in find_element method? so that I can use same element locator for test steps?

Also if this is not possible, is there a way to write fail safe routine that try to locate using find_element(:id,'greenbay_packaers") but if fails try find_element(:id,'sf_49ers"). And only fail test if above 2 are not found.

thanks

Upvotes: 0

Views: 196

Answers (1)

Saifur
Saifur

Reputation: 16201

It is possible with or

//*[(@id='test1') or (@id='test2')]

For second part, I would suggest you to try try..catch..finally since you have only two conditions to match

try
{
    Driver.FindElement(By.Id("ID1"));
}
catch (NoSuchElementException ex)
{
    Driver.FindElement(By.Id("ID1"));
}
finally
{
    Console.WriteLine("Failed");
}

Written in C#

Upvotes: 2

Related Questions