Tips for extracting CSS data with Selenium webdriver's executescript function

Just starting to learn Javascript in a Node environment...

I'm trying to use the code snippet below to extract CSS values for a specific web element, but I'm having trouble parsing it in JavaScript.

    driver.executeScript(script, ele).then(p => {
       console.log(p);            
    })

When this script is executed, the following output is displayed in the console:

{ class: 'container ng-scope', 'ng-controller': 'CalcCtrl' }

The result is of type Object and I'm struggling to access the value associated with the "class" key.

If I modify the code as follows:

driver.executeScript(script, ele).then(p => {
       console.log(p);            
       var obj = JSON.parse(p);
       console.log(obj.class);
    })

An error occurs with reference to column 27 during the execution of JSON.parse...

Error Screenshot

Answer №1

In response to the query posed, the solution is as follows:

    Utilizing the executeScript method in Selenium WebDriver, we interact with an element and capture the promise object it returns. We then proceed to check if the property "class" exists within the returned data, and if so, log its value.               
    })

When accessing a non-existent key in a JSON object using JavaScript, it typically results in undefined. To circumvent this issue, the conditional statement If(p.class) verifies the presence of the "class" key before displaying its corresponding value.

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

What is causing the 'AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file' error to appear in my code? I have included the full code in Pycharm/Pytest for reference

Despite checking similar questions, the provided answers do not address my specific issue. I am currently executing a test named 'test_HomePage' in Pycharm (Python 3.9) with Pytest as the test runner The code is as follows: Conftest.py: import ...

Issue with Jade not displaying external JSON data

I am struggling with a jade template that is supposed to display all possible solutions from the QPX Express search request: { kind: 'qpxExpress#tripsSearch', trips: { kind: 'qpxexpress#tripOptions', requestId: 'RwDOf6H ...

What is the reason behind Selenium's inability to locate XUL buttons?

I have been working on automating tests for a Firefox add-on using Selenium, specifically the insecure-links-highlighter add-on. However, I am facing an issue where it cannot locate the "Preferences" button in about:addons: extensionEntry.findElement(By.c ...

Asynchronously retrieving results in MongoDB

My task involves fetching all users from the users collection. app.post('/login', function(req,res,next){ users = self._db.get('users', {}) }) Below is the function in my database class: this.get = function( col, opt ) { ...

Ways to resolve the issue "Module Not Found Error: Cannot locate module './src/bot/index'"

Whenever I run the command node src/index.js I encounter the following error message: Error: Cannot find module './src/bot/index' Require stack: C:\Users\MIMAR\Desktop\EJS\src\index.js What could be causing this er ...

What is the best practice for adding one string to the end of another?

So, is this the best way to concatenate strings in JavaScript? var str = 'Hello'; str += ' World'; While most languages allow for this method of string concatenation, some consider it less efficient. Many programming languages offer a ...

How to simultaneously play a sound and display an alert message using JavaScript

My JavaScript code attempts to play a sound and then display an alert, but the alert always shows up first. When I click 'ok', the sound plays. How can I make it so that the sound plays before the alert appears? <script> function play() { ...

What is the process for sending an InMemoryUploadedFile to my S3 storage?

My upload form is quite simple and includes an image as a FileField: def post(request): if request.user.is_authenticated(): form_post = PostForm(request.POST or None, request.FILES or None) if form_post.is_valid(): inst ...

Exploring the functionalities of can-deactivate without incorporating routing

I'm facing an issue with a parent component and multiple child components. The parent component contains a form, while each child component also has its own form. Unfortunately, all child components share the same route as the parent component and hav ...

Attempting to incorporate icons into a Material UI table design

Hello, I've incorporated a Material UI table into one of my projects with a design concept similar to this - https://i.stack.imgur.com/i6Fsj.png I'm aiming to include icons within the tables. Here's the code I've worked on so far - ...

A step-by-step guide on how to implement a window scroll-controlled color transition

I've implemented jQuery code to change the opacity of my navbar's background as the user scrolls, transitioning from transparent to blue. Here's the snippet: $(window).scroll(function(){ var range = $(this).scrollTop(); var limit = 45 ...

invoking a JavaScript function from an HTML file

I want to create a JavaScript server on Raspbian that not only serves .html content to the browser but also allows for user input through events like button clicks. Both my .html and .js files are located in the same directory, and I have used absolute pat ...

Modifying dynamic input fields based on the selected input field type

Seeking advice on a challenge I'm facing while testing a website. My task is to mask input fields in screenshots after executing tests because we share data with other teams. I've tried using JS before the script by changing the input type to &ap ...

Having trouble with Javascript in getting one-page scroll navigation to work?

Hey there, I am working on creating a one-page scroll navigation with some basic javascript to add a smooth animation effect that takes 1 second as it scrolls to the desired section. However, I seem to be experiencing an issue where it's not functioni ...

Verify whether the user's email is registered in the database

I have a login form and I want to verify if a user's email exists in the database, but I'm not sure about the syntax for integrating Angular with Node. In my Main.js file, I have an ng-click on the submit button that triggers this function. I ex ...

Retrieve data from a text file using ajax and then return the string to an HTML document

Just starting out with ajax, I have a text file containing number values For example, in ids.txt, 12345 maps to 54321 12345,54321 23456,65432 34567,76543 45678,87654 56789,98765 Here is the Html code snippet I am using <html><body> < ...

Ways to showcase INPUT TYPE when making a Selection?

I've been struggling with a simple issue and despite trying multiple solutions, I can't seem to get it right. I have a form where I'm using the <select> tag with two options: coo and uh. What I want is for an additional input type fiel ...

Issues arising from TypeScript error regarding the absence of a property on an object

Having a STEPS_CONFIG object that contains various steps with different properties, including defaultValues, I encountered an issue while trying to access the defaultValues property from the currentStep object in TypeScript. The error message indicated tha ...

Sending data as a string in an AJAX request

I have been struggling with this coffeescript function that controls dynamic select boxes. I am trying to pass the content of "modelsSelect" to another script, but for some reason, it's not working as intended. customScript.coffee dynamicSelection = ...

The agent.add() function is malfunctioning, while console.log() is functioning properly

I'm currently working on integrating a bot using Telegram, Dialogflow, and Firebase. One particular function that I'm struggling with is as follows: function findDoc(agent){ const userId = request.body.originalDetectIntentRequest.payload.dat ...