Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting a specific value in column 2, based on value in column 1, in a grid with XPATH

I am using selenium in Python to automate the tracking of attendance for events using a specific website for my organization. The list of attendees is built in a grid, with column 1 with the names of the attendees, and column 2 with the checkbox.

The idea for my Python code is to seek for a specific name in column 1, and then check the box in column 2.

The structure of the grid is the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div id="grid" style="padding: 1em; display: grid; grid-template-columns: repeat(3, minmax(auto, 20em)); gap: 10px; align-items: center">
    <div style="grid-column: 1">
        "name1"                                        
    </div>
    <div style="width: 25px; grid-column: 2">
        <div class="cbx-container">
            <div class="cbx cbx-md cbx-active" tabindex="1000">
                <span class="cbx-icon"></span>
            </div>
            <input type="text" id="u1b7be9100c2-evforodn5xad-attended" class="check-all-item-1" name="attended-1" value="0" data-krajee-checkboxx="checkboxX_4d99482d" style="display: none;">
        </div>                                        
    </div>
    <div style="grid-column: 1">
        "name2"                                        
    </div>
    <div style="width: 25px; grid-column: 2">
        <div class="cbx-container">
            <div class="cbx cbx-md cbx-active" tabindex="1000">
                <span class="cbx-icon"></span>
            </div>
            <input type="text" id="u17528d00c2-evforodn5xad-attended" class="check-all-item-1" name="attended-1" value="0" data-krajee-checkboxx="checkboxX_4d99482d" style="display: none;">
        </div>                                        
    </div>                                                                                                          </div>

In the example above, I have 2 lines. In the first line, " name1" in column 1, and a checkbox in column 2. In the second line, " name2" in column 1, and a checkbox in column 2. The real dataset has more lines.

What would be the XPATH syntax to select the checkbox for "name2"?

Unfortunately, I was unable to get this done, as I am only starting with XPATH. So far, I was only able to select the names using the following:

"//div[contains(text(),'name2')]"

>Solution :

You were on the right track with your proposed XPath… you just needed one more step, following::, e.g.

//div[contains(text(),'name1')]//following::input

Since you are using python, you can assign the desired name to a variable, name, and then use that in your XPath, e.g.

name = "name1"
driver.find_element(By.XPATH, f"//div[contains(text(),'{name}')]//following::input")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading