Skip to content Skip to sidebar Skip to footer

How To Deal With Single Quote In Xpath

I have a line where I check if a certain element by partial text exists on the page. self.b.find_element_by_xpath('.//*[contains(text(), '%s')]' % item_text) So it is possible tha

Solution 1:

try as below :-

self.b.find_element_by_xpath(".//*[contains(text(), \"Hanes Men's Graphic\")]")

or

self.b.find_element_by_xpath('.//*[contains(text(), "%s")]' % item_text)

or

self.b.find_element_by_xpath(".//*[contains(text(), \"%s\")]"% item_text)

Hope it will work..:)

Solution 2:

It might be that the xpath implementation won't allow double quotes for the string so you'll need to do something like

if (item_text.find("'")):
    item_text= item_text.replace("'", "', \"'\", '")
    item_text="concat('"+ item_text+"')"else:
    item_text ="'"+ item_text +"'"self.b.find_element_by_xpath(".//*[contains(text(), %s)]"% item_text)

Post a Comment for "How To Deal With Single Quote In Xpath"