1

I'm trying to convert the following element:

@[width="300"]

That I convert to xpath as:

//*[@width="300"]

To a css selector. Because with lxml if I run:

selector = "@[width="300"]"
tree = lxml.html.fromstring(fparse(fileIn).to_string())
tree.cssselect(selector)

I get the following error:

Expected selector, got <DELIM '@' at 0>

How can I convert it without using external libraries not included in lxml itself? I need to use cssselect and not xpath because I have other cases where my program need to parse selectors like the following one:

@[width="300"] > p > a
J0ker98
  • 447
  • 5
  • 18

1 Answers1

1

You don't need the @ for attribute definition in a CSS selector:

[width="300"]

Double quotes are actually extra for this particular value (as long as an attribute value is alphanumeric):

[width=300]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195