In a Web page having the following xpaths.

xpath1 = By.xpath(".//*[@id='gamepackage-game-information']/article/div/div[2]/span")).getText();

xpath2 = By.xpath(".//*[@id='gamepackage-game-information']/article/div/div[1]/figure/figcaption/div")).getText();

Some webpages having xpath1 & Some webpages having xpath2

How to get the text using if condition in Selenium Webdriver?

  • Would you please, share the HTML that represent the 2 controls and a sample text to search for. – A.A.A Jul 16 '17 at 4:33

In xpath creation, we can use logical OR condition.

xpath1 = By.xpath(".//*[@id='gamepackage-game-information']/article/div/div[2]/span" | .//*[@id='gamepackage-game-information']/article/div/div[1]/figure/figcaption/div)).getText();

I hope it solves your problem

| improve this answer | |

You can use try cache block.

String text = null;
   text = driver.findElement(By.xpath(xpath1)).getText();
}catch(NoSuchElementException ex){
   text = driver.findElement(By.xpath(xpath2)).getText();

Hope that helps. P.S: This is just a pseudo code.

| improve this answer | |
  • 3
    That would catch ALL exceptions now and in the future and could lead to very hard to diagnose bugs due to other exceptions being caught by it. – Michael Durrant Mar 14 '17 at 20:07
  • @MichaelDurrant I understand that part. Its just a pseudo code, so its upto the user to fill in what exception to be used. – Mayur Mar 16 '17 at 8:19
  • @Ravi Kindly select the best answer. – Mayur Mar 16 '17 at 8:20
  • @mayur that makes no sense. the real code wold use the exception approach, correct? – Michael Durrant Mar 16 '17 at 15:00
  • @MichaelDurrant Yes actual code will use the exception approach. I get what you are saying. Thank you for pointing it out. – Mayur Mar 17 '17 at 6:40

In our software testing company we encountered a similar situation and below worked for us.

return driver.findElement(By.xpath(xpath1)).getText();
return driver.findElement(By.xpath(xpath2)).getText();
| improve this answer | |

I would use something like

# pseudo code
String text = null;
text1 = driver.findElements(By.xpath(xpath1)).getText();
text2 = driver.findElements(By.xpath(xpath2)).getText();
if text1 <> '' or text2 <> '' then  # NOTE: I don't know java syntax!

You might have to use findElements (note plural) and check for the array size being zero (that is if findElement raises an error). findElements tends to get around that by just returning an empty array.

| improve this answer | |
  • driver.findElement(By.xpath(xpath1)) is null, it will throw exception and execution will not proceed. – Mayur Mar 16 '17 at 8:21
  • Yes, updated for both queries to use findElements instead of findElement – Michael Durrant Mar 17 '17 at 22:24
  • When you use findElements, it will return list of elements. So, you will have to use index. Example, in Java driver.findElements(By.xpath(xpath2)).get(0).getText() – Mayur Mar 20 '17 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.