I'm using Selenium XPATH or CSS to identify and select a checkbox within an HTML table based on the specified text. As a result, I have successfully

I am trying to select a checkbox from an HTML table with multiple columns. Specifically, I want to choose the checkbox that corresponds to the "CLEAN_AUDIT" option for the Name column. However, my current XPATH is selecting 4 checkboxes labeled CLEAN_AUDIT for Name, DOB, Address, and Phone:

Here's what the table structure looks like:

Table Header:             field        variable
               checkbox | Time_STAMP  | DOB
               checkbox | Time_STAMP  | Address
               checkbox | Clean_AUDIT | Name
               checkbox | Clean_AUDIT | Address
               checkbox | Clean_AUDIT | DOB
               checkbox | Clean_AUDIT | Phone
               etc...

The specific checkbox I need to select is:

  • Clean_AUDIT for Name

Currently, my XPATH selects all 4 checkboxes as mentioned above.

  • Clean_AUDIT for Name
  • Clean_AUDIT for Address
  • Clean_AUDIT for DOB
  • Clean_AUDIT for Phone

The third column in the table represents the Variable column where "Name" appears and I require the corresponding field from the second column known as Field.

How can I construct an XPATH expression to locate the third column containing "Name" and the second column with "Clean_AUDIT"

In a different scenario, I might need to identify "Address" marked as "Clean_AUDIT"

Your version of the XPATH currently collects all occurrences of Clean_AUDIT across all Variables:

//table[@id="reporting_add_report_tab_manual_ct_fields_avail"]//div[(text()="CLEAN_AUDIT")]/preceding::td[1]//input

To refine it further, I must somehow include Name to pinpoint the exact Clean_Audit selection.

An excerpt of the simplified HTML code sample is as follows:

<table id="reporting_add_report_tab_manual_ct_fields_avail" class="GLKP2TGBJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
    <colgroup>
        <tbody style="">
            <tr class="GLKP2TGBCD GLKP2TGBJD" __gwt_subrow="0" __gwt_row="0">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-500" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-501" style="outline-style:none;">USN</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD GLKP2TGBKD">
                    <div __gwt_cell="cell-gwt-uid-502" style="outline-style:none;" />
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="52">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Address</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="20">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">EXPANDED_CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Name</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCE" __gwt_subrow="0" __gwt_row="21">
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CATEGORY</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDE GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">Name</div>
                </td>
            </tr>
            <tr class="GLKP2TGBCD" __gwt_subrow="0" __gwt_row="62">
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBED">
                    <div __gwt_cell="cell-gwt-uid-552" style="outline-style:none;">
                        <input type="checkbox" tabindex="-1" />
                    </div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD">
                    <div __gwt_cell="cell-gwt-uid-553" style="outline-style:none;">CLEAN_AUDIT</div>
                </td>
                <td class="GLKP2TGBBD GLKP2TGBDD GLKP2TGBOD">
                    <div __gwt_cell="cell-gwt-uid-554" style="outline-style:none;">DOB</div>
                </td>
            </tr>
        </tbody>
        <tbody style="display: none;">
            <tfoot style="display: none;" aria-hidden="true" />

Sincerely, Riaz

Answer №1

UPDATED

WebElement element = getDriver().findElement(By.xpath("//table[@id='reporting_add_report_tab_manual_ct_fields_avail']//div[(text()='CLEAN_AUDIT')]/following::div[(text()='Name')]/preceding::td[1]/preceding::td[1]//input"));
element.click();

This is a functional example. I have verified the functionality of this code snippet using the provided sample in the original question.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering a dropdown element that has the attribute aria-activedescendant using Selenium

I've encountered a challenge while automating a webpage using Selenium. The page has a dropdown with three options, but when I try to interact with it, there are no visible changes in the HTML code within Chrome devtools. This makes it difficult for m ...

Performing a search using the ID attribute in Selenium with C# leads to the utilization of the Css

Okay, so I have this DIV that I am trying to access: <div id="FromTitle" [something,something]> My code for accessing it looks like this: var searchID = "FromTitle"; //some other code that successfully finds elements driver.FindE ...

Struggling to locate element with xpath in Selenium using Python

I am new to Python+selenium and trying to create some automations. Unfortunately, inspecting elements on a specific webpage has been quite challenging. There are no ids available for me to use, so I have been attempting to use xpath instead. I am specifica ...

What is the method for fetching text with neither a node nor attribute using xpath in Selenium?

I am currently attempting to extract specific text from an HTML document using xpath. The structure of the HTML is shown below: The desired "target text" that I want to retrieve is located within a p node. However, this "target text" does not have any s ...

Divide a SINGLE BACKGROUND IMAGE in HTML into two separate links of equal size, one at the top and

As a beginner in HTML, I am trying to find a way to divide a background image into two equal sections without using image mapping. I attempted to split the links by setting the style to 0% and 50% to designate the top and bottom halves, but unfortunately, ...

How can you find the "et-custom-css" section in WordPress?

One of the challenges I'm facing with my WordPress page is that some CSS classes are located under <style type="text/css" id="et-custom-css">. Additionally, there is a comment at the top of the CSS saying: /* - - - - - - Additional page info - ...

Displaying the information from a nested array of objects in an HTML table through iteration

In the code snippet below, there is an input with a nested array of objects. The main array of objects is called summary and within it, there's a nested array called run_type. let input = { "summary": [ { " ...

Navigating a website and interacting with non-button elements using Selenium and BeautifulSoup

I am trying to automate the process of clicking on each element in a list on this website. The list includes items such as: Die Päpstin Leo Lausemaus will nicht teilen Pixi ... Löcher - Die Geheimnisse von Green Lake ... Upon inspecting the HTML of the ...

How to perfectly center a dropdown menu using CSS across different web browsers

I am dealing with a dropdown menu that is auto-generated by WordPress, so altering the HTML structure is not an option. However, I have the flexibility to adjust the CSS styling to correct an alignment issue in my dropdown menu. Currently, I'm facing ...

I'm struggling to make this background show up in a div

Anyone able to help me figure this out? I can't seem to get the achtergrond_homepage.png as a background in rounded corners. Edit: It seems like the gray color is always on top. Could it be controlled in the JavaScript part? This is the CSS: @ch ...

Adjusting image size within floating containers

I am working on a design with two floating divs, one on the left and one on the right, both nested within a main div. Each floating div contains an image that needs to be resized to the maximum size possible without differing sizes. This is what I curren ...

Automating Checkbox Selections with Selenium in Python

I'm having trouble clicking on a checkbox. Here is the HTML Code: <div class="mb-1 p-3 termsCheck"> <input class="form-check-input float-end" type="checkbox" value="" id="flexCheckDefau ...

Implementing css padding to text preceding images

My content includes text and images in a mix, and I wish to add CSS margin to elements that appear directly before and after the images. Unfortunately, the placement of these elements can vary and is beyond my control. The code snippet below demonstrates ...

What is the best way to add a centered progress spinner on top of an overlay?

Here is a code snippet that includes functionality to append a site overlay as needed. I am currently trying to implement the feature of also displaying an image progress spinner in the center of the screen over the overlay. I would prefer if this spinner ...

Issue: unable to locate a compatible version of seleniumwire for Windows Server 2022

As a new developer in Python, I am looking to use Seleniumwire on my Windows 11 system. After running the command "pip install seleniumwire," I encountered an error stating "could not find a version that satisfies the requirement seleniumwire." Any assis ...

Discovering the correct element for text input in Selenium Automation

I am looking to automate a test on the Microsoft Redeem Code page I attempted to input a value using the following code: driver.findElement(By.id("tokenString")).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ"); Upon inspecting the element, ...

Present Images and Text Alongside Each Other in a Grid Layout

My goal is to have an image and text displayed side by side, but for some reason they are appearing stacked on top of each other. https://i.stack.imgur.com/F8OXL.png If you want to take a look at the code I am using, here is the link to my CodePen: https: ...

Top method for identifying "abandoned words" within text that has been wrapped

Our content and design teams have a specific request to prevent paragraphs from ending with an "orphan word" - a single word on the last line of text that has wrapped onto multiple lines. The designer's proposed solution is to adjust the margins sligh ...

Design a logo to serve as the main button on the navigation bar of the

I've been experimenting with adding a logo instead of the 'Home' text in my navigation bar, but I'm not sure where to implement it. Should I add it within #nav or separately? Also, I want the nav bar to stay fixed without overlapping on ...

Even in the implementation of find_element(By.XPATH, value='xpath_value'), NoSuchElementException may still manifest

Objective: Extracting movie titles from and converting them into a list. The provided code: driver = webdriver.Chrome() driver.get('https://www.boxofficemojo.com/chart/top_lifetime_gross/?ref_=bo_cso_ac') xpath = '//td[@class="a-text ...