title: XPath: Find element that contains text X and not Y
body: <p>I'm attempting to find a web element using XPath in <code>Selenium</code> for Python. The web element should contain the text <em>"Chocolate"</em> but not include <em>"Dark"</em>.</p>
  
  <p>I've tried this syntax but not gotten it to work (be mindful of parantheses):</p>
  
  <pre><code>choco = driver.find_element_by_xpath("//*[text()[contains(., 'Chocolate')] and not[[contains(., 'Dark')]]]")
  </code></pre>
  
  <p>Here is the same code using line breaks and concatenation for readability:</p>
  
  <pre><code>choco = driver.find_element_by_xpath((
                                      "//*[text()[contains(., 'Chocolate')]" + 
                                      "and not[[contains(., 'Dark')]]]"
                                      ))
  </code></pre>