Getting whole text of an td in Python (lxml)
<p>I'm trying to get the whole text that is contained in this td:</p>

<p>Example:</p>

<pre><code>&lt;td&gt;
 &lt;p&gt;Some Text&lt;/p&gt;
 &lt;a&gt;SAMPLE&lt;/a&gt;
 &lt;table&gt;
  &lt;tbody&gt;
   &lt;tr&gt;
    &lt;td&gt;something&lt;/td&gt;
    ....
   &lt;/tr&gt;
   ...
  &lt;/tbody&gt;
 &lt;/table&gt;
 ...
&lt;/td&gt;
</code></pre>

<p>There are lots of tags inside this td, which makes it hard for me. Even tables are contained.</p>

<p>In FirePath (Firefox) I can simply attach //text()</p>

<blockquote>
  <p>.//*[@id='Testcases__list']/table/tbody/tr/td//text()</p>
</blockquote>

<p>But in Python code the //text() part throws me an error, using the lxml library</p>

<pre><code>Traceback (most recent call last):
  File "D:\pythonscripts\Bachelor\TestMain.py", line 52, in &lt;module&gt;
    print tr.findall('./td[6]//text()')[0].text
  File "src\lxml\lxml.etree.pyx", line 1563, in lxml.etree._Element.findall (src\lxml\lxml.etree.c:56897)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 304, in findall
    return list(iterfind(elem, path, namespaces))
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 277, in iterfind
    selector = _build_path_iterator(path, namespaces)
  File "C:\Python27\lib\site-packages\lxml\_elementpath.py", line 260, in _build_path_iterator
    selector.append(ops[token[0]](_next, token))
KeyError: '()'
</code></pre>

<p>How can I get the whole text of that td in Python?</p>

<p>You should be using <em>.xpath</em> not <em>findall</em>:</p>

<pre><code>tr.xpath("//*[@id='Testcases__list']/table/tbody/tr/td//text()")
</code></pre>

<p>To just get the first td:</p>

<pre><code> tr.xpath("(//*[@id='Testcases__list']/table/tbody/tr/td)[1]/text()")
</code></pre>

<p>I would also verify that the source actually has a <em>tbody</em> element, often it is added by the browser and not in the actual source.</p>

<p>You can <em>text_content</em>:</p>

<pre><code>tr.xpath("(//*[@id='Testcases__list']/table/tbody/tr/td)[1]")[0].text_content()
</code></pre>

python html xpath lxml