When using Selenium with XPATH, I need to target a specific checkbox within a column of checkboxes. My goal is to select the checkbox located in the second row

I need help selecting a specific checkbox from a list of checkboxes in a table. The XPATH I've tried is not working as intended and ends up selecting all the checkboxes.

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]//input[@type = "checkbox"]

Here is the relevant HTML:

<table id="match_configuration_add_possible_tab_match_rules_tb_match_rules" class="GOFU2OVJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
    <thead aria-hidden="false">
        <tr __gwt_header_row="0">
            <th class="GOFU2OVID GOFU2OVGD" __gwt_header="header-gwt-uid-311" __gwt_column="column-gwt-uid-310" colspan="1">
                <span style="">
                    <input type="checkbox"/>
                </span>
            </th>
            <th class="GOFU2OVID GOFU2OVAE" __gwt_header="header-gwt-uid-313" __gwt_column="column-gwt-uid-312" colspan="1">Name</th>
        </tr>
    </thead>
    <colgroup>
    <tbody style="">
        <tr class="GOFU2OVCD GOFU2OVMD" __gwt_subrow="0" __gwt_row="0">
            <td class="GOFU2OVBD GOFU2OVDD GOFU2OVED GOFU2OVND">
                <div __gwt_cell="cell-gwt-uid-299" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GOFU2OVBD GOFU2OVDD GOFU2OVOD GOFU2OVLD GOFU2OVND">
                <div __gwt_cell="cell-gwt-uid-300" style="outline-style:none;">
                    <input id="" class="" type="text" style="color: blue;" value=""/>
                </div>
            </td>
        </tr>
        <tr class="GOFU2OVCE GOFU2OVJD" __gwt_subrow="0" __gwt_row="1">
            <td class="GOFU2OVBD GOFU2OVDE GOFU2OVED GOFU2OVKD">
                <div __gwt_cell="cell-gwt-uid-299" style="outline-style:none;">
                    <input type="checkbox" tabindex="-1"/>
                </div>
            </td>
            <td class="GOFU2OVBD GOFU2OVDE GOFU2OVOD GOFU2OVKD">
        </tr>
    </tbody>
    <tbody style="display: none;">
    <tfoot style="display: none;" aria-hidden="true"/>
</table>

I'm trying to select the checkbox with attribute cell-gwt-uid-299 without using the UID value directly in the XPATH because it changes dynamically. Can you suggest an alternative XPATH?

I've also attempted using ancestor and preceding axes, but they are selecting checkboxes from other tables. Here's an example of what was highlighted when using preceding:

//table[@id="match_configuration_add_possible_tab_match_rules_tb_match_rules"]/preceding::tr[1]//input[@type = "checkbox"]

What would be the correct XPATH to target the desired checkbox?

Answer №1

If you're looking to target the checkbox in the second row, here's how you can do it:

//table[@id='match_configuration_add_possible_tab_match_rules_tb_match_rules']//tbody/tr[2]//input[@type='checkbox']

When it comes to using "preceding" and "ancestor":

The reason you were unable to locate the checkbox is because it appears AFTER your table element. By using "preceding", you were actually searching for a checkbox that comes BEFORE the table element in the HTML structure. The same logic applies to "ancestor" - this was attempting to find an element "ABOVE" the table element as a tr, which might not be present unless the table is nested within another table. Instead of these, consider using "descendant", which searches "WITHIN" your specified element.

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

Adjusting background position using incremental changes through JavaScript

Although I have a strong background in PHP coding, using javascript is a new venture for me so I hope this question doesn't sound naive. My objective is to create a button that, when clicked, shifts the background-position of a specified DIV object b ...

Issue with background image resizing when touched on mobile Chrome due to background-size property set to cover

My current challenge involves setting a background image for a mobile page using a new image specifically designed for mobile devices. The image is set with the property background-size: cover, and it works perfectly on all platforms except for mobile Chro ...

The hamburger symbol (&#9776;) appears as a dull shade on Android screens

Imagine a basic website design featuring a gray background with white text. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> </head> <body style="background: gray; color: white; font-size:50px;" > The t ...

Compatibility issues with jQuery observed in Firefox and Internet Explorer 11

Check out the code here: jsfiddle demo HTML CODE: <div class="first"> <!-- Part one --> <div class="acord_col"> <div class="img_class" id="exist_site"></div> <div class="intro_text"> </div> </div&g ...

To calculate the sum of input field rows that are filled in upon button click using predetermined values (HTML, CSS, JavaScript)

Greetings for exploring this content. I have been creating a survey that involves rows of buttons which, when clicked, populate input fields with ratings ranging from 5 to -5. The current code fills in one of two input fields located on opposite sides of ...

Horizontal Accordion Design for Cascading Style Sheets (CSS) Books

I am currently working on developing a page that features a book layout. This page will include tabs that users can expand individually. If you would like to see a working example, you can check out this link: https://codesandbox.io/s/book-layout-l28gh?fi ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

When trying to display an image using CSS, the image does not appear on the screen

I've been working on streamlining my HTML code and moving as much as possible to my CSS file. However, I'm facing an issue with getting an image to display when writing the CSS code for it. Here's the HTML: <html> <head> <l ...

Customizing the screen dimensions using PhantomJS in C# using Selenium automation

Is there a way to adjust the browser screen size in PhantomJS? I currently have it set up as below, but when I take a screenshot, it only shows a 400px wide image. var driverService = PhantomJSDriverService.CreateDefaultService(); driverService.HideComm ...

How do I ensure in Selenium using Java that all backend processes have finished before moving on to the next command?

I have multiple backend processes running in my project, causing some of my test cases to pass intermittently. Is there a method in Selenium that allows me to wait for all backend processes to complete before executing the next command? ...

In Selenium using C#, the exception status was identified as ReceiveFailure, with the accompanying message stating that the underlying connection had been closed

"An exception occurred with a null response when sending an HTTP request to the remote WebDriver server for URL http://localhost:49803/session. The status of the exception was ReceiveFailure, and the message was: The underlying connection was closed due to ...

Using RMarkdown to incorporate custom CSS styling in your documents

Having some trouble with my HTML report created from RMarkdown and applying CSS. The browser version displays correctly, but when printed, the styling doesn't come through. Below is an example of the RMarkdown code: --- title: "Table" output: html_ ...

Encountered a ValueError while attempting to convert the string '1,077' to an integer using my personally-created cookie clicker bot following a tutorial on YouTube

Recently, I followed a tutorial on Youtube by Techwithtim and started executing a script using python and selenium. Everything was going smoothly until I encountered an error: "ValueError: invalid literal for int() with base 10: '1,077'" Here is ...

Creating a curved container with CSS styling

I've attempted to create a design resembling the image below, but I'm not achieving the desired result. Can someone provide me with assistance? Any help would be greatly appreciated. https://i.sstatic.net/J4LzC.png <!DOCTYPE ht ...

Parent component in Material-ui theme distinguishing itself from child component of the same type

I'm working on customizing the styling of a Material UI Expansion Panel, specifically to apply my theme only to the main parent panel when it is expanded, and not to any nested panels within it. For instance: https://i.sstatic.net/nBhcN.png Here&ap ...

Positioning a button on the side of the screen while a hidden side panel is open

I am attempting to create an animation using a side panel that will slide into view when a button is clicked. Here is how it appears when the page loads: https://i.sstatic.net/JPQ2W.jpg When the user clicks on one of the buttons, the notes panel will be ...

Troubleshooting Bootstrap 3.0: Issues with nav-tabs not toggling

I have set up my navigation tabs using Bootstrap 3 in the following way: <ul class="nav nav-tabs pull-right projects" role="tablist" style="margin-top:20px;"> <li class="active"><a role="tab" data-toggle="tab" href="#progress">In Pr ...

A div set to absolute positioning and a width of 100% will exceed the boundaries of a Bootstrap 4

I am attempting to position a div with 100 percent width within a Bootstrap 4.1 col using absolute positioning in order to attach it to the bottom. However, I am encountering an issue where it overflows on the right side of the screen. I have tried assigni ...

Effortlessly lower several elements simultaneously with SlideDown

My form div contains multiple inner divs: <div class="form" style="display:none"> For example: <div class="row"> <?php echo $form->labelEx($model,'comment'); ?> <?php echo $form->textField($model,'commen ...

Getting all inline styles from an HTML string using JavaScript

(JavaScript) I am working with an html email template stored as a string. Is there a way to extract all the inline styles used in the template? For instance, if I have this input: <div style="min-width:300px;max-width:600px;overflow-wrap:break-word ...