mercredi 13 juillet 2016

XPath: Find element that contains text X but not Y


I'm attempting to find a web element using XPath in Selenium for Python. The web element should contain the text "Chocolate" but not include "Dark".

I've tried this syntax but not gotten it to work (be mindful of parantheses):

choco = driver.find_element_by_xpath("//*[text()[contains(., 'Chocolate')] and not[[contains(., 'Dark')]]]")

Here is the same code using line breaks and concatenation for readability:

choco = driver.find_element_by_xpath((
                                    "//*[text()[contains(., 'Chocolate')]" + 
                                    "and not[[contains(., 'Dark')]]]"
                                    ))

Aucun commentaire:

Enregistrer un commentaire