Discovering the state of a checkbox element in Java and Selenium can be a challenge, especially when the element is not identified as a checkbox even with the

I'm currently creating test scenarios for the aviasales.com website and I am attempting to confirm the status of a checkbox.

Locating and clicking on the checkbox was simple using the following code:


WebElement checkboxValue = driver.findElement(By.xpath("//label[@class='mewtwo-show_hotels__label']"));
checkboxValue.click();

The issue arises when checking the state with

boolean bool  = checkboxValue.isSelected();
, as it consistently returns false even after being selected.

After researching, I came across suggestions to use JavaScript. I first attempted this in the console before implementing it in Java, yet encountered the same problem - always returning false:


var field = document.getElementsByClassName("mewtwo-show_hotels__label");
if (field[0].checked == true) {alert("1")} else {alert("2")}

Regardless of whether the checkbox is checked or not, I only received an alert with 2.

If Chrome's locator identifies the checkbox as ::before in the source code, what other approaches could I explore?

https://i.sstatic.net/518cp.png

Thank you!

Answer №1

To locate the checkbox element, you need to search for the actual element instead of using a label as you originally intended.

However, there are other elements to consider:
Input with the class - memtwo-hotels-checkbox and
Div with the class - memtwo-custom_checkbox_wrapper
There may be certain attributes associated with these elements that can help determine if the element is selected. If no such attribute is found, reach out to your DEV team to provide one.

Since you have a custom checkbox, standard functions may not work. Look for an attribute within the div that changes when the checkbox is checked or unchecked. Once you identify this special attribute, you will need to use Java to implement something like this:

if (element.getAttribute(MySpecialAttribute) != null) { //your code here }

I hope this explanation proves helpful.

Answer №2

Simply search for the checkbox element only

You should target the element with the class mewtwo-show_hotels__*checkbox* (not mewtwo-show_hotels__*label*)

Even though you see a label, it is actually designed to mirror the hidden checkbox's state

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 locating and interacting with elements in the Fields table across various Shadow DOMs with Selenium

I've been grappling with scraping data from a particular website using Selenium and Python. The site in question can be found at: . My target is to extract the information from the Fields table, however, I've hit a roadblock. Every attempt to ac ...

Differences between Regular Expressions in VBScript and Java

During the feasibility analysis of a GUI application, we are currently in the process of comparing QTP (VBScript) and Selenium (Java) tools. Selenium's advantage lies in being an open source tool which makes it cost-effective. The GUI application we a ...

Oops! An uncaught exception error occurred because the primordials were not defined

I used npm to install the package called aws-s3-zipper, but now I am encountering an error. This is the code snippet causing the issue: AWS = require("aws-sdk"); var S3Zipper = require("aws-s3-zipper"); function zipFolderOnS3() { var zipper = new S3 ...

Creating packing features specifically designed for resolution within a reusable module

I've decided to revamp my Angular application based on John Papa's style guide (well, mostly) and my main focus is on improving modularity. The stumbling block I've encountered is with route resolves. So far, I've been using a global ap ...

nuxt-link: take me to the identical position with the hash in the URL

I'm facing an issue with the <nuxt-link> component in my Nuxt application: The first time I click on the link, everything works perfectly and the page is changed as expected. However, if I scroll down a bit and try clicking the link again, noth ...

Dropzone JavaScript returns an 'undefined' error when attempting to add an 'error event'

When using Dropzone, there is a limit of 2 files that can be uploaded at once (maxFiles: 2). If the user attempts to drag and drop a third file into the Dropzone area, an error will be triggered. myDropzone.on("maxfilesexceeded", function(file){ a ...

What is the best way to transfer data between classes asynchronously?

Can someone guide me on how to achieve this? I am working with two views: ActivityMain.java FilmActivity In MainActivity, I have set up an intent to retrieve information from the second view: @Override protected void onPostExecute(Void aVoid) { ...

"Enhancing Your List: A Comprehensive Guide to Editing List Items with the Power of AJAX, jQuery, and

At the moment, I am able to edit a list item by clicking the 'Edit' link. However, I would prefer to simply click on the list item itself to initiate the editing process. This is the content of my _item.html.erb partial. In this case, each proj ...

Using React js to transform objects into arrays for useState

Hey there! I'm currently working on a webshop demo project for my portfolio. I've encountered an issue while trying to fetch data from commerce.js. The data is in the form of objects, which are causing problems when I try to map them. I've a ...

Class for making elements draggable using jQuery UI

Is it possible to use jQueryui's draggable/droppable combo and add a class to the dragged item when dropped into a specific container, rather than adding a class to the container itself? I've tried adding a class to the container, but that is not ...

When initiating the Grunt Express Server, it prompts an issue: Error: ENOENT - the file or directory 'static/test.json' cannot be found

I'm currently in the process of updating my app to utilize the Express Node.js library. As part of this update, I have made changes to my Grunt.js tasks to incorporate the grunt-express-server package. However, after running the server successfully, I ...

Leveraging Angular and Firebase to integrate data with iframes for various platforms such as

Currently, I am in the process of creating an application using Angular and Firebase as the backend. I have a question regarding the data section - is it possible to include iframe code in this section and then connect it to my view using ng-bind-html? Th ...

Tips for effectively parsing JSON information in Java

After spending some time coding today, I encountered an error that has left me puzzled and without a solution. The JSON data retrieved from a URL looks like this: {"server_stats": { "n_mobile": 1200, "n_tracking": 1200, "n_disclose_id": 717, "n_stealth": ...

TypeScript is unable to recognize files with the extension *.vue

Can someone assist me with an issue I'm facing in Vue where it's not detecting my Single File Components? Error message: ERROR in ./src/App.vue (./node_modules/ts-loader!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/Ap ...

Storing information in the DOM: Choosing between Element value and data attribute

When it comes to storing values in a DOM element, we have options like using the data attribute. For example, $("#abc").data("item", 1) can be used to store values and then retrieve them with $("#abc").data("item"). However, I recently discovered that th ...

Where is the typical location for selenium to be used?

I am feeling overwhelmed by selenium. As a newcomer to automated testing, I am finding it challenging to grasp the basics of selenium. http://www.seleniumhq.org/projects/webdriver/ Although I understand how to write tests and have some experience with P ...

Problems arise when using $(window).width() in conjunction with scrolling functionality

I am attempting to ensure this code only activates when the device window exceeds 960px and triggers when the window scrolls down 700px. The second condition is functioning as intended, but the first condition is not working properly. The code functions f ...

Automaton Framework - Selenium not required

I have recently installed selenium, but Robot is showing that it has not been installed. Can anyone offer assistance with this issue? Click here to view image ...

Tips for setting a variable using useState() and useEffect() with an asynchronous database call

I have a scenario where I am attempting to set a variable by making a simple GET request to the database. The issue I am facing is that even though the database call is returning the data correctly, the variable remains undefined after each re-render. Belo ...

What is the best way to determine if an HTML element reaches the bottom of the browser window?

In this scenario, my div is intersecting the bottom of the page. How can I use JavaScript to determine if this element is intersecting with it or not? For example, if bottom:0px; should not be intersected. .fixed{ position:fixed; bottom:-10px; le ...