Each time the system boots up, a new Xpath ID is generated dynamically, making it difficult to pinpoint

Suppose you want to modify an element with no direct identifiers, but using a similar Xpath:

#js_n6 > div > ul > li:nth-child(4) > a > span > span

You may typically use it within document.querySelector(''), but there's a catch:

The #js_n6 ID is generated dynamically during each page load (when the webpage is created or refreshed):

During each page load, you receive a different ID like #js-x1, #js_v9, #js z4, #js k1 and so on, while the rest of the Xpath remains constant.

I attempted using a CSS wildcard such as [id^="js"] for the ID, but it doesn't work effectively as it also targets other elements with similar IDs found by the wildcard.


One example of this can be seen on Facebook.com's messages page. Visit the page with all conversations and execute the following in console:

document.querySelector('.5blh.4-0h').click();

Then, inspect the "Delete" button on the modal menu, and note the Xpath. Refresh the page and observe again to see the issue.

This is just an illustration; I'm not suggesting any scraping nor specifically referring to Facebook, but rather trying to understand a general concept.


In such scenarios, I am curious about how one can target an object without direct identifiers, especially when the Xpath includes a dynamic ID.

Notes:

  1. In such situations, I do not have control over the website and cannot add identifiers from the server side.

Answer №1

Utilizing a combination of selecting a parent element along with a CSS ID wildcard proved to be effective.

This is the general outline of my approach:

1) I targeted an element that serves as the parent for the element with the specific ID (.idElementParent). 2) Using a CSS ID wildcard, I selected the element with a dynamic ID. 3) Subsequently, I incorporated the relevant section of the Xpath.

document.querySelector('.idElementParent > [id^="js"] > div > ul > li:nth-child(4)').someMethod();

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

Experimenting with the inner workings of a method by utilizing vue-test-utils alongside Jest

Is there a way to properly test the internal logic of this method? For instance: async method () { this.isLoading = true; await this.GET_OFFERS(); this.isLoading = false; this.router.push("/somewhere"); } This method toggles isLoading, ...

Trigger $q manually in AngularJS

My understanding of $q in AngularJS is that it runs every time I refresh my page, similar to using a function in ng-init. Here is the code for $q.all that I have: $q.all([$scope.getCart(), $scope.getCategory(), $scope.getMenu(), $scope.getRestaurant()]). ...

Achieving a full-height div using CSS and HTML to fill the remaining space

Here is the current layout structure I am working with: div { border: 1px solid } <div id="col_1" style="float:left;width:150px;">1</div> <div id="col_2" style="float:left;width:100px;">2</div> <div id="col_3" style="float:l ...

Using the Tailwind CSS framework in combination with Vue's v-html

My vue component is designed to accept a prop of raw HTML, which originates from a wysiwyg editor utilizing tailwind classes for styling - similar to our vue app. The issue arises when using v-html="responseFromAPI" in my component, as the raw H ...

The async module has already been invoked with a callback function

Below is an array that I am working with: var files = [ { name: 'myfile.txt' }, { name: 'myfile2.txt' } ]; My goal is to access these objects asynchronously and send them for extraction, as shown below: Extraction function: ...

The Vue.js component is only refreshing after I manually refresh the browser page

As a newcomer to Vue.js and other reactive frameworks, I am still learning the ropes. I have a component that needs to update whenever there is a change. The goal is to display a balance from a specific login. <li :key="balance">Balance {{ balance ...

Form validation displaying only the initial error message

When validating a form, I am encountering an issue where only the first error message can be displayed: ContactUs.prototype.desktopErrors = function(){ var THIS = this; THIS.$el.find('#submit').on('click', function(){ ...

Transforming the header of a table in a CSV file into a schema field

My task is to create a schema for a CSV file using Mongoose, but upon inspecting the file, I discovered it contains nearly a hundred fields or columns. While I am familiar with defining schemas in Mongoose for files with only a few fields, I am unsure ho ...

Having trouble getting the libphonenumber npm package up and running, encountering an error stating that fs.readFileSync is not functioning properly

I am currently working on incorporating the googlei18n libphonenumber library for validating phone numbers. I have installed the npm package using npm i libphonenumber. However, when I try to use it like this: var libphonenumber = require('libphonenu ...

Struggling to make even the most basic example work with TypeScript and npm modules

After stumbling upon this repository that made using npm modules within a Typescript program look easy, I decided to give it a try by forking it and making some changes. My goal was to add another package to get a better understanding of the process. So, I ...

What sets apart .create from .save in mongoose?

A while ago, I completed a bootcamp course on Express and Mongoose on Udemy. In the course, we learned how to add new fields to data by writing code like this: var playground = require("../models/playground.js"); route.post("/", middleware.isLoggedIn,fun ...

AngularJS blank drop-down selection

I am currently working on an AngularJS select element with ng-options using the code below: <select ng-model="vm.selectedship" ng-change="vm.updateShip()" data-ng-options="ship as ('ship' + ' is ready (' + ship.currentlocation + & ...

How can I stop an element from losing focus?

One issue I'm facing is that when I have multiple elements with the tabindex attribute, they lose focus when I click on any area outside of them. The Problem - In traditional desktop applications, if an element is not able to receive focus, clicking ...

I require help with my digital greeting card

Apologies for any language errors in advance. I'm almost done with my first "online-card" project, but I've hit a frustrating issue. I can't seem to remove the gap between the footer and the tab-content (the Hungarian version is fine). I&apo ...

jQuery - Error: Unexpected token SyntaxError

I'm new to jQuery and facing a challenge with an AJAX request. I have a server on my local network running a Python service. When I make a request to the server through the browser using the URL: http://192.168.0.109:5000/api/environment?seconds=10 ...

Error is thrown when attempting to access nested elements using GetElementsByClassName within the window.onload function

I have a funky looking table on my webpage, here's an example: <body> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Product name</th> <th>Product ...

There seems to be an issue with Vue JS slots[name$1].every function as it is

I attempted to implement a custom isEmpty function for the Object prototype as follows: Object.prototype.isEmpty = function() { for (var key in this) { if (this.hasOwnProperty(key)) { return false } } return true } However, when tryin ...

The functionality of the Selenium Web driver (driver.find_element(By.XPATH, 'https//:xyz/asd')) in Python appears to be malfunctioning

I've been attempting to retrieve a simple text from the <h5> tag, but keep encountering a NoSuchElementException. Despite confirming that there is no <iframe> parent element, and even allowing time.sleep(20) seconds for the page to load co ...

watchWebpack is compiling all the files

As per the webpack documentation on watching files webpack can keep an eye on files and recompile them whenever there are changes. My understanding is that this implies webpack will only compile the files that have been modified. I have a configuratio ...

Tips for populating an array with information gathered from an axios request within a React application

Currently, I am using axios to fetch data from an API and attempting to store the retrieved data in a variable that represents an array within the state of the component. However, when I try to do so, I encounter the following error: Objects are not valid ...