2

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?

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

4 Answers 4

1

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

0

You can use try cache block.

String text = null;
try{
   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.

5
  • 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. Commented Mar 14, 2017 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
    Commented Mar 16, 2017 at 8:19
  • @Ravi Kindly select the best answer.
    – Mayur
    Commented Mar 16, 2017 at 8:20
  • @mayur that makes no sense. the real code wold use the exception approach, correct? Commented Mar 16, 2017 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
    Commented Mar 17, 2017 at 6:40
0

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

if(driver.findElement(By.xpath(xpath1)).isdisplayed){
return driver.findElement(By.xpath(xpath1)).getText();
}
else
return driver.findElement(By.xpath(xpath2)).getText();
0

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!
  code...

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.

3
  • driver.findElement(By.xpath(xpath1)) is null, it will throw exception and execution will not proceed.
    – Mayur
    Commented Mar 16, 2017 at 8:21
  • Yes, updated for both queries to use findElements instead of findElement Commented Mar 17, 2017 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
    Commented Mar 20, 2017 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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