0

There are 52 page objects as follows, all of which has Strong tag containing a String called "Gideon". How possibly can I get all of them by findElements method? enter image description here

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

1 Answers1

0

The "quotes" are within a text nodes. So to retrieve the text you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • xpath:

    List<WebElement> parentElements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//strong[text()='Gideon']//..")));
    for (WebElement parentElement:parentElements)
        System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", parentElement).toString());
    
  • xpath with preceding:

    List<WebElement> parentElements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//strong[text()='Gideon']//preceding::p[1]")));
    for (WebElement parentElement:parentElements)
        System.out.println(((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", parentElement).toString());
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217