2017-06-18 224 views
0

我“米试图用seleniumpython检索词‘年度报告’和‘首次公开发行招股说明书’。蟒蛇硒打印“日”

我尝试使用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> 
+0

如果没有看到其他表*,很难给出一个很好的答案。你能分享一个吗? – Andersson

回答

0

请使用以下XPath

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

此XPath能够在乌拉圭回合的HTML定位包括文本code.Try出这个

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")]) 
0

你说有多个表在页面上。你知道这张桌子的完整路径吗?获取每个'th'元素的全部(a.k.a.绝对)路径,并将单独的WebDriver调用到find_element_by_xpath。

现在已经说了,你通常不希望使用绝对路径来定位元素(它们需要很长时间,而且很脆弱)。所以,如果有可能(即您或您认识已经开发了这个网页,并能完全控制的HTML的人),你应该把一个ID在该表中,然后你可以这样做:

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

甚至更​​好,把ID放在你想要的两个'th'元素上。