Html 查找XPATH表达式
对于以下html:Html 查找XPATH表达式,html,css,xml,xpath,Html,Css,Xml,Xpath,对于以下html: <tr> <td class="first">AUD</td> <td> 0.00 </td> <td> 1,305.01 </td> <td> 1,305.01 </td> <td> -65.20 </td> <td> 0.00 </td> <td> 0
<tr>
<td class="first">AUD</td>
<td> 0.00 </td>
<td> 1,305.01 </td>
<td> 1,305.01 </td>
<td> -65.20 </td>
<td> 0.00 </td>
<td> 0.00 </td>
<td> 1,239.81 </td>
<td class="fx-rate"> 0.98542 </td>
</tr>
使用以下命令:
//td[@class = 'first' and normalize-space() = 'AUD']/parent::tr/td[@class = 'fx-rate']
或者更清楚:
//tr[td[@class="first1" and normalize-space()="AUD"]]/td[@class="fx-rate"]
这就是我使用部分XPath解决问题的方法:
### get all the elements via xpath
currencies = driver.find_elements_by_xpath("//td[@class='first']")
fx_rates = driver.find_elements_by_xpath("//td[@class='fx-rate']")
### build a list and zip it to get the k,v pairs
fx_values = [fx.text for fx in fx_rates if fx.text]
currency_text = [currency.text for currency in currencies if currency.text]
zip(currency_text,fx_values)[1:]
我当然更喜欢第二种表达方式。从左到右更容易阅读。
### get all the elements via xpath
currencies = driver.find_elements_by_xpath("//td[@class='first']")
fx_rates = driver.find_elements_by_xpath("//td[@class='fx-rate']")
### build a list and zip it to get the k,v pairs
fx_values = [fx.text for fx in fx_rates if fx.text]
currency_text = [currency.text for currency in currencies if currency.text]
zip(currency_text,fx_values)[1:]