Getting text out of range in Selenium

I am having trouble getting a list of text using selenium 2.46.0. I want to return every text value in between.

HTML code:

<p id="wordList">
  <span>
      owl
  </span>
  <span>
      end
  </span>
</p>

      

Note that span tags do not have a class or id to link.

My code:

List<WebElement> spans = driver.findElements(By.tagName("span"));
    for (WebElement span : spans) {
        System.out.println(span.getText());
    }

      

It doesn't return any text. In fact, it doesn't even acknowledge that span tags exist. I've also tried different options (XPath, CSSSelector, etc.), but nothing seems to work. Any help would be appreciated.

+3


source to share


1 answer


I would try to find the gaps from the parent node using the following css

#wordList>span

      

Implementation:



List<WebElement> spans = driver.findElements(By.cssSelector("#wordList>span"));
for (WebElement span : spans) {
     System.out.println(span.getText());
}

      

Using tagName and span is not very convenient for finding an element from a web page as it can contain a lot.

+1


source







All Articles