Obtain a unique HTML ID dynamically with Selenium and VBA

I am currently working on automating a web form using Selenium and VBA. The form includes a search box that generates an autogenerated list when a value is entered. While I can successfully input a value into the search box and trigger the javascript to create the list, the issue arises with the dynamic ID value assigned to the list each time. I believe utilizing Xpath search may be the solution to obtaining the ID, but I'm struggling to implement it accurately.

The following code snippet executes the autocomplete feature to generate a dynamic list with "121312" as the search value, limiting the list to contain only one item. Therefore, we need to extract the value at the 0 index from the auto-generated list.

$('.ui-autocomplete-input').slice(20,21).val("121312").autocomplete('search')

This code correctly populates the cell with the value from the autogenerated list. However, the challenge lies in handling the changing number after "ui-id-". This number varies each time and needs to be dynamically found.

$('#ui-id-6.ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front').children().first().click()

Referencing the HTML inspection below where the ID has been highlighted in blue, it is evident that everything except the number remains constant. I've been grappling with this issue for several days now, so any assistance would be greatly appreciated!

Inspection of HTML

I suspect that leveraging Xpath could automatically detect the value to be clicked. However, I am not well-versed in Xpath, and copying the Xpath directly results in a specific ID value that changes every time.

Here are two examples of Xpath copied from inspection: `/html/body/ul[21]/li[14]/div //*[@id="ui-id-1855"] //*[@id="ui-id-563"] /html/body/ul[31]`

UPDATE

Following Michael M's suggestion, I have observed some intriguing outcomes.

It was necessary to include the ID search at the beginning of the CSS selector.

$('[id^="ui-id-"].ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front').children().first().click()
.

Although this approach works, it seems to only function on the first entry box, which puzzles me.

https://i.sstatic.net/4oJBo.png

The initial entry box operated flawlessly. The search initiated, allowing me to enter the desired value into the box using

`$('[id^="ui-id-"].ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front').children().first().click()`
Subsequently, trying another box further down did not yield any results.

Image illustrating the executed JavaScript https://i.sstatic.net/yQWFt.png

The first two lines performed perfectly, while the third line represents the search on the second box. Surprisingly, the fourth line failed to input the searched value, leaving me puzzled.

Answer №1

To modify your CSS selector so that it only targets the beginning of the ID, you can use [id^="ui-id-"] (If needed, you can refer to all the guidelines for matching attributes like this here on MDN). This will make sure that only the initial part of the ID is selected while ignoring the rest. Here's an example:

$('ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front[id^="ui-id-"]')

Answer №2

After trying different methods, I ultimately decided to utilize Selenium sendKeys for sending a down arrow, tab, followed by an enter command post search execution. This approach proved to be much simpler compared to hunting for the dynamically generated ID repeatedly.

A shoutout to Michael M for providing the response!

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

Send JavaScript variable using a query parameter

I am working on passing a JavaScript variable across all pages in my project. The goal is to calculate the total value from various pages and display it at the end of navigation. Below is an example of my JS code: <script> $(document).ready ...

Drop-down selection triggering horizontal auto-scroll

Within my div element, I have a table with dropdowns. The div is set up to be horizontally scrollable. To create the dropdown functionality, I utilized Harvesthq Chosen. Let me provide some context. In Image 1, you'll notice that I've scrolled ...

Disable the ability to select text when double-clicking

Is there a way to prevent text selection on double click while still allowing selection on mouse drag? Whenever I try to remove selection on dblclick or mouseup, it flashes, which is not the desired outcome as shown in this jsfiddle. UPD: I am not lookin ...

A web application using JavaScript and Node.js with a focus on creating a REST

After extensive research, I am eager to develop a web application using JavaScript and Node.js with an SQL back-end. However, the abundance of frameworks and tools available has left me feeling overwhelmed. While I have identified some that I would like to ...

Should the page.js in a Next.js App router be a server component?

Is it standard practice in Next.js 13 and above (App router) for the page.js of all routes/folders to be a server component? I know that it can be a client component with use client, but is this advisable or are there potential issues during the build proc ...

TabPanel with Grid component causes Error: The server-rendered UI does not match the initial UI, resulting in failed hydration

After completing all the necessary steps and transferring all the files from nextjs-with-typescript, everything seemed to be in order until I tried adding Grid inside the TabPanel in the code snippet below: import * as React from 'react'; Tabs fr ...

What's the trick to efficiently managing multiple checkboxes in an array state?

I have a lengthy collection of checkboxes that is not well optimized. I am trying to obtain the checked ones and store them in an array state. However, I am uncertain about how to handle this situation and I would appreciate some assistance. It should also ...

How can you declare variables in CSS using LESS?

I am facing a challenge where I need to dynamically change the branding color and other styling variables on the portal based on certain conditions at runtime. I have successfully implemented this functionality using CSS with the code snippet provided be ...

Is there a way to transfer a significant volume of data from one webpage to another without relying on the POST

Currently, I am utilizing a web server framework that exclusively operates with GET requests. Presently, my task involves transferring a substantial volume of data (specifically the text content in a textarea) inputted by users onto another page where it i ...

Having trouble grasping the error message "Uncaught Typerror Cannot Read Property of 0 Undefinded"?

As I embark on creating my very first ReactJS website with Node in the back-end, I encountered an issue in fetching and printing data. While I successfully displayed the names, pictures, and emails of project members from the server, I faced an error when ...

I'm having trouble with fixing the TypeError that says "Cannot read properties of undefined (reading 'style')." How should I address this issue

I'm having trouble with a slideshow carousel on my website. When the site loads, only the first image is shown and you have to either click the corresponding circle or cycle through all the images using the arrows for the image to display. I'm al ...

Expanding list - Using individual toggles to expand and collapse multiple list items

I stumbled upon an interesting jquery script that allows for a collapsible sub menu within a list. After implementing this code: $('.menu-item-has-children').prepend('<a id="fa-menu" href="#"><i class="fa fa-plus"></i>< ...

How can one extract information from an ASP.NET MVC on a separate webpage?

Experimenting with ASP.NET MVC for the first time here, so bear with me if this comes across as textbook-like. A basic content management system has been developed using ASP.NET MVC. The URL to retrieve a list of content, specifically announcements, looks ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Are you logging in with the IWebDriver and providing your credentials?

For my website, I need to input some credentials: https://i.sstatic.net/Pfion.png Is it possible to use selenium IWebDriver to enter the credentials? ...

How can I maintain the selected state of checkbox and radio button in ReactJS after page refresh?

I am facing an issue with my multistep form that contains radio buttons and checkboxes from material UI. Although the data is stored correctly in state and local storage, when I move to the next page and then return, the selections appear unselected to the ...

mysql nodejs function is returning a null value

Kindly review the contents of the dbfn.js file /*This is the database function file*/ var db = require('./connection'); function checkConnection(){ if(db){ console.log('We are connected to the Database server'.bgGreen); ...

Is the required attribute for form submission in Material UI not verifying in another file?

It seems that I may be importing incorrectly because a required field does not display a "please fill in this field" popover when attempting to submit a form. The imported classes are as follows: import * as React from 'react' import FormContro ...

Ways to conceal a text-filled div behind an image using 3D transformation

I've been working on creating three images with a fun 'flipcard' effect for the past day and a half, and I believe I'm getting closer to achieving my goal. However, there is one issue remaining, as you can see in the codepen. When the ...

Sorting of tables does not function properly following an ajax request

I built a table that utilizes an AJAX response triggered by selecting a date from a drop-down menu. <!--datepicker--> <div class="col-md-4"> <input type="text" name="date_po_month_picker" id="date_po_month_picker" class ...