Xpath - How to select related cousin data

2019-09-17 19:25发布

<html>
    <table border="1">
        <tbody>
            <tr>
                <td>
                    <table border="1">
                        <tbody>
                            <tr>
                                <th>aaa</th>
                                <th>bbb</th>
                                <th>ccc</th>
                                <th>ddd</th>
                                <th>eee</th>
                                <th>fff</th>
                            </tr>
                            <tr>
                                <td>111</td>
                                <td>222</td>
                                <td>333</td>
                                <td>444</td>
                                <td>555</td>
                                <td>666</td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</html>

How can i select specific related cousin data using xpath, The desired output would be be:

<th>aaa</th>
<th>ccc</th>
<th>fff</th>
<td>111</td>
<td>333</th>
<td>666</td>

The most important aspect of the xpath is that i am looking to be able to include or exclude certain <th> tags and their corresponding <td>tags

So based on the answers so far the closest I have is:

//th[not(contains(text(), "ddd"))] | //tr[2]/td[not(position()=4)]

Is there any way of not explicitly using position()=4 but instead reference the corresponding th tag

2条回答
叼着烟拽天下
2楼-- · 2019-09-17 19:42

I'm not sure that this is the best solution, but you might try

//th[not(.="bbb") and not(.="ddd") and not(.="eee")] | //tr[2]/td[not(position()=index-of(//th, "bbb")) and not(position()=index-of(//th, "ddd")) and not(position()=index-of(//th, "eee"))]

or shorter version

//th[not(.=("bbb", "ddd", "eee"))]| //tr[2]/td[not(position()=(index-of(//th, "bbb"), index-of(//th, "ddd"),index-of(//th, "eee")))]

that returns

<th>aaa</th>
<th>ccc</th>
<th>fff</th>
<td>111</td>
<td>333</td>
<td>666</td>

You can avoid using complicated XPath expressions to get required output. Try to use Python + Selenium features instead:

# Get list of th elements
th_elements = driver.find_elements_by_xpath('//th')
# Get list of td elements
td_elements = driver.find_elements_by_xpath('//tr[2]/td')
# Get indexes of required th elements - [0, 2, 5]
ok_index = [th_elements.index(i) for i in th_elements if i.text not in ('bbb', 'ddd', 'eee')]
for i in ok_index:
    print(th_elements[i].text)
for i in ok_index:
    print(td_elements[i].text)

Output is

'aaa'
'ccc'
'fff'
'111'
'333'
'666'

If you need XPath 1.0 solution:

//th[not(.=("bbb", "ddd", "eee"))]| //tr[2]/td[not(position()=(count(//th[.="bbb"]/preceding-sibling::th)+1, count(//th[.="ddd"]/preceding-sibling::th)+1, count(//th[.="eee"]/preceding-sibling::th)+1))]
查看更多
可以哭但决不认输i
3楼-- · 2019-09-17 19:54

Using XPath 3.0 you can structure that into

let $th := //table/tbody/tr[1]/th, 
$filteredTh := $th[not(. = ("bbb", "ddd", "eee"))], 
$pos := $filteredTh!index-of($th, .)
return ($filteredTh, //table/tbody/tr[position() gt 1]/td[position() = $pos])
查看更多
登录 后发表回答