0

Please find the HTML text

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

<a id="id_10808296" class="activity"> 
<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

Prophet
  • 32,350
  • 22
  • 54
  • 79
nayansFosgit
  • 69
  • 1
  • 7

2 Answers2

1

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

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • thanks it worked but i have encountered another problem on getting values from strong tag, have raised a new case kindly please look into it thanks, https://stackoverflow.com/questions/75153762/how-to-get-text-value-from-strong-tag – nayansFosgit Jan 18 '23 at 01:01
1

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]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352