Storing the output value of a JavaScript function in robot framework selenium2library results in a returned value of

My main goal is to automatically click on all the links present on a web page and then close any tabs that open up as a result. The structure of these links is like this:

<ul>
   <li>
      <a href=#>random text</a>
   </li>
   <li>...

Since there are multiple links to interact with, I attempted the following approach:

Click All Links
    Set Global Variable    ${i}    1
    ${elementCount}=    Execute Javascript    return document.querySelectorAll('a').length
    FOR    ${i}   IN RANGE    ${elementCount}
        Execute Javascript    document.getElementsByTagName('a')[i].click();
        Run Keyword and Ignore Error    Delete Tab
        Exit For Loop If    ${i} == elementCount
    END
    Log    Exited


Delete Tab
    Sleep    ${Delay_short}
    ${title_var}    Get Window Titles
    Select Window    title=${title_var}[1]
    Close Window

However, I am facing an issue where I cannot progress beyond retrieving the initial length of zero for ${elementCount}:

Executing JavaScript:
return document.querySelectorAll('a').length
Without any arguments.
20210104 10:20:26.687 :  INFO : ${elementCount} = 0

I would greatly appreciate any guidance or assistance in resolving this matter.

Answer №1

After some investigation, I discovered that the issue was not with the code itself but with the page being accessed - it returned a 401 error, so I needed to address that first.

Since the lower section was also malfunctioning, I implemented the following solution:

Click All Links
    Set Variable    ${i}    0
    ${elementCount}=    Execute Javascript    return document.querySelectorAll('a').length-1
    FOR    ${i}   IN RANGE    ${elementCount}
        Execute Javascript    i='${i}'
        Execute Javascript    document.getElementsByTagName('a')[${i}].click();
        Sleep    0.5s
        Wait Until Element Is Visible    css=a
        ${title_var}    Get Window Titles
        #Select Window    title=${title_var}[${i+1}]
        Log    ${title_var}[${i+1}]
        #SeleniumLibrary.Capture Page Screenshot
        Sleep    0.5s
        Select Window    title=${title_var}[0]
        Exit For Loop If    ${i} == ${elementCount}
    END
    Log    Exited
``

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

Loading a page via AJAX without triggering a reload of the entire website

I am experimenting with loading content from a different page using AJAX. The website I am currently testing on is dev.dog-company.com. Here's the code snippet that I have been working on: $('a[rel="load"]').click(function(){ //var sit ...

If variable is greater than, hide the division element

I have a variable called lineNum that increases each time I click on an h1 element I'm attempting to use an 'if' statement to hide a div by setting its display to none, but for some reason, I can't seem to get the 'if' code r ...

Using Vue.js to make an AJAX request to an API that returns a list in JSON format

I am attempting to utilize an AJAX call with the free API located at that returns a list in JSON format. My goal is to display the result on my HTML using Vue.js, but unfortunately, it doesn't seem to work as expected. This is my JavaScript code: va ...

Is it possible to bypass the standard syntax and manipulate geometry buffers directly in three.js in order to achieve improved performance?

Embarking on a creative journey into the realm of geometry generation and manipulation, I am eager to explore intricate and large-scale projects. While I am familiar with the conventional methods of achieving this, as demonstrated in the informative respon ...

Validating data for Telegram Web Bots using JavaScript

Struggling with creating a user verification script for my Telegram web app bots. Need help troubleshooting. import sha256 from 'js-sha256' const telegram = window.Telegram.WebApp const bot_token = '<bot-token>' const data_check_ ...

Encountering an error when trying to switch between tabs and close a tab in a window

Currently, my code is designed to perform a sequence of actions: open a window, navigate to a link on the page, extract some data from that page, and then close the tab. However, I am encountering an issue with closing the tab after completing these step ...

Unable to modify page property status through the Notion API

I've been attempting to use the Notion JS-sdk to update a page's status using their API. However, I've run into some issues that I can't seem to resolve. Updating the status requires modifying the properties object, but no matter what ...

Run two observables concurrently without needing to transform them into promises

I am in need of a way to simultaneously call multiple API endpoints asynchronously. I have Observable results from individual API calls, but what I really want is to gather all these results together and be able to handle them collectively, with an optio ...

Querying data with parameters in a client-side rendered application with Next.js

Currently, I am developing a chat application using Next.js and Axios. One of the functionalities I implemented is a dynamic route called chat/:pid to fetch data using the provided pid. However, I encountered an issue where the values are coming back as un ...

How to locate a particular element containing specific text using xpath

I have a unique set of spans to work with: <span> <span>foobar</span> <span>textexampleone</span> </span> Currently, I am attempting to utilize xpath in order to locate the span containing "textexampleone" with ...

Unable to display individual elements of an array using the map function in React Native

Below is my react-native code that I am using to display a list of array elements using the map function. import React from 'react'; import { createStackNavigator } from '@react-navigation/stack'; import {Card} from 'react-native-e ...

Despite the presence of a producer and topic, sending Kafka messages is proving to be a challenge

Currently, I am using TypeScript and the KafkaJS library on my local machine with a single Kafka broker. After successfully connecting a producer, confirming the creation of my topic, and creating messages like so: const changeMessage = { key: id, ...

When a function is passed as an argument in Javascript code, the setTimeout function may behave in unique ways

After running the code below, I noticed an interesting behavior: setTimeout(() => console.log('first'), 0) console.log('second'); As expected in JavaScript's asynchronous nature, the output was as follows: second first Howev ...

Searching for and modifying a specific subdocument in Mongoose

I am currently working with the schema for a document called Folder: var permissionSchema = new Schema({ role: { type: String }, create_folders: { type: Boolean }, create_contents: { type: Boolean } }); var folderSchema = new Schema({ nam ...

Utilize vanilla JavaScript to invoke the Angular factory

Struggling to find the right title for this query, I'm diving into Angular and using ngMaterial. Currently, I have a toast set up through an Angular factory. app.factory('notify', ['$mdToast', '$animate', function($mdToa ...

Minify Magic Dreamweaver Plugin

Does anyone know of a plugin that can minify javascript or css files as they are uploaded using Dreamweaver or similar coding software? For example, if I create a file like functions-global.js, the plugin would upload the original file and also generate a ...

What is the easiest way to connect to MongoDB and add a new post with just a few lines of code?

Currently, I'm working with express.js and node.js. Can you share a brief snippet of code for connecting to mongo and inserting a post into the database? ...

Is there a method for dynamically loading angular directives?

Check out this quick demo: http://jsfiddle.net/aSg9D/ Essentially, both <div data-foo-{{letterA}}></div> and <div data-ng:model="foo-{{letterB}}"></div> are not being interpolated. I'm trying to figure out a way to dynamical ...

Iterate through and conduct conditional verification

In my project using AngularJS and HTML, I have created a table to display records. I am looking for a way to iterate through the column values and strike through any value in the column that meets a certain condition. For example, in the demo provided her ...

Removing a method signature from a type that extends a function returning any in TypeScript

I am currently developing an API in typescript where I aim to return a function that includes multiple functions as properties, one of which is the same function. My approach to achieving this includes: type MainFunc = () => PublicInterface type Publi ...