查找带有td的xth td包含在同一tr xpath python中 [英] find xth td with td contains in same tr xpath python

查看:99
本文介绍了查找带有td的xth td包含在同一tr xpath python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面有一个包含11列的表,为第2列提供了文本值,我想单击同一行的第11列(箭头按钮).

My page has a table with 11 columns, given text value for column 2, I want to click on the 11th column (arrow button) for the same row.

我的DOM看起来像这样:

My DOM looks like this:

<table id="atab"  width="100%" class="at-class">`  
    <thead>…</thead>  
    <tbody>  
        <tr>...</tr>  
        <tr>  
            <td>…</td>  
            <td>  
                <a href="/abc/def/ghi/prop?id=1">grp1</a>  
            </td>  
            <td>…</td>  
            <td>…</td>  
            <td>200</td>  
            <td>…</td>  
            <td>…</td>  
            <td>…</td>  
            <td>…</td>  
            <td>…</td>  
            <td>  
                <ul class="dropwd"  
                    <li class>  
                        <input class="a-act a-menu-button" type="button" value="^"  
                        <ul class="sub_menu" style="vis"">…</ul>  
                    </li>  
                </ul>  
            </td>  
        <tr>  
    <tbody>  

这工作(因为我刚刚从inspect元素中获取了xpath).我不能使用它.

THIS WORKS (as I just grabbed the xpath from inspect element). I can’t use it though.

myxpath = "//*[@id='atab']/tbody/tr[2]/td[11]/ul/li/input"
myxpath.click()

我需要使用Contains,然后抓住同级物品(第11位,拥有我的Contains的位置第9位).我为myxpath尝试了以下操作,但找不到该元素.我在其他地方使用了相同的格式,这很好.唯一的区别是这一次,我没有得到相邻的兄弟姐妹,而是第x个兄弟姐妹.

I need to use contains, then grab the sibling (11th position, 9th from where I have my contains). I tried the following for myxpath, but it cannot find the element. I have used same format elsewhere and it was fine. The only difference is this time, I am not getting the adjacent sibling but the xth sibling.

campg = grp1

//table[@class='at-class']//td[contains(text(),' " + campg + " ')]/following-sibling::td[9]/ul/li/input
//* [@id='atab']//td[contains(text(),' " + campg + " ')]/following-sibling::td[9]/ul/li/input
//* [@id='campaigns']//td[contains(text(),' " + campg +  " ')]/following-sibling::td[9]/ul/li/input[@type=’button’]

我也尝试了其他组合.

推荐答案

此XPath表达式选择表,查找匹配的行(其中第二列包含要搜索的指针),然后在此行第11列,其中包含相应的输入元素.

This XPath expression selects the table, looks for matching rows (in which the second column contains the needle you're searching for), then selects in this row the 11th column with the respecting input element.

不需要使用任何使用祖先和其后跟随的相对轴步,通常,只有下降树才更快,更易读且更不易出错.

No need to use any relative axis steps using ancestor and following, only descending the tree is usually faster, more readable and less error-prone.

//table[@id='atab']//tr[contains(td[2], 'needle')]/td[11]/ul/li/input

这篇关于查找带有td的xth td包含在同一tr xpath python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆