Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python selenium print';th';从所选表格_Python_Selenium - Fatal编程技术网

python selenium print';th';从所选表格

python selenium print';th';从所选表格,python,selenium,Python,Selenium,我试图在python中使用selenium来检索单词“年度报告”和“IPO招股说明书” 我尝试使用驱动程序。通过类名称(“sic\u highlight”)查找元素,但由于有多个表具有相同的类名称,因此它也会打印其他表中的所有内容 我如何打印“年度报告”和“IPO招股说明书”文本而不搜索其他表格 <table class="sic_table" cellspacing="1"> <thead> <tr class="sic_tableTopRow">

我试图在
python
中使用
selenium
来检索单词“年度报告”和“IPO招股说明书”

我尝试使用
驱动程序。通过类名称(“sic\u highlight”)查找元素
,但由于有多个表具有相同的
类名称,因此它也会打印其他表中的所有内容

我如何打印“年度报告”和“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>

报告类型
年终
下载
年度报告
2016年度报告
2016年6月

.... .... 首次公开募股说明书 2011年首次公开募股招股说明书 2011年7月

使用以下xpath

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

此Xpath能够在您的html代码中找到这两个文本

XPATH:-
*//tr[@class=“sic_highlight”]/th[包含(text(),“年度报告”|“IPO招股说明书”)]


您说过页面上有多个表。您知道该表的完整路径吗?获取每个“th”元素的完整(也称为绝对)路径,并分别调用WebDriver以通过xpath查找元素

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

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

或者更好的方法是,将ID放在您想要的两个“th”元素上。

如果不查看其他表,很难给出一个好的答案。您可以共享一个吗?
driver.find_element_by_id('tableIdHere').find_elements_by_class_name('sic_highlight');