python selenium从所选选项卡打印“th”

2024-09-27 17:55:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用selenium来检索“年报”和“IPO招股说明书”这两个词。在

我尝试使用driver.find_elements_by_class_name('sic_highlight'),但是因为有多个表具有相同的class_name,所以它也打印其他表中的所有内容。在

我怎么能只打印出‘年报’和‘IPO招股说明书’的文本,而不去搜索其他表格呢?在

<table class="sic_table" cellspacing="1">
  <thead>
    <tr class="sic_tableTopRow">
      <th scope="col">Report Type</th>
      <th scope="col">Year Ended</th>
      <th scope="col">Download</th>
    </tr>
  </thead>
  <tbody>
      <tr class="sic_highlight">
        <th colspan="3" scope="col" class="sic_highlight">Annual Report</th>
      </tr>
        <tr>
          <th class="si_left">Annual Report&nbsp;2016</th>
          <td class="si_center">Jun 2016</td>
          <td class="si_center">
              <a href="some_link">Part 1(1.41 MB)</a><br>
          </td>
        ....
        ....
        </tr>
      <tr class="sic_highlight">
        <th colspan="3" scope="col" class="sic_highlight">IPO Prospectus</th>
      </tr>
        <tr>
          <th class="si_left">IPO Prospectus&nbsp;2011</th>
          <td class="si_center">Jul 2011</td>
          <td class="si_center">
              <a href="some_link">Part 1(5.10 MB)</a><br>
          </td>
        </tr>
  </tbody>
</table>

Tags: reporttablecoltrclasstdcenterscope
3条回答

你说有多张桌子。你知道这个表的完整路径吗?获取到每个'th'元素的完整(也称为绝对)路径,并进行单独的WebDriver调用,以通过xpath查找\u元素。在

说到这里,您通常不想使用绝对路径来定位元素(它们需要很长时间,而且非常脆弱)。因此,如果有可能(例如,您或您认识的人开发了此网页并完全控制了HTML),您应该在该表上添加一个ID,然后您可以执行以下操作:

driver.find_element_by_id('tableIdHere').find_elements_by_class_name('sic_highlight');

或者更好的是,在你想要的两个'th'元素上加上id。在

使用以下xpath

 //table[@class='sic_table']/tbody/tr/th

这个Xpath能够在urhtml中找到这两个文本代码。试试出去吧

在XPATH:-*//tr[@class="sic_highlight"]/th[contains(text(),"Annual Report"|"IPO Prospectus" )]

driver.find_element_by_xpath('*//tr[@class="sic_highlight"]/th[contains(text(),"Annual Report"|"IPO Prospectus")])

相关问题 更多 >

    热门问题