Guide on utilizing selenium webdriver for link clicking

Is there a way to utilize selenium webdriver in order to retrieve all links from a page that contain the attribute a[href*=/simulation/form/], and proceed to open and close each one?

Although I attempted the code provided below, I encountered the error message

TypeError: link.click is not a function

var simLinks = driver.findElements(By.css('a[href*=/simulation/form/]'));

for(var link in simLinks){
  link.click();
  driver.wait(30000);
  driver.back();
}

Running console.log(simLinks.length) resulted in undefined

However, when attempting to open a single link of that type, it executes without issue:

var simLinks = driver.findElement(By.css('a[href*=/simulation/form/]')).click();

Answer №1

To check objects thoroughly, utilize the util.inspect() function:

const util = require('util');
...
console.log(util.inspect(simLinks));

Upon inspection, you will notice that simLinks is a ManagedPromise, requiring you to modify your code in the following manner for it to function correctly:

driver.findElements(By.css('a[href*="/simulation/form/"]')).then(function (simLinks) {
  for (let link of simLinks) {
    link.click();
    driver.wait(until.urlContains('/simulation/form/', 30000));
    driver.navigate().back();
  }
});

The issue arises when only the initial click operation works, and upon navigating back, a valid StaleElementReferenceError occurs - indicating that the page has changed and the subsequent element in the loop is no longer visible. To tackle this, it would be more effective to gather the link addresses instead and use them in conjunction with driver.navigate().to(link):

driver.findElements(By.css('a[href*="/simulation/form/"]')).then(function (simLinks) {
  var hrefs = simLinks.map(link => link.getAttribute('href'));
  for (let link of hrefs) {
    driver.navigate().to(link);
    driver.wait(until.urlContains('/simulation/form/', 30000));
    driver.navigate().back();
  }
});

Answer №2

My recommendation would be to utilize the following:

console.log(simLinks.size())

As for the parentheses, I am uncertain if they are necessary in JavaScript, but they are mandatory in Java.

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

Yeoman - A guide for integrating an express server task into Gruntfile.js from the generator-angular template

Currently, I am diving into the world of Grunt and attempting to integrate an Express server into my AngularJS application that was initially created with Yoeman. I've made adjustments to the following task as shown below: grunt.registerTask('s ...

Send the user back to the previous page once authentication is complete

I am integrating Google authentication through Passport in my web application and I am facing an issue with redirecting the user back to the original page they requested after a successful sign-in. It seems like the use of location.reload() might be causin ...

AccessMediaStream - camera direction

My current setup involves using an Android tablet and GetUserMedia to capture images in my program. It seems that by default, GetUserMedia uses the front camera. How can I set the rear camera as the default instead? Below is the code snippet I am using w ...

Having trouble incorporating a variable into a jQuery selector

Trying to crack this puzzle has been an ongoing battle for me. I seem to be missing something crucial but just can't pinpoint what. I recently discovered that using a variable in the jQuery selector can make a significant difference, like so: var na ...

The issue with Angular-gettext is that it fails to update dynamically generated strings in the code

Whenever I try to set the language using the code gettextCatalog.setCurrentLanguage(langString);, it doesn't seem to affect my side-nav menu. My side menu has two possible states: expanded or collapsed, and I use ng-include to control its content base ...

When using jQuery Ajax, only pass the query string if it is defined and not empty

Using jquery and ajax to communicate with a CMS API, I am constructing a query string to fetch data: region = typeof region !== 'undefined' ? 'region='+region : ''; centre = typeof centre !== 'undefined' ? 'cen ...

Here is a unique version: "A guide on centering a carousel item in jquery upon being clicked."

Does anyone know how to center the item I click in a carousel? I've searched high and low for a solution but couldn't find a clear answer. Can someone please assist me with this? This is what I have tried so far: http://jsfiddle.net/sp9Jv/ Here ...

Obtain the dynamic $scope variable within the HTML structure

When creating numerous directives with dynamic scope variables initialized in the link functions, accessing these values within the directive templates can get tricky. For example: // link: function(scope, ele, attr){ scope.key = scope.somevar + 's ...

Are there any JavaScript alternatives to Python's pass statement that simply performs no action?

Can anyone help me find a JavaScript alternative to the Python: pass statement that does not implement the function of the ... notation? Is there a similar feature in JavaScript? ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

How to Embed a Javascript (jquery) Variable within a JSON Object

I have searched everywhere for a "simple answer" to this problem, but unfortunately, I cannot find a solution that aligns with my understanding of JSON and jQuery. Perhaps I am too much of a beginner in JSON to accurately formulate the right question (and ...

Unlock the power of jQuery chaining with the val() method

My selected background color is set to: var _back = "#FF0000"; Why doesn't this code change the input field's background color: $("#input").val( _back ).css("background-color",$(this).val()); But this one does? $("#input").val( _back ) ...

Deferred computed property fails to recognize the final character entered into the input field

I am facing an issue with my automated tests using selenium. The problem lies with a particular input field in a web form: <input data-bind="value: searchText, valueUpdate: 'afterkeydown'"></input> Here is the model associated with ...

Unable to access API endpoint for retrieving items as described in the Express/React tutorial on Pluralsight

Currently, I am going through a tutorial on React, Express, and FLUX. Unfortunately, I have encountered a roadblock in the form of a CANNOT GET error when trying to access my API data. https://i.stack.imgur.com/RbUzf.png In the server file under Routes & ...

"Real-time image upload progress bar feature using JavaScript, eliminating the need for

I have successfully implemented a JavaScript function that displays picture previews and uploads them automatically on the onchange event. Now, I am looking to add a progress bar to show the upload status. However, all the solutions I found online are rel ...

Having issues with AJAX and button submit while trying to upload a file using Flask

I've been attempting to incorporate a file upload feature (specifically a .csv file) using bootstrap, and then submit it by clicking on a button. I've experimented with various methods to implement the file upload functionality, but haven't ...

Is there a way for me to detect if the user has manipulated the CSS or JavaScript in order to alter the look of my website?

Is there a way to determine if someone is utilizing script or CSS extension tools? For instance, if they have adjusted alignments, applied display: none; to multiple elements, or utilized jQuery's .hasClass to manipulate divs. ...

Display a specific element only if another element exceeds a specified height

A snippet of HTML code is given below: <span class="day-number">{{day-number}}</span> <div class="event-box"> <div class="event-container"> </div> <div class="more-events">more ...</div> </div> The .e ...

What could be causing the "Circular structure to JSON" error in Node.js when executing a MySQL query?

Currently, I am working on executing a MySQL query to retrieve all the data from a table named LOGIN. If you navigate to https://localhost:2000/select, and provide the parameter in the req.body as table_name with the value set as login. When making t ...

Searching for elements by their classname that starts with specific text in Selenium can be accomplished by using the

Here is the element description: <div class="css-control-textinput-122aa"><input type="text" aria-required="true" disabled="" required=""</div> The class mentioned above is named css-control-textinput-122aa, however, the value 122aa is not ...