I need either XPATH or CSS to target a specific checkbox within a list of checkboxes wrapped in span tags

I'm trying to find the XPATH or CSS locator for a checkbox in an HTML span tag that has a label with specific text, like "Allow gender mismatch". I can locate the label using XPATH, but I'm not sure how to target the input tag so I can click on the checkbox.

Unfortunately, I can't use id="gwt-uid-1204" as it's a dynamic value that changes each time the page is visited.

The XPATH to get the label text is:

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]

Here is an example of the HTML snippet:

<div id="match_configuration_add_possible_tab_match_rules_fp_flags">
    <div class="gwt-Label matchruleheader">Gender and title flags</div>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1204" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1204">Gender must be present in both names</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1205" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1205">Gender must be consistent in both names</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
        <input id="gwt-uid-1206" type="checkbox" value="on" tabindex="0"/>
        <label for="gwt-uid-1206">Allow gender mismatch</label>
    </span>
    <span class="gwt-CheckBox" style="display: block;">
    <span class="gwt-CheckBox" style="display: block;">
    <span class="gwt-CheckBox" style="display: block;">
    -- More checkboxes
</div>

If anyone can assist with finding the correct XPATH or CSS locator for this scenario, I would greatly appreciate it. Thank you! - Riaz

Answer №1

There are at least two options available:

1) You can utilize xpaths ".." to move up one element

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]/../input

2) Alternatively, you can employ xpaths "preceding-sibling":

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]/preceding-sibling::input

Personally, I prefer the first method as it remains effective even if the order of sibling elements changes. With the second approach, you must always verify whether the sibling is "preceding" or "following" (in which case you would use following-sibling)

Answer №2

If you want to target the input element before the label with the text "Allow gender mismatch", make sure to include /preceding-sibling::input in your XPath expression. The updated XPath should look like this:

//div[@id="match_configuration_add_possible_tab_match_rules_fp_flags"]/span/label[contains(text(), "Allow gender mismatch")]/preceding-sibling::input

With this adjustment, you can accurately select the input element for further interaction.

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

Utilizing C# Selenium to Extract Information from a Table Nested Within a Div

Currently, I am facing a challenge in extracting data from a table consisting of two columns where the rows lack a unique identifier. Despite this limitation, I can utilize the information in the first column to determine if I need to extract data from the ...

Simulating mobile browser behavior using Capybara

I need to simulate mobile devices in my testing environment using rspec and capybara. I am aware that resizing windows and changing user-agents in Chrome is not enough, as each browser behaves differently beyond just resolution and user-agents. I want to ...

Incorporating a picture backdrop into a button element in a React Typescript component

I am working on a React project with TypeScript and using a Material UI library. I am trying to set a background image for a button, but when I use src or imageURL, it gives me a TypeScript error. The CSS style also does not show the picture. Here is my ...

Bootstrap 3 offset doesn't respond to dimensions

Even though I have set an offset for a medium-sized screen, the offset still appears in large screens. Here is a snippet of my HTML code. How can I resolve this issue? <div class="col-lg-6 col-sm-12"> <div class="row"> <div class="co ...

Is it possible to use jquery to specifically target classes?

I am trying to achieve the following: $('.class.img').css('cellpadding', variable); However, this code does not seem to be working as expected. Despite searching online, I have not been able to find a solution. Any assistance on how to ...

Tips for detecting if text appears in bold on a webpage using Selenium

I came across a pdf document with the following content: <div class=**"cs6C976429"** style="width:671px;height:18px;line-height:17px;margin-top:98px;margin-left:94px;position:absolute;text-align:left;vertical-align:top;"> <nobr ...

Selenium/c# facing issue with clicking Dynamics365 dropdown menu - getting "ElementNotVisibleException" error

Currently working on automating Dynamics365 CRM using Selenium/C#. I am able to successfully click and drop down the menu, however, I am facing issues finding and clicking on the "Submitted" option (line 2 of the code). The exception I am encountering is: ...

Encountering Issues with Selenium Firefox Webdriver's Get Function while Utilizing FirefoxProfile

Encountering a frustrating issue with the Selenium Firefox webdriver crashing when making calls to the "get" function. Interestingly, the error only arises when utilizing a firefox_profile; everything functions smoothly when firefox_profile is set to None. ...

Can you use "or" or "not" syntax with CSS input type selectors?

In the world of programming, if they actually exist, Suppose I have created an HTML form with the following input fields: <input type="text" /> <input type="password" /> <input type="checkbox" /> I wish to style all input fields that a ...

When combining CSS grids, nesting them can sometimes cause issues with the height layout

Check out the code on jsFiddle .component-container { width: 800px; height: 200px; background-color: lightyellow; border: 1px solid red; padding: 10px; overflow: hidden; } .component-container .grid-container-1 { display: grid; grid-tem ...

How to manipulate wrapping behavior in flexbox

My flex container holds three divs, arranged in two rows: First row: One div with a fixed width Another div that should stretch to fill the remaining space Second row: Just one item However, the first div stretches to 100% width and pushes the seco ...

Converting from CAPS lock to using the capitalize property in CSS

Is there a way to transform a sentence that is all in CAPs lock without changing it? This sentence is part of a paragraph, and I am looking for a CSS solution (maybe with a little Jquery) that works reliably across most devices! I have come across similar ...

CSS Element Overlapping Issue in Safari Browser

I am currently developing an audio player for my application that includes a pause/play button, next button, and back button. I have encountered a problem specifically on Safari where the back/pause buttons are overlapping each other. The play/pause button ...

Space between an image and a div element

While creating a website in Dreamweaver CC, I noticed a significant gap between my image and a div tag. Here is a screenshot for reference: Below is the code from my index file: <!doctype html> <html> ... </div> Additionally, here is a ...

Ensuring that a group of items adhere to a specific guideline using JavaScript promises

I need to search through a series of titles that follow the format: <div class='items'> * Some | Text * </div> or <div class='items'> * Some more | Text * </div> There are multiple blocks on the page wit ...

What is the best method for flipping the sequence of elements in media queries?

I am facing an issue with two divs, left and right, on my webpage. When the screen size is less than 500px, I want the left div to move to the bottom and the right div to move to the top. Essentially, I need to swap the positions of these divs in the DOM. ...

Adaptable arrow-shaped progress bar featuring sleek transparent borders

I am currently working on creating a progress bar similar to those commonly found in checkout processes. One issue I have encountered is that the borders between the arrows appear transparent, and the entire design needs to be responsive. So far, I have m ...

Getting started with Codeception may not always be straightforward: [PHPUnitFrameworkException] Error encountered - Element index not defined

After following the Codeception Quick Start instructions diligently, I proceeded to run the initial example test using PhpBrowser... # Codeception Test Suite Configuration # # [further comments omitted] # actor: AcceptanceTester modules: enabled: ...

Using Bootstrap 3 to create a carousel with a seamless fading transition as the background

I want to utilize Bootstrap Carousel as the background for my website. I've successfully incorporated a standard carousel as the background, but I'm encountering difficulties with making fade transitions. Here is my current implementation: HTML: ...

Implementing the rotation of an element using 'Transform: rotate' upon loading a webpage with the help of Jquery, Javascript, and

A div element designed to showcase a speedometer; <div class="outer"> <div class="needle" ></div> </div> The speedometer animates smoothly on hover; .outer:hover .needle { transform: rotate(313deg); transform-origin: ce ...