Storing a link's id in JavaScript - the essential guide

Currently, I am exploring ways to utilize the selenium IDE in order to retrieve, save, and apply the ID of a specific record.

The HTML code in question is as follows:

<span class='small'><a href="/users/change_district/31">(Select)</a></span>

My objective is to extract the 31 section from the snippet above and assign it to a variable named myVar.

According to the guidance provided for selenium usage, the recommended approach involves incorporating JavaScript. Thus, the suggested step is to employ the following script:

store | javascript{...} | myVar

The key query that arises is: What should be included within the {...} segment of the JavaScript code?

Answer №1

To retrieve the desired information, you can employ a blend of XPath and JavaScript. Here is an example:

storeAttribute | //*[@class='small']/a[text()='(Select)']/@href        | hrefValue
store          | javascript{storedVars['hrefValue'].replace(/.*\//,'')} | myVariable
echo           | ${myVariable}

Result:

18

Answer №2

saveAttribute css=a[href*='users/update_location']@href URL

verifyEval javascript:{storedVars['URL'] == '/users/update_location/31'} true

I would appreciate your feedback on whether the script provided above is functioning correctly or not.

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

A numeric input area that only accepts decimal numbers, with the ability to delete and use the back

I have successfully implemented a code for decimal numbers with only two digits after the decimal point. Now, I am looking to enhance the code in the following ways: Allow users to use backspace and delete keys. Create a dynamic code that does not rely o ...

Which specific page initiates the post request?

Seeking help with my post request that originates from a specific page on my website: reqdata = 'text=' + mytext; $.ajax({ type: "POST", url: "/request.php", data: reqdata, cache: false, success: function(html) { alert(html) } ...

What is the best way to trigger a function on every navigation event in a React Native application?

Here is my scenario: We are currently working on adding an inactivity timeout feature to a react native app. The goal is to have the timeout reset every time a user interacts with the app or navigates between screens. At the moment, we have a callback fu ...

The issue of empty req.body in POST middleware with Medusa.JS

Feeling completely frustrated with this issue. Grateful in advance to anyone who can lend a hand. Any insights on why req.body is showing up empty? Medusa.JS should be utilizing bodyParser by default, correct? It was functioning fine earlier today but now ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

What's the best way to handle variables and numerical calculations within PHP loops when dealing with forms?

I'm currently developing a basic PHP page where users can choose how many numbers they want to add together. After selecting the quantity, they input the numbers on a new page, click a button, and then see the sum displayed. The challenge I'm fa ...

Submitting data to an external PHP file using the JavaScript function $.post

Having issues with the PHP page function ajaxFunction(){ var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequ ...

Having difficulty with Python selenium to automatically log in to a website

I have written a Python script to connect to a website automatically, but I am encountering an error. The code I am using is: from selenium import webdriver from getpass import getpass username = input("Enter your CUID:") password = getpass("Enter your C ...

When the button is clicked, redirect to a new page and submit a form using AJAX with data obtained from the click event

I have a page called 2.html where inputting an ID number results in some output being displayed. This functionality is achieved using Ajax post method, sending data along with the data parameter. I also have another page named 1.html that contains a list o ...

Getting started with jquery plugins: installation and usage

I have some questions as a beginner. Firstly, I'm interested in obtaining this plugin: Unfortunately, I am having difficulty downloading it and seem to be stuck in a loop. Can someone guide me on the exact steps to download it? Secondly, how do I go ...

A dynamic way to refresh select options in HTML with PHP and JavaScript powered by JQuery

Imagine a scenario with the following layout: There is a table called "Regions" with columns "id" and "name". Also, there is another table named "Cities" containing columns "id", "region_id", and "name". How can you efficiently refill an HTML select opt ...

Assistance needed for jQuery append/prepend function selector

Imagine you have the following HTML code: <div> <span></span> <span></span> <span></span> </div> Is there a way to add text before the last span tag? Here's an example: <div> <span>< ...

Leveraging react-query with next-mdx-remote MDX Components?

I have been using next-mdx-remote in my next.js project. One of the components I've passed to it makes API calls using axios, which has been working well. However, I now want to switch to using react-query. When implementing this change, I encountered ...

Obtain an array containing the keys of an object

How can I retrieve the keys of a JavaScript object as an array, using either jQuery or pure JavaScript? Is there a more concise approach than this? var data = { 'first' : 'apple', 'second' : 'banana' }; var element ...

Finding the Row Number of an HTML Table by Clicking on the Table - Utilizing JQuery

Seeking assistance to tackle this issue. I currently have an HTML table with a clickable event that triggers a dialog box. My goal is to also retrieve the index of the row that was clicked, but the current output is not what I anticipated. //script for the ...

Tips for dividing the AJAX response HTML using jQuery

I have a piece of code that is functioning properly. However, I am having trouble splitting the HTML response using AJAX jQuery. I need to split it into #div1 and #div2 by using "|". Could you please provide a complete code example and explanation, as I am ...

Adding items from the JSON data to a nested array at index i

In my project, I am working with two files: index.js and list.json. My goal is to extract an element from list.json and insert it into a nested array following the structure [hour][visits per hour]. The hour refers to the index of the hour; for example, ...

Having difficulty integrating plugins like Lighthouse into my Cypress project

I need assistance with integrating the cypress automation plugin into my project. I made changes to my index.js file in the plugin folder, but now I am receiving this error: cy.lighthouse() is not a function Here is my index.js file content: const { light ...

Understanding the separation and communication techniques in Vue.js components

I recently developed a small vuejs application and I find myself getting confused with the functioning of components. Here is the code snippet that I have been working on: <div id="app"> <div v-if="loggedIn"> <nav> <r ...

Sending Python Object from Django View to Javascript Script in Template

Here is a basic code snippet created solely to illustrate a problem. While the task at hand can be achieved without JavaScript, I am exploring how to accomplish it using JavaScript for my actual code where it is necessary. In view.py Inside the play_game ...