1

While trying to print values present within the strong tag using XPath, I'm getting the following exception:

org.openqa.selenium.NoSuchElementException: Unable to locate element: strong

This is my code:

   WebElement eleText = driver.findElement(By.xpath("//strong"));
    
    String testerName = eleText.getText();
    System.out.println(testerName);

This is my webpage which I'm trying to get values within the strong tag:

<a id="id_109996" class="activity"> 
<strong>heizil</strong>: <label id="sample_label">

Expected output: heizil

If this is not the proper way, can anyone suggest any other way of getting the values present in the strong tag?

2
  • What is happening after : System.out.println(driver.findElement(By.tagName("strong")).getText()); Jan 16 at 17:13
  • 1
    thanks it worked!! Jan 19 at 0:49

3 Answers 3

0

There are multiple workarounds to handle such scenarios while doing web automation using Selenium. While working in outsourced software testing company and developing frameworks in multiple languages below is the most widely used approach -

WebElement eleText = driver.findElement(By.xpath("//a[@class='activity']/strong")).getText()
String testerName = eleText.getText();
System.out.println(testerName);
1
1

Solution :

  • I have tried accessing the element and get text successfully using

System.out.println(driver.findElement(By.tagName("strong")).getText());

  • Found working as expected
  • Suggestion : Use the xpath as last option and choice. Its not most reliable strategy
0
0

This error occurs because your xpath selector is too general, it is trying to locate any tag on the page.

A more specific xpath selector can be used to locate the text "heizil":

WebElement eleText = driver.findElement(By.xpath("//a[@id='id_109996']/strong"));
String testerName = eleText.getText();
System.out.println(testerName);

This xpath selector finds the <strong> tag that is a direct child of the tag with the id "id_109996".

Your Answer

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

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