0

getText(), getAttribute("textContent") is not returning correct result.

This is the html tag with tag:

<span>
class="panel-title text-primary header-font ng-binding" ng-click="titleLink()" style="" xpath="1">Applied Configs: TEST, MSA TEST, MSACONFIGURABLE
</span>

Code trials:

driver.findElement(By.xpath("//*[contains(@name,'Applied Configs')]/div[1]")).getAttribute("textContent"));

or

driver.findElement(By.xpath("//*[contains(@name,'Applied Configs')]/div[1]")).getText();

Returning Applied Configs: instead of complete Text Applied Configs: TEST, MSA TEST, MSACONFIGURABLE.

I am not sure what mistake I am doing here.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

1 Answers1

0

To extract the text Applied Configs: TEST, MSA TEST, MSACONFIGURABLE as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using xpath and getText():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')]"))).getText());
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')]"))).getAttribute("innerHTML"));
    

Update

As an alternative you can induce WebDriverWait for TextToBePresentInElementLocated():

new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')][contains(., 'Applied Configs:')]"), ","))
System.out.println(driver.findElement(By.xpath("//span[@class='panel-title text-primary header-font ng-binding' and starts-with(@ng-click, 'titleLink')][contains(., 'Applied Configs:')]")).getAttribute("innerHTML"));
DebanjanB
  • 118,661
  • 30
  • 168
  • 217