Attempting to use Selenium in JavaScript to download a file with a new element called 'TransitionRejection'

I am looking to streamline the process of downloading multiple files from a website. To achieve this, I navigate through several pages to obtain the file IDs using selenium and a perl script.

Since selenium does not have a built-in download function and using curl is not an option due to needing the session information from selenium, I am attempting to implement a workaround that I have successfully used in other projects.

The workaround involves creating a JavaScript element and executing a script with the URL for each file, like so:

var a = document.createElement("a");
a.setAttribute("href", "https://myurl.com/id1/export?format=TCX");
a.setAttribute("download", "https://myurl.com/id1/export?format=TCX");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

However, when I attempt this, I encounter the following error in the script:

Error while executing command: stale element reference: The element reference of is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 391. at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 348.

Testing this directly in the Firefox console yields:

TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#2( 'top.ids.info.byId'{"myId1":"XXXXX","myId2":"YYYYYY"} -> 'home'{"feedId":null,"tag":null} ))

When accessing the URL via a normal browser GET request, I receive the download prompt without any issues (provided I am logged in).

Using selenium to access the URL works for the first file, but then gets stuck and does not proceed to download the second file.

$driver->get($download_url1); # This one is downloaded
$driver->get($download_url2); # This line is not executed

Answer №1

After some troubleshooting, I managed to resolve the issue using JavaScript. Rather than dynamically creating the element, I modified certain attributes to mimic the website's standard functionality. Additionally, I adjusted the URL from:

https://myurl.com/id1/export?format=TCX

to

/id1/export?format=TCX

As a result, the updated code appears as follows:

my $script = 'var a = document.createElement("a");' .
    'a.setAttribute("ng-href", "' . $url_to_download .'");' .
    'a.setAttribute("target", "_self");' .
    'a.setAttribute("class", "button button-default button-block");' .
    'a.setAttribute("ng-click", "dismiss()");' .
    'a.setAttribute("href", "' . $url_to_download .'");' .
    'document.body.appendChild(a);' .
    'a.click();' .
    'document.body.removeChild(a);';

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

Error 404: This page seems to have gone missing. Django and react-router

Struggling to integrate reactjs and react-router (1.x) with my Django project. I'm finding it challenging to make everything work together seamlessly. For more details, you can check out the project on GitHub: https://github.com/liondancer/django-che ...

Obtaining a value using the Node.js inquirer package

I'm currently working on a flashcard generator using the node.js inquirer package, but I'm struggling to capture the user's selection. When the user selects an option, I want to be able to log that choice, but right now it's just return ...

Creating captchas seems like a mistake in reasoning to me

I am encountering an issue with my code. I created a basic newbie-level captcha using Javascript. Below is the code snippet: <!DOCTYPE html> <html> <head> <style> </style> </head> <body> <h1>T ...

I am looking to transfer information in CodeIgniter from a view utilizing AJAX post, handle that information in the controller, and then return the resultant array back to the view

I have been searching the entire internet, but unfortunately, I couldn't find a helpful solution. Any assistance would be greatly appreciated. Thank you in advance. MY HTML <div class="row"> I am unsure whether the form tag is required in my ...

Creating a dynamic cascading dropdown list with Vue.js: Step-by-step guide

I successfully implemented a dropdown list using Vue.js, but now I want to add another similar list. How can I set this up? Here are the codes for both dropdown lists: var addUserVue = new Vue({ el: "#app", data: { heading: "Vue Select Cas ...

Direct your attention to alternative data sources following the selection of an item in the typeahead search feature

Having some trouble with angular-ui bootstrap: I am using 2 input elements: <input type="text" ng-model="selected" uib-typeahead="state for state in states | filter:$viewValue | limitTo:8" typeahead-on-select="focusSuccessive($item, $model, $label, $ev ...

Having trouble transmitting data with axios between React frontend and Node.js backend

My current challenge involves using axios to communicate with the back-end. The code structure seems to be causing an issue because when I attempt to access req.body in the back-end, it returns undefined. Here is a snippet of my front-end code: const respo ...

The error message 'Cannot read property 'navigate' of undefined' is displayed because the function '_this.props.navigation.navigate' is not

I'm facing an issue trying to access 'Home' from Form.js. Despite my efforts in arrowF, routes, and other methods, the same error persists. How can I resolve this? In my Form.js file, when I call onPress=(), I have tried using functions, ...

Unable to assign a scroll event delegation by using the "on" method

When attempting to delegate a scroll event in order for my element to maintain the same handler after being returned by an ajax call, I utilized the on method for delegation. $('body').on({ scroll:function(){ alert('scrolling&ap ...

Changing focus to 'DIV' element without JQuery using Javascript and Angular's ng-click event

Instructions for using an Angular directive to set focus on a "DIV" element when clicked <a href="#" class="skipToContent" ng-click="showContent()" title="skip-to-main-content">Skip To Main Content</a> <div class="getFocus" role="button" ...

How to use a self-hosted font in a custom Material-UI theme for ReactJS?

Currently, I am in the process of setting up my initial project utilizing Material-UI for ReactJS. My intention is to personalize the default theme by incorporating a custom font (from the server, not sourced from Google Fonts or anything similar). Despite ...

Caution: React alert for utilizing the UNSAFE_componentWillReceiveProps in strict mode

As a newcomer to React, I encountered a warning that has me stuck. Despite researching extensively online, I still can't resolve it. The warning message is: https://i.stack.imgur.com/4yNsc.png Here are the relevant portions of the code in App.tsx: ...

What is the method for displaying html files in a POST request?

This is the code snippet I am working with: app.post('/convert', function(req,res){ var auxiliar = "somo Ubuntu command line(this works)"; exec(auxiliar, function(err, stdout, stderr){ if(err){ console.log ...

I am struggling to link my JS application with a PHP file as it is showing "PHP file not found

I am facing an issue with my JS application. I am unable to send data from jQuery to a PHP file, as I encountered this error message: POST https://magazyn.rob-tech.pl/savesettings.php 404 The app is running on PORT=8080 npm run start and the package.json ...

Node.js refuses to launch - the dreaded error 404, signaling that it has mysteriously vanished

I am brand new to node.js, so please be patient with me as I learn. Currently, I am using the express framework and attempting to create a basic application that displays content as HTML. Below is the essentials of my app.js: var express = require(' ...

Joi mistakenly demanding certain fields that should not be mandatory

I've encountered an issue with posts validation using @hapi/joi 17.1.1. In my schema, I have two fields: textfield and picture. Although both fields are not required, the validation is still indicating that the picture field is mandatory. posts valid ...

Is there a way to select a Dropdown menu choice on a website by utilizing VB.NET and Selenium?

Currently, my main goal is to access the Supplier section on the website. I have included a screenshot of the source code for reference. At this point, I have successfully logged in to the site, but now I need to navigate to the next screen by clicking o ...

Update the content of the document element by assigning it a lengthy string

I'm utilizing JQuery to dynamically assign content to a div element. The content has numerous lines with specific spacing, so this is the approach I am taking: document.getElementById('body').innerHTML = "Front-End Developer: A <br/> ...

What steps can be taken to ensure express Node.JS replies to a request efficiently during periods of high workload

I am currently developing a Node.js web processor that takes approximately 1 minute to process. I make a POST request to my server and then retrieve the status using a GET request. Here is a simplified version of my code: // Setting up Express const app = ...

Utilizing ng-style with a ForEach loop on an Object

How can I dynamically add a new style property to objects in an array in Angular, and then use that property inside ng-style? CommentService.GetComments(12535372).then(function () { $scope.comments = CommentService.data(); angular.forEac ...