Retrieve the JavaScript names employed by the webpage through Java Selenium WebDriver

I'm currently working on automating a website and I need to identify which JavaScript files are being called for specific URLs. Is there a way to retrieve the names of JS files that are used or called by a webpage? Typically, I am looking for the names of JS files listed under Developers Tool > Network > JS tab. You can see an example image of what I want to capture here (Sample image of Stackoverflow homepage).

I have attempted to achieve this using the following code:

DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://stackoverflow.com");
LogEntries logs = driver.manage().logs().get(LogType.SERVER);
for (LogEntry log : logs) {
     System.out.println(log.getMessage());
}

However, this code provides more details than needed. Is there a way to extract only the names of the JavaScript files?

Answer №1

If you're looking to retrieve data from the Resource Timing API using JavaScript, you can achieve this easily. Take a look at the example snippet I've provided below:

ArrayList<String> results = (ArrayList<String>) ((JavascriptExecutor)driver).executeScript(
"return window.performance.getEntries().filter(e=>e.initiatorType==='script').map(item=>item.name);");
results.forEach((url)->System.out.println(url));

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

Testing a chrome problem using chromedriver

from selenium import webdriver from selenium.webdriver.chrome.service import Service driver_path = r"C:\Users\hp\Desktop\Udemy\chromedriver.exe" # Ensure the path is correct # Creating a ChromeDriver service object serv ...

Using an id as the attribute value for a React ref

I have a question about referencing DOM nodes in a component. Currently, I am able to get the nodes and children using the following code: export class AutoScrollTarget extends React.Component { constructor(props) { super(props); this ...

infinite cursor function in Java for Mongo DB

Having trouble with an aggregate query on a document that is returning only 500 results even when using various limits such as limit(0), limit(10000), or limit(20000). The goal is to retrieve more than the limited number of results, but so far it has not ...

Issue with adding events using .on() to dynamically added items in datatables when using ajax based fnDraw()

Whenever the checkbox is selected, a datatable retrieves fresh data using ajax and fnDraw(). Unfortunately, the .on() event is failing to add the event properly. $("#reviewcheck").click(function() { reviewTable.fnDraw(); }); $(".review tbody td img").o ...

Navigating through a fresh batch of retrieved elements

Presently I have: two separate lists containing unused and used elements. My task is to transfer specific elements from the first list to the second list (an element can be transferred by either double-clicking on it or clicking on it while pressing a butt ...

Is JSON required to transmit an object using socket.io?

I have an object on the frontend that I want to broadcast to all connected clients. Can I send it as is, in its original form? Or do I always have to convert it into a JSON string before sending? Here is my object: var myBox = { x: 400, ...

Troubleshooting Jqgrid Keyboard Navigation Problem

Here is a link to the jsfiddle code snippet. Upon adding jQuery("#grid").jqGrid('sortableRows'); into my JavaScript code, I encountered an issue where keyboard navigation no longer worked after sorting rows Below is the JavaScript code snippet: ...

Is it possible to execute MySQL queries in the form of Javascript using Reactjs?

I'm currently working on a database project that involves using Javascript with React, Node, and MySQL. I have been trying to figure out if there's a way to execute MySQL queries through a form submission. For example, being able to type in a que ...

What causes an error when calling a method inside onSubmit?

Whenever I attempt to invoke the metetor method, I encounter an error stating "Uncaught TypeError: Meteor.call is not a function". Strangely enough, when I make the same call in a different file located at imports/api/some.js, it works perfectly fine. This ...

Storing property data outside of the render method in ReactJS is key for efficient

I have encountered an issue while attempting to map data outside of the render method in my function and return it within the render. The mapping error is causing confusion as I am uncertain about its underlying cause. Below is the function responsible fo ...

The dropdown menu in Mantine is malfunctioning, even though I copied and pasted the exact code from

While following a tutorial, I encountered an issue with the Mantine Menu and Dropdown components. The tutorial creator did not wrap the React App inside the MantineProvider, which resulted in errors when trying to use the Dropdown component. Even after add ...

What is the best way to create a universal variable that can be accessed across all routes in an Express/

Exploring the world of nodejs and express, I have turned to the Parse API for my backend database needs. At the moment, I have an ajax post triggered on page load to one of my routers /getuser, which retrieves the current user if they are logged in. I am ...

Looping through data and converting it into a JSON format can

Can anyone help me figure out how to work through this JSON data using VUE? I'm having trouble accessing keys that v-for can't seem to reach: [ { "Lavandería": { "id": 1, "name": "Lavandería", "i ...

Guide on utilizing Chrome driver with head in Scrapy Selenium

Currently, I am using Scrapy with scrapySelenium. However, the scrapy selenium module initiates a headless browser by default. For certain purposes, I need to disable the headless mode so that I can visually observe what my Scrapy code is doing on the brow ...

special InputFormat for hadoop

Seeking assistance, I need help working with the following sample data: -21.33,45.677,1234,1245,1267,1290,1212,1111,10000,1902 -21.34,45.677,1264,1645,1266,1260,1612,1611,16000,1602 -21.35,45.677,1244,1445,1467,1240,1242,1211,11000,1912 -21.36,45.677,1231 ...

Tips for avoiding XMLHttpRequest from buffering the complete response

Trying to implement an ajax call to a streaming endpoint on my server. Need the connection to accept continuous pushed data from the server, but XMLHttpRequest seems to buffer the entire response. Want the browser client to receive each chunk of data once ...

Issue with event delegation when using if-else conditions is not being resolved

I have set up an event listener on an HTML container, and when the user clicks on the play again button, the code inside the 'if' statement should be executed. However, nothing happens when clicking on the play again button and no logs are output ...

Change the code from a for-loop to use the Array#map method

I have been working on a simple JavaScript function and attempting to convert the code from using a for-loop to Array#map. I'm sharing my code in the following fiddle as I am currently learning about array map: http://jsfiddle.net/newtdms2/. function ...

The correct method for declaring services and factories within AngularJS

After reading multiple tutorials on AngularJS, it became evident that there are various approaches to defining a service. I am now curious about the best practices and potential drawbacks associated with each method. The initial difference I observed rela ...

Tips for animating input width as the size value changes during an ajax query transformation

This is my JavaScript code: function updatePriceDisplay() { $.ajax({ url:"query.php?currency=<?=$currencycode;?>" }).done(function(data) { $("value").attr("value", data).attr("size", data.length - 2); }); } updatePriceDi ...