【问题标题】:Selenium Python how do i find the right checkbox element from a HTML table within a table using XPathSelenium Python如何使用XPath从表格中的HTML表格中找到正确的复选框元素
【发布时间】:2016-07-03 22:02:01
【问题描述】:

我在单击一个元素后显示了一个对话框。在此对话框中有 2 个表格 (HTML)。表中的表。 表 1 有文本“匹配审计代码” 表 2 有一些行和列。 第 1 列有一个复选框第 2 列有一个 char 值,例如字母“我”) 第 3 列有一些文字,例如“匹配到完整地址”

我想从第 1 列找到复选框,该复选框具有文本描述“与完整地址匹配”,并且父表具有文本“匹配审核代码”

我尝试了以下 Xpath,它找到了带有“匹配到完整地址”文本的复选框

//div[contains(text(), "Matched to full address")]/ancestor::tr[1]/td[1]//input

我的 Selenium Python 脚本不会点击此复选框。开发人员说,HTML 中的某处可能有超过 1 个“匹配到完整地址”。

我需要 Xpath 从具有文本“匹配审核代码”父表开始,然后向下到下一个表 其中有文本“匹配到完整地址”,然后找到复选框。

请问我该如何构建 Xpath?

HTML 是:

 <table class="GJPPK2LBAL" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
        <td align="left" style="vertical-align: top;">
            <div class="GJPPK2LBPK">Match audit codes</div>
        </td>
        <td align="left" style="vertical-align: top;">
            <button class="gwt-Button GJPPK2LBLK" type="button">X</button>
        </td>
    </tr>
    </tbody>
</table>
<table class="GJPPK2LBOK" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
        <td align="left" style="vertical-align: middle;">
            <table cellspacing="0" cellpadding="0">
                <tbody>
                <tr>
                <tr>
                    <td align="left" style="vertical-align: top;">
                        <div style="overflow: auto; position: relative; width: 25em;">
                            <div style="position: relative;">
                                <table class="GJPPK2LBJE" cellspacing="0"
                                       __gwtcellbasedwidgetimpldispatchingfocus="true"
                                       __gwtcellbasedwidgetimpldispatchingblur="true">
                                    <thead aria-hidden="false">
                                    <colgroup>
                                    <tbody>
                                    <tr class="GJPPK2LBCD" __gwt_subrow="0" __gwt_row="0">
                                        <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED">
                                            <div __gwt_cell="cell-gwt-uid-5972" style="outline-style:none;">
                                                <input type="checkbox" tabindex="-1"/>
                                            </div>
                                        </td>
                                        <td class="GJPPK2LBBD GJPPK2LBDD">
                                            <div __gwt_cell="cell-gwt-uid-5973" style="outline-style:none;">I</div>
                                        </td>
                                        <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD">
                                            <div __gwt_cell="cell-gwt-uid-5974" style="outline-style:none;">Matched to
                                                full address
                                            </div>
                                        </td>
                                    </tr>
                                    <tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="1">
                                        <td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBED">
                                            <div __gwt_cell="cell-gwt-uid-5972" style="outline-style:none;">
                                                <input type="checkbox" tabindex="-1"/>
                                            </div>
                                        </td>
                                        <td class="GJPPK2LBBD GJPPK2LBDE">
                                            <div __gwt_cell="cell-gwt-uid-5973" style="outline-style:none;">B</div>
                                        </td>
                                        <td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBOD">
                                            <div __gwt_cell="cell-gwt-uid-5974" style="outline-style:none;">Matched to
                                                building
                                            </div>
                                        </td>
                                    </tr>
                                    <tr class="GJPPK2LBCD" __gwt_subrow="0" __gwt_row="2">
                                    <tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="3">
                                    </tbody>
                                    <tbody style="display: none;">
                                    <tfoot style="display: none;" aria-hidden="true"/>
                                </table>

谢谢, 里亚兹

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案1】:

    我不确定我是否收到您的问题。您想找到包含“匹配审计代码”的表,然后转到下一个包含“匹配到完整地址”的表,并在该表中搜索与“匹配到完整地址”文本在同一行中的复选框?

    如果是这样,那么它应该是这样的:

    //table[.//div[text()='Match audit codes']]/following-sibling::table[.//div[.='Matched to full address']][1]//div[.='Matched to full address']/ancestor::tr[1]/td//input[@type='checkbox'][1]
    

    如果.='Matched to full address' 不起作用,请尝试使用contains(.,'Matched to full address')

    【讨论】:

    • 是的,这行得通。 Selenium 现在单击元素。感谢您的帮助!
    • 很高兴我能帮上忙!下一次,尝试发布完整的有效 XHTML。使其更容易重现,因此您将更快地获得更多答案。
    【解决方案2】:

    这里有一个提示:

    找到所有要匹配文本的元素。

    List<WebElement> texts = driver.findElements(By.xpath("//td[@class='GJPPK2LBBD GJPPK2LBDE GJPPK2LBOD']"));
    

    现在存储包含所需文本的元素的索引。

    List<Integer> list = new ArrayList<>();
    for(int i=0;i<list.size();i++){
    if(text.get(i).getText().equals("Matched to full address"))
    list.add(i);
    }
    

    现在您有了所有索引的列表,其中包含“匹配到完整地址”的文本。您现在可以调用相应的复选框并播放。

    List<WebElement> checks = driver.findElements(By.xpath("//td[@class='GJPPK2LBBD GJPPK2LBDE GJPPK2LBED']"));
    
    for(int index:checks){
    WebElement cb = checks.get(index);
    //Do something with cb
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多