Arrange links in a list using Protractor and select one to click at random

How can I create a list of all 100 search engine result weblinks and then use a random number generator to click on one randomly?

Here is the code I've been working with that doesn't seem to be functioning properly:

for (var iterate=1; iterate<100;){
    var randomnumber=Math.floor(Math.random()*4); 
    var i = randomnumber + 1; 
    console.log("Random Number generated for iteration " + iterate+ " is: "+i);
    var link=element.all(by.tagName("a"));
    browser.sleep(3000).then(function(){ console.log('Applied sleep for sometime');});
    link.map(function(links){

    return links;

    }).then(function(links){


    links[i].click();
    })

Any advice or suggestions would be greatly appreciated.

Answer №1

@elRuLL came close to providing an answer in the comments, but I wanted to expand on it further for anyone searching in the future.

element.all does not actually return an Array; instead, it returns an ElementArrayFinder object, and Protractor API offers specific functions to work with ElementArrayFinder.

get() -

element.all(locator).get(index) - Retrieves an element from the ElementArrayFinder by index.

filter(), first() and more... These special methods must be used to interact with the result of element.all(). Using element.all.[index] was an incorrect way to access it.

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

Using Ajax to update multiple text field rows with unique values

I have a question regarding utilizing Ajax to update the second text field based on the input from the first text field. I am looking to create multiple rows using a for loop, with each row having its own set of values. HTML <form style="text-align:c ...

Issue with Photoswipe pswp class Not Getting Properly Cleared Upon Closing Image

My website has a Photoswipe image gallery from . The issue I'm facing is that the CSS class does not reset or clear after closing the gallery for the second time. For example, when a user opens item 1, the images are loaded into the picture div via A ...

ngModel and ngRepeat allow for easy manipulation and iteration of properties

Here is a sample HTML code: <div ng-repeat="item in items()"> <input type="checkbox" ng-model="anObject[item.name]"> </div> I am trying to access a property that depends on the item.name within my $scope.anObject which I defined in ...

Obtaining Mouse Click X, Y, and Z Coordinates with Three.js

I am currently utilizing version 68 of three.js. My objective is to gather the X, Y, and Z coordinates upon clicking somewhere on the canvas. Despite following the steps outlined in this guide, I am encountering a consistent Z value of 0: Mouse / Canvas X ...

Having Trouble Sending Text to InputBox Using Selenium WebDriver

Greetings everyone Can someone guide me on how to use Selenium to input a Login and Password in an Alert Dialog Box? Upon loading the webpage, the alert is already displayed: https://i.stack.imgur.com/F1O5S.png I have attempted the following code: Str ...

Getting the text from a button type webelement

I'm attempting to extract text from a button element using Python scripting in Selenium. The button's HTML code is as follows: <button class="list-item ng-binding" ng-click="selectLineFilter(line)" type="button"> <i class="mdi mdi-domai ...

Is there a way to make the checkbox and corresponding text display on a single line within my code snippet?

Bootstrap text-truncate is causing the checkbox and text to appear on separate lines in the following example. How can I adjust it to have the text right after the checkbox? <!DOCTYPE html> <html> <head> <meta charset="UTF-8" / ...

Setting a time delay in a RESTful service

I am currently working on a service built with node.js + express. However, I am facing issues with managing the timer functionality. My service does not maintain any state, and it consists of 2 functions in the route. route.js var timerId; exports.linkU ...

Tips for generating JSON data in the correct format dynamically while continuously adding new information to the existing object

In my form, users input their email and password, which then generates dynamic JSON upon submission. However, I encountered an issue where the JSON gets corrupted when logging in with different user details as it updates the existing JSON with a new object ...

Tips for configuring <link> in NextJs to only activate on a single click

I am currently utilizing the Flickity-React-Component to create a swappable Carousel and I have included NextJs <Link> in its children. However, the issue arises when I click and swap the carousel - after releasing the mouse click, it automatically ...

What are the differences between using the open prop and conditionally rendering a Material-UI Modal component?

Is there a difference in using the open prop or conditionally rendering Material-UI's Modal component and its built components? The closing transition may be lost, but are there any performance advantages when dealing with multiple Modals? Example wi ...

Is it possible to run a Universal Windows Platform desktop app on an Android mobile device?

After successfully developing a Universal Windows Platform app in Visual Studio using WinJS, JavaScript, CSS and HTML, I am now interested in leveraging the same code base to create an Android application. Could you guide me on the necessary steps to run ...

Persisting a modal in NextJS on page refresh - navigating with elegance

After reviewing the Modal method discussed on this particular page in the NextJS documentation, as well as exploring responses to questions on Stack Overflow like this one, I noticed that in all instances, when the page is refreshed, the modal simply displ ...

Incorporating Common Types for Multiple Uses

Is there a way to efficiently store and reuse typings for multiple React components that share the same props? Consider the following: before: import * as React from 'react'; interface AnotherButtonProps { disabled?: boolean; onClick: (ev ...

Using wp_register_script to add multiple JavaScript files in WordPress

I'm trying to include multiple JavaScript files in WordPress using wp_register_script. I know how to add a single file like this: function custom_scripts() { wp_register_script( 'custom-script', get_template_directory_uri() . '/js/ ...

The React component using createPortal and having its state managed by its parent will remain static and not refresh upon state modifications

I am facing a problem that can be seen in this live example: https://stackblitz.com/edit/stackblitz-starters-qcvjsz?file=src%2FApp.tsx The issue arises when I pass a list of options to a radio button list, along with the state and setter to a child compon ...

Employing square brackets for accessing JSON data in JavaScript

I am facing a requirement where I need to extract the value of the JSON data based on the column specified by the user. for (var k = 0; k < arr.length; k++) { for (var i = 0; i < checkedFilters.length; i++) { console.log("value[columnNam ...

Guide on generating and inserting numerous dynamic form sections into a MySQL database

I am looking to create dynamic text fields that will capture information such as name, surname, age, and gender. I want to have a button labeled "add new user" that will allow me to add a new row for entering this information. The reason for needing it dyn ...

What is the best way to integrate the each_slice method in Rails views with React components

In my Parent component Monsters, I am rendering the Monster component. for each monster in @state.monsters React.createElement Monster, key: monster.id, monster: monster, team: @state.team... Within the monster: div className: 'col-md-4' ...

Learn how to default export React with withRouter, all while taking advantage of Material UI's makeStyles

I have been working on integrating Material UI makeStyles with class components, passing useStyles as props while default exporting it in an arrow function. export default () => { const classes = useStyles(); return ( <LoginClass classes={cl ...