I need to have two variables in one sentence, but when I have two it wrote error message: is not a valid XPath expression

Viewed 25

When I have 1 variable it works, but I need 2 and then it wrote error.

product = "Air Force 1 Low"
color = "White"

findJS = (
"""
const $xOne = xp =>document.evaluate(xp, document, null,XPathResult.FIRST_ORDERED_NODE_TYPE, 
null).singleNodeValue;
($xOne("//*[contains(text(), """+product+""")]/../following-sibling::p/a[contains(text(), 
"""+color+""")]")).click();
"""
)
1 Answers

The second argument to contains() should be quoted.

It's easier to write something like this using an f-string rather than concatenation.

findJS = f"""
const $xOne = xp =>document.evaluate(xp, document, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
$xOne("//*[contains(text(), '{product}')]/../following-sibling::p/a[contains(text(), '{color}')]").click();
"""
Related