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')]]]"
))
请尝试遵循XPath
并让我知道是否发生任何错误:
//*[contains(text(), "Chocolate")][not(contains(text(), "Dark"))]
Here is the syntax with using just one set of square brackets.
For elements containing the word 'Chocolate' but not 'Dark', use:
//*[contains(text(), 'Chocolate') and not(contains(text(), 'Dark'))]
For just the text of the elements, use:
//text()[contains(., 'Chocolate') and not(contains(., 'Dark'))]
Given the following XML:
<doc>
<e>Dark Chocolate</e>
<e>Chocolate</e>
</doc>
The first expression results in:
> xpath -e "//*[contains(text(), 'Chocolate') and not(contains(text(), 'Dark'))]" test.xml
<e>Chocolate</e>
The second expression results in:
> xpath -e "//text()[contains(., 'Chocolate') and not(contains(., 'Dark'))]" test.xml
Chocolate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.