The CSS selector functions as expected when used in a web browser, however, it

While conducting test automation using Selenium, I typically rely on css selectors to find elements. However, I recently came across a peculiar issue. I observed that in certain cases, the css selector works perfectly when tested in the browser console. For instance, to select a specific button among several, I use:

(1) $("div.v-app[id^='v-discussions'] .v-button-caption:contains('Add')")

But when I attempt to use it in Selenium, it throws an error:

Caused by: org.openqa.selenium.InvalidSelectorException: The provided selector div.v-app[id^='v-discussions'] .v-button-caption:contains('Add') is either invalid or does not point to a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal selector was specified

I also tried the same string with querySelector, and it resulted in an "Illegal string" error:

(2) document.querySelectorAll("div.v-app[id^='v-discussions'] .v-button-caption:contains('Add')")

Why do you think this might be happening?

P.S.

This is how each element looks like:

<span class="v-button-caption">Add Comment</span>

Answer №1

Here's a solution:

WebElement buttonElement = driver.findElement(By.className("v-button-caption"));

This method has been effective for me in my use of Selenium Webdriver.

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

The static menu is malfunctioning and is disrupting the smooth operation of the website

I've created a custom Wordpress menu that should switch to a different layout when scrolling down. While the functionality works fine, I'm facing an issue where a portion of the page is lost every time the menu transitions from "relative" to fixe ...

Determine the quantity within the data array using javascript

My dataset contains an array with thousands of objects structured like this: var data = [ {type:'A',time:'2009'}, {type:'A',time:'2009'}, {type:'A',time:'2009'}, {type:'A',time: ...

The framework for structuring web applications using Javascript programming

While I have extensive experience in PHP and C# programming, my knowledge of Javascript is limited. When it comes to server side programming, MVC is my preferred method as it allows me to keep my code well-organized. However, when it comes to writing Java ...

Hover animation using CSS is a great way to add interactivity

I'm trying to create an animation in a moving box that flips upside down when the mouse is raised. I've attempted to use keyframes and hover to achieve this effect, but it's not working as expected. Any suggestions on how to make it work? ...

Retrieve the data in the format of [1,2,3] from a MySQL database by employing PHP and jQuery

I'm currently working on a jQuery code that connects to getdata.php. The val in this code is a dynamic value and the function gets called every time an option is selected from a dropdown. function getMoleculeData(val){ var molval=val; ...

Using jQuery to show/hide linked CSS3 animations upon Mouseenter/Mouseleave events

Exploring the capabilities of animate.css and jQuery within a bootstrap environment has been quite interesting for me. However, I've encountered a major issue! I am attempting to trigger animations on mouseenter / mouseleave, which led me to create a ...

Stopping autoplay in React Swiper when hovering over it

I'm currently struggling to find a way to pause the autoplay function on swiper when hovering over it. Despite my efforts, I have not been able to locate a solution. <Swiper spaceBetween={0} navigation={{ ...

I'm looking to integrate Jest in my React project with npm. How can I achieve

I've developed my application with create react app and npm. While reviewing the official documentation for jest at https://jestjs.io/docs/tutorial-react, I noticed that they only provide information on testing CRA apps using yarn. Does this suggest t ...

Thinking of hosting an event centered around Google Maps?

Are there specific event listeners for panning or zooming the Google Map, in addition to the mouseover, mouseout, and click events? UPDATE: I tried using 'center_changed', but it didn't work the way I expected. Whenever I move the mouse ov ...

What is the best way to effectively use combinedLatestWith?

https://stackblitz.com/edit/angular-ivy-s2ujmr?file=src/app/country-card/country-card.component.html I am currently working on implementing a search bar in Angular that filters the "countries$" Observable based on user input. My approach involves creatin ...

Implementing a time delay for submitting an ajax form

I am looking for a way to delay the submission of a form by 2 seconds. I have tried different solutions, but none seem to work for me. I attempted using setTimeout to set a delay, but it is not working as expected. Here is my script and the form tag - wh ...

Error: The variable `$` has not been defined in your Vue.js webpack project

Encountered an error in the browser console while running a Vue.js webpack project. The error occurred due to minimized js files within core.min.js. Specifically, the jQuery minimized js is included inside core.min.js. To access the code of core.min.js, ...

Using the Ruby on Rails link_to tag along with a mouseenter jQuery function allows for dynamic

How can I make an image display in a div when the mouse hovers over a link_to using Rails? The code below seems to work, but I'm unsure of how it functions. <a href="#" class="nav-link roll"> <%= image_tag("chair1.jpg") %>Chesterfield Cha ...

How to center a container in HTML using Bootstrap techniques

Here is the code I have been working on: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/> <div class="span4 offset4 centered"> <div class="container overflow-hidden"> < ...

Difficulty encountered in resetting progress bar post ajax form submission

Hello, I hope you can assist me with an issue I am facing regarding my progress bar. After submitting the form using AJAX to insert data, my progress bar does not reset. I would like it to reset after clicking the submit button. The progress bar is speci ...

What's the deal with ajax constantly failing to deliver results?

Here is the ajax function that I have been working with: $.post({ url: "login", data: { nomutilisateur: nomutilisateur, motdepasseutilisateur: motdepasseutilisateur } }).done(function() { console.log("Success"); }).f ...

Switch the style of a set of thumbnail images when clicked

I am working with a set of thumbnails where one has an "p7_current" class applied, giving it a border, while the rest have an "p7_inactive" class removing the border. My goal is to have the last clicked thumbnail in a group of 6 to have the "p7_current" c ...

Add a border to the 'contenteditable' class in a Bootstrap table after it has been modified

Looking for help with JavaScript and jQuery! I have a script that creates an HTML table from a CSV file, using Bootstrap for styling. I want to add CSS or a border to a table cell after it has been edited, but my jQuery attempts haven't worked. Any su ...

Can we disable hydration warnings for all nested children within a component in NextJS?

Note: I am currently using NextJS 14, but I suspect this issue also exists in NextJS 13. I have created a ThemeToggle component that utilizes the next-themes package. Even if I develop my own version of next-themes using React Context or another state man ...

How to get the most out of the $scope variable?

Is it possible to assign a regular JavaScript variable the current value of an Angular $scope variable without having their values binded together? //$scope.var1 is set to a specific value, for example, 5 var v2 = $scope.var1; //$scope.var1 is then update ...