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.

https://i.sstatic.net/gwY8t.png

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

Issue encountered: ENOENT error - The specified file or directory does not exist. This error occurred while attempting to access a directory using the

I don't have much experience with nodejs or express, but I have an API running on http://localhost:3000. One of the endpoints triggers a function that uses the file system to read a file synchronously. However, when I send a post request using Postman ...

Strategies for locating elements with the href attribute in Selenium using C#

Having trouble selecting a link using the href attribute. <a id="1 - GovSearch" name="1 - GovSearch" title="Population and Immigration Authority" href="https://www.gov.il/en/Departments/population_and_immigration_authority" class="content-title first ...

Centering the scrollIntoView feature on mobile devices is presenting challenges with NextJS applications

Description While navigating on mobile browsers, I'm facing a challenge with keeping an element centered as I scroll due to the browser window minimizing. I've experimented with different solutions such as utilizing the react-scroll library and ...

Utilize the controller data to display information on a day-by-day basis

In my attempt to design a weekly calendar using AngularJS, I am facing challenges when it comes to inserting events into the 7 boxes representing each day of the week on the calendar. The current HTML structure is as follows: <div class="week"> ...

Function compilation did not succeed in the module

I've put together a MERN (MongoDB, ExpressJS, React, Node) project using express-generator (express myNewApp). Within a react component, I have implemented this ES6 code snippet: onChange = (event, { newValue }) => { // Line 53 this.setSt ...

Why isn't the correct component being shown by react-router?

I have encountered an issue with my code. When I type localhost:8080 in the browser, it displays the App.js component as expected. However, upon navigating to localhost:8080/#/hello, it still shows the App.js component instead of hello.js. Interestingly, ...

Conceal and reveal feature for multiple dynamic identifiers simultaneously

After submitting the form, I am dynamically creating buttons. <template name="workflow"> {{#each newaction}} <div class="btn-box" > {{> actioncardsubcontent}} <button type="button" class="cancelsub" >New Action</button&g ...

Steps for eliminating double quotes from the start and end of a numbered value within a list

I currently have the following data: let str = "1,2,3,4" My goal is to convert it into an array like this: arr = [1,2,3,4] ...

Is tsconfig.json Utilized by Gatsby When Using Typescript?

Many blog posts and the example on Gatsby JS's website demonstrate the use of a tsconfig.json file alongside the gatsby-plugin-typescript for TypeScript support in Gatsby. However, it seems like the tsconfig.json file is not actually utilized for conf ...

The read_html() function in Pandas is not displaying all the entries in the table

I'm working on a web scraping project where I need to log into a secure website, find and extract data from a dynamic table that updates frequently. The webpage layout can be viewed here: The specific table I'm interested in is the RANKING table ...

Conceal / reveal with jQuery

I am attempting to hide all divs and only display those connected with the "button" class. However, I'm encountering an issue where only the last div is visible, regardless of which div/button was clicked. EDIT: Here is the complete HTML code: < ...

The element is implicitly assigned an 'any' type due to the fact that an expression of type 'any' cannot be used to index types in nodejs and solidity

I am in need of setting networks in my contract using NodeJS and TypeScript. Below is the code I have written: let networkId: any = await global.web3.eth.net.getId(); let tetherData = await Tether.networks[networkId]; Unfortunately, I encountered ...

I'm using Python with Webbot and I'm not quite sure how to activate the button on the Chrome PDF viewer

Currently, I have a Python application that is utilizing Webbot to navigate through a website. One of the final pages of this process involves rendering and streaming a PDF document to the browser without an endpoint URL. The document appears in the Chrome ...

Dynamic content loading for Twitter Bootstrap Popover using Ajax

Below is my code for dynamic content loading in a popover using AJAX. The popover displays correctly after the initial load, but there is an issue with its behavior on the first "show" event – it appears and disappears immediately. $("a.mypopover"). ...

Understanding the reverse order of numbers displayed with while loop and innerHTML

function doItAgain() { var loopCount = 5; while(loopCount > 0) { var target = document.getElementById("target"); target.innerHTML = "LoopCount: " + loopCount + "& ...

Issues encountered when using the findElement function for web scraping with Selenium in Python

Currently, I am studying building restoration and have been learning the process of web scraping. My current project involves gathering data from churches located in Spain, specifically using the Catastro website to extract information. However, I have enc ...

Attaching an input text box to an Angular $scope is not possible

In my angular app, I have a piece of markup that showcases a table with saved queries. Each query has the option to add a title using an input field. When you click the edit icon, it should display the newly created title. <table class="table table-str ...

Tips for customizing the main select all checkbox in Material-UI React data grid

Utilizing a data grid with multiple selection in Material UI React, I have styled the headings with a dark background color and light text color. To maintain consistency, I also want to apply the same styling to the select all checkbox at the top. Althou ...

Arranging elements in an array based on two different properties

Trying to organize an array of elements (orders details). https://i.stack.imgur.com/T2DQe.png [{"id":"myid","base":{"brands":["KI", "SA"],"country":"BG","status":&qu ...

Step-by-step guide for setting up chartjs on Laravel 6 using npm

Can anyone guide me on how to properly install and integrate [email protected] in my laravel application? Currently, I am using cdn links, but I want to avoid that in the future. List of cdn links being used: <script src="https://cdnjs.c ...