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

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.