0

I did the following search

parts.get(i).findElements(By.xpath("//li[starts-with(@class, '_lessons--row-')]"))

and it returned dozens of results, while I see in Developer Tools, that there are no more than 3 of them.

parts.get(i) returns single WebElement.

Looks like it searches not children of a given element, but over entire page. Can double slash cause this? What double slash means in XPath?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • `//` is short for `/descendant-or-self::node()/`. See https://www.w3.org/TR/xpath/#path-abbrev for more info. – Daniel Haley Jan 12 '17 at 23:22
  • "descendant or self" sounds contradicting with what other people saying (from root)? – Dims Jan 13 '17 at 07:38
  • Take a look at the link to the spec above. Scroll past the examples in the beginning of that section and read the last few paragraphs. It explains both `//` and `.` and how they work together. – Daniel Haley Jan 13 '17 at 09:53
  • This might also help: http://stackoverflow.com/q/33918010/317052 – Daniel Haley Jan 13 '17 at 15:50

4 Answers4

2

Your xpath is faulty here.

"//li[starts-with(@class, '_lessons--row-')]"

// searches from root level, to search from node preappend .:

".//li[starts-with(@class, '_lessons--row-')]"
Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
0

Try your xpath with .// , normally you should start xpath with "." to stop finding elements from root.

.//li[starts-with(@class, '_lessons--row-')]
promitheus
  • 67
  • 2
  • 13
0

// match relative data. which starts at the document root. In your case you are trying to locate using

//li[starts-with(@class, '_lessons--row-')]

So it will return all the match in your html. If you want to locate some specific portion of element with class have start text_lessons--row- . You have to make your xpath more specific.

e.g

 //div[@id='someid']//li[starts-with(@class, '_lessons--row-')]
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

I had a similar case, but . before // didn't help me. Just added findElements(By.xpath("your_xpath")).stream().filter(WebElement::isDisplayed).toList() as a workaround.

collder
  • 26
  • 5