CSS selectors specify patterns to match elements in a tree.
This table shows some examples showing how to extract different HTML elements from an
HTML tree:
Task | CSS Selector | Example |
---|
Find all paragraph (<p> ) elements. | "p" | findElement(tree,"p") |
Find all paragraph (<p> ) and list item
(<li> ) elements. | "p,li" | findElement(tree,"p,li") |
Find all paragraph (<p> ) elements that are inside table
(<table> ) elements. | "table p" | findElement(tree,"table p") |
Find all hyperlink (<a> ) elements with hyperlink
reference attribute (href ) values ending with
".pdf" . | "a[href$="".pdf""]" | findElement(tree,"a[href$="".pdf""]") |
Find all paragraph (<p> ) elements that are the first
child of their parent. | "p:first-child" | findElement(tr,"p:first-child") |
Find all paragraph (<p> ) elements that are the first
paragraph element of their parent. | "p:first-of-type" | findElement(tr,"p:first-of-type") |
Find all emphasis (<em> ) elements where the parent is a
paragraph (<p> ) element. | "p > em" | findElement(tr,"p > em") |
Find all paragraph (<p> ) elements appearing immediately
after a heading 1 (<h1> ) element | "h1 + p" | findElement(tr,"h1 + p") |
Find all empty elements. | ":empty" | findElement(tr,":empty") |
Find all nonempty label (<label> ) elements. | "label:not(:empty)" | findElement(tr,"label:not(:empty)") |
The findElement
function supports all of CSS level 3, except for
the selectors ":lang"
, ":checked"
,
":link"
, ":active"
, ":hover"
,
":focus"
, ":target"
, ":enabled"
,
and ":disabled"
.
For more information about CSS selectors, see [1].