Search code examples
javaseleniumxpathcss-selectorswebdriverwait

How can I find the exact text in browser automation using Selenium with Java?


<div data-ng-if="!newRequestReceived" class="ng-scope">
                <!-- ngIf: !authRequestCount --><p data-ng-if="!authRequestCount" class="ng-scope" style="" xpath="1">No more candidates awaiting Authorization</p><!-- end ngIf: !authRequestCount -->
                <!-- ngIf: authRequestCount -->
            </div>

I need help,

if find string like "No more candidates awaiting Authorization"

the loop will working How???

driver.findElement(By.xpath("//p[@class='ng-scope'][contains('No more candidates awaiting Authorization']"));

this is correct


Solution

  • To extract the string No more candidates awaiting Authorization you don't need a loop as such. As the element is an Angular element you need to induce WebDriverWait and you can use either of the following solutions:

    • CssSelector:

      String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ng-scope[data-ng-if*='newRequestReceived'] p.ng-scope[data-ng-if*='authRequestCount']"))).getAttribute("innerHTML");
      
    • XPath:

      String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");