Finding an element and applying a CSS class to it using Selenium in JavaScript

Is there a way to add a class to a specific element?

I attempted to use the classList.add method in JavaScript, but it didn't work 😞

await driver.findElement(By.css(".promotion-code-group.promo-shown .promo-selector")).classList.add("show");

TypeError: Cannot read property 'add' of undefined

I'm looking for guidance on using Selenium with JavaScript. Any help would be greatly appreciated. Thank you all in advance!

Answer â„–1

To update the class list of an element, you can utilize the execute_script() method.

When using setAttribute(), ensure to include both the current classes and the new ones as the second argument:

const targetElement = await driver.findElement(
   By.css(".update-me.current visible")
);

await driver.execute_script(
   "arguments[0].setAttribute('class', 'update-me current show')", 
   targetElement
);

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

Difficulties arising from displaying errors when submitting empty fields

I am facing an issue with my script - the problem arises when trying to display errors. The error message appears but does not function properly, causing a break after the submit button. Essentially, if the fields are empty, an error should be displayed, a ...

Having trouble converting TypeScript to JavaScript, and encountering difficulty locating the 'protractor' module

I'm facing an issue with compiling TypeScript to JavaScript because I can't find the module 'protractor'. Despite having Protractor, TypeScript, and Jasmine installed locally with their respective types. Here is the structure of my pro ...

Attempting to import a npm module that is not defined

I recently released an npm package. It is working perfectly when accessed via the global variable window.Router in the browser, but I'm facing issues when trying to import it using ES modules in a Meteor application. It keeps returning undefined... ...

How can you select all elements within a class except for the following element?

Here is my jQuery code snippet: $(function(){ $(".details").hide(); $("li").mouseenter(function(){ $(this).css("color","blue"); }); $("li").mouseleave(function(){ $(this).css("color","black"); }); $("li").click(func ...

Problem encountered when working with several child HTML elements while utilizing Directives in AngularJS

I've been using a template to create a popup menu that displays alerts, and so far it's been working well. However, I wanted to add a manual alert option by including an input text field. But to my surprise, the input field seems to be disabled f ...

Changing the default right-click functionality on a website with JavaScript

When using a web application, the default behavior is to display a context menu when right-clicking. However, in my case, I want to replace this default context menu with a custom one. Despite writing code to achieve this, both menus are currently being di ...

Showcasing local storage data in a textarea using React hooks

I am currently developing a note taking application using React. Successfully implemented state management with local storage. The objective is to display the 'notes' from local storage in the textarea upon rendering and refreshing. Currently, ...

Retrieve the information of the currently logged-in user on Discord using next-auth

Can anyone help me with extracting the banner and ID of the currently logged in user? Feel free to reach out at next@12 [email protected] I have successfully logged the profile details at the backend, but I'm facing issues pulling this informatio ...

Skipping is a common issue encountered when utilizing Bootstrap's Affix feature

I'm trying to implement Bootstraps Affix feature in a sticky subnav. <div class="container" data-spy="affix" data-offset-top="417" id="subnav"> I've adjusted the offset to ensure there's no "skip" or "jump" when the subnav sticks. Ho ...

Growler and ES6Code

My project uses a Grunt compiler along with jshint for validation. When working with multiline strings, I typically use the following syntax: string = '` Hello `'; However, upon compiling, I encounter the following error: [L328:C33] ...

Complete Form Validation Issue Unresolved by Jquery Validation Plugin

I'm facing an issue with the jQuery validation plugin while attempting to validate a form. Strangely, it only works for one input field. Can anyone suggest a solution? <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.valid ...

Tips for retrieving the most recent number dynamically in a separate component without needing to refresh the page

Utilizing both the Helloworld and New components, we aim to store a value in localStorage using the former and display it using the latter. Despite attempts to retrieve this data via computed properties, the need for manual refreshing persists. To explore ...

Implementing an Audio Notification System in PHP

When a certain value is violated, I want to display multiple alert boxes as well as an audio alert. Additionally, I need an option to close the audio alert. My current attempt looks like this: if( ($mark<=$minValue) || ($mark>= $maxValue)) { ...

Retrieve the attribute from the element that is in the active state

I'm facing a challenge in determining the active status of an element attribute. I attempted the following approach, but it incorrectly returned false even though the element had the attribute in an active state - (.c-banner.active is present) During ...

Accessing the background page of a Chrome extension while it's in operation

I am in the process of developing my first chrome extension that allows youtube.com/tv to run in the background so it can be accessed easily on a phone or tablet. Everything is working fine, except for the fact that if I want to watch the video and not j ...

Is nodemon waiting for file changes before restarting due to an app crash?

PS D:\CODE\2077\ONLINESHOP> npm run dev > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44372c2b3440">[email protected]</a> dev D:\CODE\2077\ONLINESHOP > SET NODE_ENV=DEVEL ...

Send a pair of values using a set of two parameters

My current code is currently passing only one parameter value. However, I would like to modify it to pass two values with two parameters. This is my current code: <script> function getfilter(str){ document.getElementById("result"). ...

Utilizing AJAX to integrate the Google Maps Direction API

Currently, I am developing a small website that utilizes the Google Maps API and AJAX to load directions based on selected locations from a dropdown menu. The database stores latitude and longitude coordinates of various locations, which are retrieved via ...

Establishing a PHP session variable and then initiating a redirect to a new page through the HREF method

I am working on a table that pulls IDs from a MySQL table named auctiontable and displays them in rows. Currently, I can easily append ?aid="1"or ?aid="2" to the end of the href attribute to utilize $_GET. However, I want to prevent users from controllin ...

Steps for coloring an image using pixel values retrieved from a database

My goal is to create a heat map by dividing an image I captured from Google Maps into a grid, which can be seen here: https://i.sstatic.net/uNv4e.jpg I want to assign colors to specific grid squares based on the number of people living in them. For exampl ...