I have a lxml etree HTMLParser object that I\'m trying to build xpaths with to assert xpaths, attributes of the xpath and text of that tag. I ran into a problem when the te
The solution is applicable If u r using python lxml
.
Its better to leave the escaping for lxml
. We can do this by using lxml
variables.
Suppose We have xpath
as below:
//tagname[text='some_text']`
If some_text
has both single and double quotes, then it causes "Invalid Predicate error"
.
Neither escaping work for me nor triple quotes. Because xml won't accept triple quotes.
Solution worked for me is lxml
variables.
We convert the xpath as below:
//tagname[text = $var]
Then execute
find = etree.XPath(xpath)
Then evaluate these variable to its value
elements = find(root, {'var': text})