Guide to executing Javascript instructions across multiple lines in Selenium Basic

Currently, I'm utilizing VBA Selenium Basic to parse through an HTML document in attempt to modify the attribute style within a td element. Despite my efforts, I have been unsuccessful in finding methods such as webelement.RemoveAtrr, webelement.SetAtrr, or similar alternatives.

After conducting extensive research on DuckDuckGo and Stack Overflow, it seems that Selenium Basic only allows for reading attributes, not altering or removing them directly. As a result, I turned to using JavaScript for this task.

Upon accessing the browser console, I executed the following lines of code:

var cell = document.querySelector('body > div > form:nth-child(5) > table > tbody > tr:nth-child(1) > td:nth-child(1)');
cell.removeAttribute('class');
cell.setAttribute('style','background-color: #00FF7F;';

The JavaScript successfully updated the page by discarding old class information and applying new style information. Encouraged by this success, I attempted to replicate the process in VBA with the following script:

oChrome.ExecuteScript "var sfCell = document.querySelector('body > div > form:nth-child(5) > table > tbody > tr:nth-child(1) > td:nth-child(1)');"
oChrome.ExecuteScript "sfCell.removeAttribute('class');"
oChrome.ExecuteScript "sfCell.setAttribute('style','background-color: #00FF7F;';"

Unfortunately, this approach did not yield the desired results. While the first line executes without errors, it fails to create the variable (as confirmed in the Console window). The second line triggers a JavaScript error instead of a Visual Basic one, stating:

sfCel.removeAttribute is not a function
.

If anyone has insights on how to execute multiline JavaScript instructions using ExecuteScript or alternative methods to remove and alter attributes without relying on JavaScript, any help would be greatly appreciated!

Thank you!

Answer №1

As stated on this page

"The script fragment will be executed within an anonymous function"

It's possible that your sfCell is not reaching the global scope.

Try assigning it using window.sfCell = ... to make it accessible globally.

Alternatively, remove the var keyword to place the variable in the global scope instead - check out this link.

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 ashx file is triggered with every server control postback in jqgrid

I have an asp .net webforms page with a jqgrid, as well as several other server controls. The jqgrid is populated using an ashx file which retrieves data from the database and binds it successfully. Challenge Whenever a postback occurs (such as from a dro ...

Leverage the power of Angular to seamlessly integrate jQuery code across your

Interested in incorporating the plugin into my AngularJS project. To achieve this, I have to: $(document).ready( function() { $("html").niceScroll(); } ); The challenge is figuring out where to place this code so that it runs site-wide and ...

Dynamically loading an AngularJS controller

I am faced with the challenge of integrating an Angular app with dynamically loaded controllers into an existing webpage. Below is a code snippet where I have attempted to achieve this based on my understanding of the API and some research: // Create mod ...

Is there a way to stop the Firebase web client from altering the error message generated by an https.onRequest function?

I am facing an issue where I cannot retrieve the original message I sent from an "https.onRequest" function in Firebase. The firebase client is rewriting the message based on the error code, making it difficult for me to recover the originally sent body or ...

html input type called array named AmountMap[1] is unable to be configured with <input type="textbox" size="15" name="amountMap[1]" value="0" >

**Having trouble setting the value of an HTML input type named like an array AmountMap[1] <input type="textbox" size="15" name="amountMap[1]" value="0" > When trying to set amountMap[1].value='10', ...

Guide to managing AutoComplete {onChange} in MUI version 5 with a personalized hook

Currently, I am utilizing a custom hook that manages the validation and handling of the onChange function. For most components like input, select, and textField, I have no trouble with handling the onChange event using the syntax below: The code snippet ...

Tips for utilizing the select feature within an ng-repeat loop while maintaining the selected value when fetching data from an API

I am currently facing an issue with using select inside ng-repeat. I am attempting to correctly map the value coming from my API to the select as the selected value. However, I seem to be missing something from my end. Can someone please help me identify a ...

Locating and interacting with elements on a fresh webpage using Selenium

Here's the scenario: Starting from the page "something/login.php", I click on a button that takes me to the home page "something/dashboard.php" When trying to interact with an element on the new page "something/dashboard.php", I am unable to locate ...

The URL routing in Vue-router seems to be incorrect as it directs to the wrong page. To fix this issue, make sure to include a '#' before the

Welcome to my website! Check out my home page here: https://i.sstatic.net/znfq0.jpg If you're interested in reading my blog, visit the blog page here: https://i.sstatic.net/oiYSY.png I've built this site using vue3. Here's a snippet of th ...

Sending a JSON array in an AJAX request results in receiving null values in an MVC application

I've been trying to send an array of JSON objects to my controller action, but it keeps showing up as null. Here's the JavaScript code I'm using: function UpdateSelected() { var items = {}; //Loop through grid / sub grids and crea ...

Manipulating data in a deeply nested field of an embedded document using Mongoose, MongoDB, and Express

I have been pondering whether it is feasible to input data into the comments field of my "TopicSchema": var mongoose = require('mongoose'); var TopicSchema = new mongoose.Schema({ username: String, topic: String, des ...

Traverse a tree structure of nested child objects in an Angular template using a JavaScript

Check out the Javascript object below: { "id": "1554038371930_ajhnms9ft", "name": "CPS", "nodes": [ { "id": "1554038905938_le34li2cg", "name": "Consumer Journey", "nodes": [ { ...

Using Vue.js, pull information from a database to populate input fields using a datalist feature

I'm currently working on a project utilizing Laravel 8 and Vue 3, and I have a Student Component. Within this component, there is a datalist that allows me to search for existing students. When I select a student from the list, I want the correspondin ...

React automatic scrolling

I am currently working on implementing lazy loading for the product list. I have created a simulated asynchronous request to the server. Users should be able to update the page by scrolling even when all items have been displayed. The issue arises when ...

What causes the modal to appear and then vanish suddenly?

I've created a simple modal code with minimal JS involved. When the button is clicked, the modal appears briefly, then the page refreshes and the modal disappears. I'm not sure what could be causing this issue. Any help would be appreciated! h ...

What is the best way to retrieve data in Nodejs from a different execution context?

My goal is to continuously fetch data from a website that updates frequently using MutateObservable. In order to achieve this, I am utilizing Puppeteer as my scraper since the data changes every second, requiring a browser to remain open constantly. For t ...

Executing memory function with JQuery replaceWith() and event handlers

Imagine you have the following HTML code: <button id="click-me" type="button">Click Me!</button> Now, picture this jQuery snippet being executed: var button = $('#click-me'); button.on('click', function() { console.l ...

Is there a way to change the background color of the body on click and have it applied throughout the entire site

Is there a way to change the background-color of the entire site and specific modal elements when a user switches between two radio buttons? Here is the HTML code for the radio buttons: <div class="btn-group btn-group-xs" data-toggle="buttons"> ...

Incorporating dynamic SVGs in real-time into a Vue.js application

I have been working on an old project that was originally built with messy jQuery code many years ago. I am now looking to upgrade it by incorporating a JavaScript framework for better scalability, state management, and overall cleaner, more maintainable c ...

Switching the link text in a Bootstrap navbar when hovered over

Is there a way to change the shortened text in my Bootstrap 4 navbar links to display the full text on hover? The full text is available in the title element and I would also like to add a small change animation for extra effect... Thank you <div class ...