title: How to specify specific elements based on their attributes using XPATH in LXML
body: <p>I am trying to improve my understanding of XPATH.  I have a document that has many elements.  I am looking for font elements within the document that have some specific text that is bolded.  </p>
  
  <p>Here is an example of a font element inside a div element.  The font element has text that is bold that I want to capture</p>
  
  <pre><code>&lt;div style="line-height:120%;padding-bottom:10px;padding-top:10px;font-size:10pt;"&gt;&lt;font style="font-family:inherit;font-size:10pt;font-weight:bold;"&gt;SECTION 1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Executive&amp;nbsp;Summary&lt;/font&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Let me be clear this is part of a much larger document,. I found an XPATH tutorial and it described how to select specific elements</p>
  
  <p>Just to make sure I am not running into an issue with how I am reading the file </p>
  
  <pre><code>tree=html.fromstring(open('c:\\mytest.htm')
  
  x=tree.xpath('//font') 
  </code></pre>
  
  <p>This worked as x has 3023 elements and when I examined them I found that they were all font elements.  Some were the elements I want.</p>
  
  <p>I then tried to isolate the relevant elements by using</p>
  
  <pre><code>my_elements = tree.xpath('//font[@font-weight='bold']')
  </code></pre>
  
  <p>That did not work as my_elements is empty.  While writing this question I looked closer at the snip of html and played around some in IDLE.  I think the problem is that I there is not an attribute of the font tag font-weight. Font-weight is something related to the style attribute  The only attribute of the font tag in this example is style.  I want to say more but am afraid I will muddy the water too much</p>
  
  <p>Bottom line I want to be able to use xpath to find all font elements that are bold and have the word section in the text I can do this by iterating through the elements and testing in a really clunky way</p>
  
  <pre><code>my_elements = [e for e in tree.iter() if e.tag == 'font' if 'bold' in e.values()[0] ]
  my_elements = e for e in my_elements if 'section' in e.text_content().lower()
  </code></pre>
  
  <p>XPATH just looks like it is well worth understanding.  </p>
  
  <p>Thanks for any explanation.</p>
  
  <p>Humm I finally am on the right track</p>
  
  <pre><code>testelem=tree.xpath('//font[contains(@style,"font-weight:bold")]')
  </code></pre>
  
  <p>okay now we use the and operator</p>
  
  <pre><code>testelem=tree.xpath('//font[contains(@style,"font-weight:bold") and contains(text(),"SECTION")]')
  </code></pre>
  
  <p>Now to make it case-insensitive</p>
  
  <p>I am getting close to understanding how contains works but am happy for someone who does to put up a solution</p>
  
  <pre><code>testelem=tree.xpath('//font[contains(@style,"font-weight:bold") and starts-with(translate(text(),"SECTION","section"),"section")]')
  </code></pre>