Extract HTML source code without any class or div

2020-05-10 04:56发布

I have list that does not contain any unique div or class.

I want to copy the HTML source code for each row below. I cannot find the class for the row. enter image description here

When I open the 'Edit HTML' I see the following code:

<tr style="font-size: 11px">
              <td class="center"><a href="/countries/1"><img alt="" src="/assets/flags/flag_1-1db156e1884c1b3d5614b55996cf96cd38843b290c7c43bdd5abbdb944b4075c.gif"></a></td>
              <td><a href="/employees/9526577">Bernard Aarslev</a></td>
              <td><a href="/clubs/1200094">Kirslev FC</a></td>
              <td align="right" style="padding-right: 5px;">69</td>
              <td>Talentspejder</td>
              <td>Talentspejder</td>
              <td align="right" style="padding-right: 5px;">24.000 C</td>
              <td class="center" style="width: 120px;">
                <div class="relative">
                  <div id="stats9526577" style="z-index: 99; position: absolute; top: -80px; right: 80px; display: none;" class="dark"></div>
                  <img src="/assets/detaljer-c83987d00da87f2fa8810793cc815a1659249440edee3c0d084333bc69323384.gif" alt="stats" onmouseout="hide_stats(9526577);" onmouseover="view_stats(9526577, 14, 13, 4, 7, 10, 8, 6, 3);">
                </div>
              </td>
            </tr>

How do I write the correct find_element_by_xpath function to make this work?

1条回答
Evening l夕情丶
2楼-- · 2020-05-10 05:30

You can use :

node = driver.find_element_by_xpath("//table[@class='stretch']//tr[@style][1]")

First we look for the table element containing a specific @class attribute. Then we look for the first tr element of this table containing a @style attribute.

EDIT : More details, since it was downvoted. Combine the preceding expression with element.get_attribute('outerHTML') (to keep the tags). So :

data = node.get_attribute('outerHTML')

If you need this for all the lines of the table, then :

node = driver.find_elements_by_xpath("//table[@class='stretch']//tr[@style]")
for elem in node :
    data = elem.get_attribute('outerHTML')
查看更多
登录 后发表回答