Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.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
Html 查找XPATH表达式_Html_Css_Xml_Xpath - Fatal编程技术网

Html 查找XPATH表达式

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

对于以下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.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:]