Skip to content

element>element selector does not work relative to an element #86

@frisi

Description

@frisi

in version 1.0.3 i get an exception when using cssselect on an element to select it's direct children
element > element (see https://www.w3schools.com/cssref/sel_element_gt.asp)

>>> from lxml import html
>>> html.fromstring('<html><body><div class="parent"><div class="child"><div class="child"></div></div></div></body></html>')
<Element html at 0x7feadf137d08>
>>> tree=html.fromstring('<html><body><div class="parent"><div class="child"><div class="child"></div></div></div></body></html>')
>>> tree.cssselect('div.parent')
[<Element div at 0x7feadf137e10>]
>>> tree.cssselect('div.parent')[0].cssselect('> .child')
*** SelectorSyntaxError: Expected selector, got <DELIM '>' at 0>

in version 0.9.1 the following worked w/o raising an exception, however it leads to an unexpected result since the second div.child is no direct child of div.parent

>>> tree=html.fromstring('<html><body><div class="parent"><div class="child"><div class="child"></div></div></div></body></html>')
# works but should return only one element
>>> tree.cssselect('div.parent')[0].cssselect('> .child')
[<Element div at 0x7fa6e973def0>, <Element div at 0x7fa6e973dfb0>]

> only works when parent selector is given in the selector

>>> tree.cssselect('div.parent > .child')
[<Element div at 0x7fa6e973de90>]

A) is it a regression, that element.cssselect('> .child') raises an exception on recent versions?

B) is there a way to select a direct child given the parent element?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions