0

While trying to print values present with in strong tag using xpath I'm getting 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 strong tag :

 <a id="id_1099808326" class="activity"> 
    <strong>heizil</strong> : 
    <label id="check_label">  " First Device Test"
    </label>
    
    <a id="id_1099808296" class="activity"> 
    <strong>riya</strong>: 
    <label id=check_label"> " Second Device Test"
    </label>

Expected output: heizil,riya

If this is not the proper way can any one suggest any other way of getting the values present in <strong> tag

nayansFosgit
  • 69
  • 1
  • 7

1 Answers1

0

As per the given HTML text heizil is within <strong> tag which is the immediate descendant of the <a> tag.

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

Solution

To print the text heizil you can use either of the following locator strategies:

  • Using cssSelector and getAttribute("innerHTML"):

    System.out.println(driver.findElement(By.cssSelector("a.activity > strong")).getAttribute("innerHTML"));
    
  • Using xpath and getText():

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

Ideally, to extract the text value, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following locator strategies:

  • Using cssSelector and getText():

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.activity > strong"))).getText());
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']"))).getAttribute("innerHTML"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thanks it really worked, had one more query on the same How can I locate an web element and fetch them when I have multiple elements sharing the same class name – nayansFosgit Jan 15 '23 at 16:55
  • Like mentioned I need to fetch two names (Heizil and Riya), as I'm having same class sharing multiple element I'm only able to get one name – nayansFosgit Jan 15 '23 at 17:10
  • @nayansFosgit Sounds to be a different question all together. Feel free to raise a new question with all the relevant details. Will be happy to look in to the usecase. – undetected Selenium Jan 15 '23 at 17:13
  • have raised a new case kindly please look into it, thanks! https://stackoverflow.com/questions/75127045/how-to-fetch-multiple-text-values-present-within-strong-tag-when-there-is-multip – nayansFosgit Jan 15 '23 at 17:35
  • 1
    Thanks for the all the above help, it worked thanks! – nayansFosgit Jan 15 '23 at 19:50