Home > Back-end >  How to fetch multiple text values present within strong tag when there is multiple elements sharing
How to fetch multiple text values present within strong tag when there is multiple elements sharing

Time:01-16

Please find the HTML text

<a id="id_1008326" > 
<strong>heizil</strong> : 
<label id="check_label">  " First Device Test"
</label>

<a id="id_10808296" > 
<strong>riya</strong>: 
<label id="check_label"> " Second Device Test"
</label>

I'm unable to fetch both the values present in strong tag as both of them are sharing same class, can we apply some conditions based on text in label tag (" First Device Test" / " Second Device Test" ) as ' label id=check_label ' is same and both id_ (id="id_1008326" , id="id_10808296" )is not constant it varies repeatedly.

using the below code line

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

I'm only able to fetch heizil

Expecyed Output : heizil,riya

CodePudding user response:

You can fetch both strong tags text contents fetching all these web elements and then iterating over the web elements extracting their texts, as following:

List<WebElement> strongs = driver.findElements(By.xpath("//a[@class='activity']/strong"))
for(WebElement element : strongs){
    System.out.println(element..getText());
}

You can also fetch specific strong node text content based on parent label element text.
F.e. if you want to get strong text content where label text is "Second Device Test" you can do this:

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

Here I'm locating the parent a based both on @class='activity' and some child node text content Second Device Test

CodePudding user response:

To print the texts heizil and riya you can use Java8's stream() and map() and you can use either of the following locator strategies:

  • Using cssSelector and getText():

    System.out.println(driver.findElements(By.cssSelector("a.activity > strong")).stream().map(element->element.getText()).collect(Collectors.toList()));
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(driver.findElements(By.xpath("//a[@class='activity']/strong")).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
    
  • Console Output:

    [heizil, riya]
    

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

  • Using cssSelector and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("a.activity > strong"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));
    
  • Using xpath and getText():

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    
  • Console Output:

    [heizil, riya]
    
  • Related