Pulling table data with mixed element types
<p>I am trying to pull data from a table using Python and Selenium however a few of the columns have a mix between gif and text. When I print the text element it returns the text along with blanks were the gif elements are within the column. However when I prints the gif elements, it returns all the gifs from the table (not just the column) without any blanks for the text fields. Any ideas how I can pull both elements types from the column? Thanks.</p>

<p>Table example:</p>

<pre><code>&lt;td class="X"&gt;
&lt;img src="/a/b/c/d.gif"&gt;
&lt;td&gt;
</code></pre>

<p>and</p>

<pre><code>&lt;td class="X"&gt;
&lt;div class="default-value"&gt;Not Applicable&lt;/div&gt;
&lt;/td&gt;
</code></pre>

<p>Code for text</p>

<pre><code>posts = driver.find_elements_by_class_name("x")
   for post in posts:
   print(post.text)
</code></pre>

<p>Code for gif</p>

<pre><code>for element in driver.find_elements_by_tag_name('img'):
    print(element.get_attribute("src"))
</code></pre>

<p>Find all <code>td</code> elements first, then, for every <code>td</code> decide if you want to get the text or the <code>src</code> attribute of the <code>img</code> element:</p>

<pre><code>posts = driver.find_elements_by_css_selector("td.x")
for post in posts:
    images = post.find_elements_by_tag_name("img")
    if images:
         print(images[0].get_attribute("src"))
    else:
         print(post.text)
</code></pre>

python selenium selenium-webdriver