2016-11-22 51 views
1

我試圖點擊此處的文本http://ecos.fws.gov/ecp0/profile/speciesProfile?sId=6994,但無法提供正確的語法。Python中用於xpath的Selenium語法

我想要做的是點擊「查看全部」,但不幸的是,在html中有多個「查看所有」。 HTML看起來像這樣:

<li style="margin-bottom:1em;"> 
<p><strong>Population location:</strong> Wherever found west of Mobile and Tombigbee Rivers in AL, MS, and LA</p> 
<p><strong>Listing status:</strong>  
        <span class="listingThreat">Threatened</span> 
</p> 
<ul> 
<ul> 
<li><b>States/US Territories</b> in which this population is known to or is believed to occur:  
    <a href="/ecp0/profile/countiesByState?entityId=181&amp;state=Alabama" onclick="window.open('', 'popup', 'width=800,height=550,scrollbars=yes,resizable=yes')" target="popup">Alabama</a> 
    , <a href="/ecp0/profile/countiesByState?entityId=181&amp;state=Louisiana" onclick="window.open('', 'popup', 'width=800,height=550,scrollbars=yes,resizable=yes')" target="popup">Louisiana</a> 
    , <a href="/ecp0/profile/countiesByState?entityId=181&amp;state=Mississippi" onclick="window.open('', 'popup', 'width=800,height=550,scrollbars=yes,resizable=yes')" target="popup">Mississippi</a> 
</li> 
<li><b>US Counties</b> in which this population is known to or is believed to occur:  
    <a href="/ecp0/profile/countiesBySpecies?entityId=181" onclick="window.open('', 'popup', 'width=800,height=550,scrollbars=yes,resizable=yes')" target="popup">View All</a></li> 
<li><b>USFWS Refuges</b> in which this population is known to occur:  
    Bogue Chitto National Wildlife Refuge, Egmont Key National Wildlife Refuge, Grand Bay National Wildlife Refuge, Hobe Sound National Wildlife Refuge, Mississippi Sandhill Crane National Wildlife Refuge<br/> 
</li> 
</ul> 
</ul> 
</li> 

我基本上需要選擇哪裏樣式是「margin-bottom:1em;」幷包含「威脅」和「查看全部」。不幸的是,對於提供的網址,有兩個「查看所有」,並且都位於style =「margin-bottom:1em;」的內部。

我只是想不出一種方法來唯一標識第一個「查看全部」。我想到的是某種條件的路徑,但不知道

回答

3

試試這個XPath出來 - "//span[@class='listingThreat']/ancestor::li//a[text()='View All']"

+0

那完美。我將不得不查看/ ancestor :: li。我還沒有看到與xpath一起使用。 – user44796

+0

@ user44796 Google「xpath axes」... – Grasshopper