0

I have a dialog displayed having clicked on an element. In this dialog there are 2 tables (HTML). A table within a table. Table 1 has the text "Match audit codes" Table 2 has some rows and columns. Column 1 has a checkbox. Column 2 has a char value, e.g. the letter "I") Column 3 has some text e.g. "Matched to full address"

I would like to locate the checkbox from column 1 which has the text description "Matched to Full Address" and the parent table has the text "Match audit codes"

I tried the following Xpath which locates the checkbox which has the text "Matched to full address"

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

My Selenium Python script will not click this checkbox. The developer says there could be more than 1 "Matched to full address" in the HTML somewhere.

I need the Xpath to start from the parent table which has the text "Match audit codes" and then go down to the next table which has the text "Matched to full address" and then locate the checkbox.

How can i build the Xpath please?

The HTML is:

 <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>

Thanks, Riaz

2 Answers 2

3

I'm not sure if I'm getting your question. You want to locate the table containing "Match audit codes", then move on to the next table containing "Matched to full address", and inside that table search for the checkbox in the same row as the "Matched to full address" text?

If so, then it should look like this:

//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]

If .='Matched to full address' doesn't work, try contains(.,'Matched to full address') instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this works. Selenium now clicks the element. Thank you for your help!
Glad I could help! Next time, try to post complete valid XHTML. Makes it easier to reproduce, thus you will get more answers more quickly.
1

Here is a tip:

Find all elements with text to be matched.

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

Now store the indexes of elements which contains your desired text.

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);
}

Now you have the list of all indexes with text as 'Matched to full address'. You can now call the corresponding check boxes and play.

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
}

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.