4

I have a query for a one of my tests that returns 2 results. Specifically the 3rd level of an outline found using

query = html("ul ol ul")

How do I select the first or second unordered list?

query[0]

decays to a HTMLElement

list(query.items())[0]

or

query.items().next() #(in case of the first element)

is there any better way that I can't see?

note:

query = html("ul ol ul :first")

gets the first element of each list not the first list.

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141

2 Answers2

6

From the PyQuery documentation on traversing, you should be able to select the first unordered list by using:

query('ul').eq(0)

Thus the second unordered list can be obtained by using:

query('ul').eq(1)
Talvalin
  • 7,789
  • 2
  • 30
  • 40
2

In jQuery one would use

html("ul ol ul").first()

.first() - Reduce the set of matched elements to the first in the set.

or

html("ul ol ul").eq(0)

.eq() - Reduce the set of matched elements to the one at the specified index.

CodeManX
  • 11,159
  • 5
  • 49
  • 70