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

Tips for styling custom buttons within the active editor popup of tinyMCE using CSS

Looking to enhance my tinyMCE active editor popup with custom CSS. Check out the screenshot provided for reference https://i.sstatic.net/vdVDF.png. Below you can find the code snippet used as a reference: tinymce.activeEditor.windowManager.open({ tit ...

Error encountered during execution of For-Each loop using VBA and Selenium?

I encountered an issue with the error message "Object doesn't support this property or method." Here is the code snippet that triggered the error: Dim webs, web As Object Set webs = driverg.findElementByCssSelector("*[class^='title-link id-trac ...

Error: Robot framework threw an AttributeError stating that the object of 'WebDriver' does not possess the attribute 'find_elements_by_xpath'

Whenever I try to run my code in Robot Framework, I keep encountering the error message "AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath'". I am using PyCharm as my IDE for this project. ...

Having trouble with `sendKeys()` function in Selenium Webdriver not sending all keys in Java?

While working with Selenium Webdriver in Java, I encountered a perplexing issue. Whenever I attempt to input text into a textfield, only the first character is sent and not the entire input. Even after trying to set it using JavaScriptExecutor, the problem ...

Issue: The system does not recognize 'cleancss' as an internal or external command

After successfully installing clean-css via npm command line, I was eager to start using it to concatenate and minify some css files. However, my excitement quickly turned to frustration when I encountered this error: 'cleancss' is not recognize ...

Tips for preventing the appearance of two horizontal scroll bars on Firefox

Greetings, I am having an issue with double horizontal scroll bars appearing in Firefox but not in Internet Explorer. When the content exceeds a certain limit, these scroll bars show up. Can someone please advise me on how to resolve this problem? Is ther ...

Customizing Bootstrap Modal's backdrop CSS for a specific element

Upon opening a bootstrap modal, the background takes on a darker shade as seen here. I am looking to apply a CSS class that mimics this shading effect to a specific element. I prefer not to use any JavaScript for this task as it is purely about styling wi ...

Tips on maintaining continuous clicking on an element while it is visible in SELENIUM

My goal is to continuously click on an element until it becomes invisible. This is my code snippet: try: element = WebDriverWait(self.browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='content']//form//div[2]//sp ...

Tips for anchoring a row to the bottom of a Bootstrap webpage

After working with bootstrap, I am looking to position the last row of the page at the bottom in a fixed and responsive size manner. ...

Storing and retrieving dynamic text in a Selenium WebDriver script using Java

Can anyone provide me with some guidance on how to perform these scripts? I appreciate any suggestions. In my application, I need to select employee country dropdown list, employee state dropdown list, and employee code dropdown list. I am able to sel ...

Struggling to Locate a Selenium Python Element

Hey there! I'm currently experimenting with selenium to locate and click on a button. Here's a snippet of the HTML code I've been working on: <input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()"> ...

Issue with verifying Apple SSO title using SpecFlow and Selenium

When working with Apple SSO, I have encountered an issue with the title containing a special character that causes my assertion for "Sign in with Apple" to fail. I have attempted using regex replace but it has proven ineffective. The failure message reads ...

Issue: In Firefox, resizing elements using position:absolute does not work as expected

My resizable div code looks like this: #box { height: 10rem; width: 10rem; resize: both; overflow: hidden; border: solid black 0.5rem; position: absolute; pointer-events: none; } <div id="item"></div> While it works perfectly ...

The icon for the weather on openweathermap is currently not displaying

Take a look at what my webpage looks like: http://prntscr.com/dg6dmm and also check out my codepen link: http://codepen.io/johnthorlby/pen/dOmaEr I am trying to extract the weather icon from the api call and display that icon (e.g. "02n") on the page base ...

Using a CSS button to activate a JavaScript function: A step-by-step guide

My current project involves writing a script to change the color of text when a specific button is clicked. The idea is that clicking the "Change color1" button triggers the text color change using the following code snippet: <button onclick="myFunction ...

Tips on organizing and designing buttons within a canvas

let canvas = document.getElementById("canvas"); let context = canvas.getContext("2d"); // for canvas size var window_width = window.innerWidth; var window_height = window.innerHeight; canvas.style.background="yellow" canvas.wid ...

Having issues with the addClass() method in JavaScript not functioning properly on hover

Coding Challenge <ul class="navBarExtended"> <li><a href="#">Link</a></li> </ul> Styling Solution .onHover{ text-decoration: underline; } Interactive Scripting $("ul.navBarExtended li a").hover( function() ...

Begin the execution of JMeter using a JUnit test

Can a recorded JMeter script, used to test the load of multiple user actions, be integrated into a Selenium/JUnit test case? I want to run the Selenium/JUnit test case with Java and receive performance results in the JUnit report, but most resources only ...

Seeking the parent class name in Selenium using ChromeDriver

Does anyone know how to find the parent class name of an element? I'm struggling to locate it. Check out the image below, which shows the element I'm trying to find the parent class name for. https://i.stack.imgur.com/hY1f8.png Any suggestions ...

Achieving horizontal alignment for divs in CSS can be challenging

Struggling to align three circular divs on my webpage, here's the code: <div class="row"> <div class="large-9 push-2 columns"> <div class="green"></div> <a href="#">Donnez</a> </div> <div cl ...