My challenge is to locate an element by its content which contains single-quotes. I do this successfully with vanilla JS here:
singleQuotes = document.evaluate("//div[contains(text(), \"'quotes'\")]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
singleQuotes = singleQuotes.snapshotItem(0);
console.log(singleQuotes)
<div>'quotes'</div>
However, when I use Python & Selenium to implement the same vanilla JS, the Xpath is invalid:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
window = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
window.get("www.someurl.com")
window.execute_script(f'''xpath = document.evaluate("//div[contains(text(), \\"'quotes'\\")]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);''')
I get a SyntaxError: Failed to execute 'evaluate' on 'Document': The string...is not a valid XPath expression.
(I would try & show this in a sandbox but I don't know how to).