Executing automated testing with Selenium to simulate clicking on a link with the href value set to javascript:void[0

My current task involves checking if clicking a link generates a report on the page using Selenium WebDriver with Java.

On the page, there are numerous links, most of which direct to another page where I automate filling in details and submitting a form to verify report generation.

However, there are three specific reports that trigger some JavaScript when clicked, followed by a new link appearing after a few seconds for downloading the report on the same page.

I am facing difficulty in clicking this particular link.

To tackle this issue, I have gathered all links on the page into a list of WebElements and then iterated over them to identify those with href containing javascript: void[0];

Subsequently, I attempted to click on the element in the list using the click method - allLinks.get(i).click();

This approach worked flawlessly for other reports, but it throws an error for these 3 reports stating "Element is not currently visible and so may not be interacted with"

Upon inspecting the link initiating the JS operation via firebug, the code snippet appears as:

<a onclick="requestReportGeneration('2cad4d4e5c8855c47a88b6ddf8345735', 'reportDiv33','CSV')" href="javascript:void[0];">CSV</a>

Seeking suggestions on how to successfully click this link?

Given that there are multiple links labeled CSV on the page, relying solely on the link text is not feasible.

UPDATE: A potential solution has crossed my mind. Initially, the page displays "Order reports" as the heading, which needs to be clicked to invoke a JS function expanding that section and revealing the links.

The reason why certain reports function smoothly is because upon reaching the page with the report links, I extract all hrefs from the page source and execute driver.get(reportList.get(i); without actually clicking the link.

I attempted obtaining the xpath of the heading and clicking it, however, even after doing so, I still encounter visibility issues when trying to click the link with href or javascript: void.

Answer №1

Verify if the following command triggers a click event on a specific link (the first one with javascript:void[0])

driver.findElement(By.xpath("//a[contains(@href,\"javascript:void[0]\")]")).click();

If it does, there may be an issue with the iterator.

Answer №2

When dealing with anchor tags that have the href="javascript:" attribute, it's best to utilize the native JavaScript click() method as shown below:

WebElement link = ((FindsByCssSelector) driver)
                .findElementByCssSelector("table[id='initiatorsTable'] tbody tr:not([class^='empty']) td[class='first'] a");

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click()", link);

//TODO: Add code to wait for action completion ...

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

Angular animated element persisting despite being told to disappear

I'm facing an issue that I need help with. Here's the link to the problem: http://jsfiddle.net/mhff6u7y/3/ The problem arises when I have an array of items that are repeated and filtered. Sometimes, I animate one of those items. The issue occurs ...

Automate website tasks with Python's Selenium: Easily obtain real-time text updates for any element

Struggling with a Python and Selenium framework issue where I need to cyclically read the text of an element, but encountering errors. While I can reference the element and read its text, attempting to access the .text property in an infinite loop results ...

Sorting in MongoDB is not case sensitive

What is the method to arrange a MongoDB collection based on a specific field in a case-insensitive manner? By default, the sorting order is A-Z then a-z. ...

Navigating the missing "length" property when dealing with partial functions generated using lodash's partialRight

I've been utilizing MomentTimezone for time manipulation within the browser. My development stack includes TypeScript and Lodash. In my application, there is an accountTimezone variable set on the window object which stores the user's preferred ...

Exploring VueJs 2.0: Iterating through components and updating properties

Searching for the optimal method to handle looping components has been my current focus. Below is an example of my coding dilemma. What I am attempting to accomplish is having payments data in my main component loop into child components. <tr v-for="p ...

Configure the SSL properties of a Spring Boot application by setting the getKeyStorePassword before the setKeyStorePassword

While attempting to set up HTTPS/SSL functionality using spring boot and tomcat, I encountered the following error: Caused by: java.lang.IllegalArgumentException: Invalid keystore format at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSL ...

Skipping BDD tests in Terminal Serenity due to unexpected errors

Having trouble running tests in Terminal for Serenity BDD, specifically using the command ( mvn verify serenity:aggregate ). Can anyone provide some guidance or assistance with this issue? Attached a screenshot for reference Your help would be greatly ap ...

What is the best way to integrate my Python GUI with my existing Python codebase?

After successfully creating a script with Selenium that automates form input from a CSV file, the next challenge is integrating it into a GUI. The GUI has already been developed, but the difficulty lies in combining these two components seamlessly. This i ...

Exploring the differences between accessing Json file content using require and fs.readFile

When dealing with multiple API responses in my web application, I need to efficiently map the values from these responses to an existing JSON file. What is the best approach for reading the JSON file in this scenario? Should I use require or fs.readfile? I ...

Troubleshooting: Issue with onclick event in vue.js/bootstrap - encountering error message "Variable updateDocument not found"

As a newcomer to frontend development, I have a basic understanding of HTML5, CSS, and Javascript. Recently, I started working on a vue.js project where I integrated bootstrap and axios. Everything seemed to be working fine until I encountered an issue whe ...

Displaying child properties in a functional ReactJS component

React version 0.14 introduces the concept of pure functional components, ensuring that the same input always results in the same output. Props are passed as function arguments. // Using ES6 arrow functions with implicit return: const PureComponent = ({url ...

Is it possible to pass a class method to an onClick event from within the render method in ReactJS?

Excuse my lack of experience, but I haven't been able to find a solution to this yet. I am attempting to utilize a class method as a callback for the onClick event in my JSX. Below is the code for my App component: import React from 'react&apo ...

Send information to a separate JavaScript file and display a notification

I am currently facing an issue with loading and passing data to a js file using the following code. jQuery.ajax({ async: true, type: "GET", url: "js/test.js", data:({data : result}), success: function() {}, dataType: 'scrip ...

What is the best way to create direct links to tabs using #anchors in JavaScript?

I am managing a website that includes info-tabs based on a template I found at this link. The tabs contain anchors like example.com/#convey, but unfortunately, direct links to specific tabs using #anchor do not work properly due to the tabs relying on Java ...

Find the current elapsed time using Wavesurfer in real time

I am currently utilizing the waveSurfer library created by katspaugh for playing audio files. In order to display 'elapsed time / total time', I have written code in the following manner: waveSurfer.on('play', function() { $scope.g ...

An error has occurred in the main thread: java.lang.NoClassDefFoundError - Unable to locate the class com.mongodb.operation.ReadOperation

I'm currently stuck on a database project and I can't seem to get this class instantiation code to work. I have double-checked that mongod is running and MongoDB is up and running, but the code below still fails. Any suggestions or tips on how to ...

What is the best way to display the letter X (Clear) in the Chrome INPUT field with the type

A custom application was developed that utilizes an input field of the number type: <input type="number" min="-1000000" max="1000000" step="0.01"> Within Internet Explorer, there is a small "clear" or [X] button on the right side of the input field ...

Leveraging DevTools functionalities on Selenium Grid within Docker environment

Although I successfully utilized DevTools features in Selenium 4 locally, I encountered issues when attempting to use it on Selenium Grid. To delve deeper into this matter, I set up a local grid with a single Chrome node using docker-compose, only to enco ...

Merging Angular with jQuery: Organizing jQuery into a distinct file leads to the error message 'Property 'top' cannot be read because it is undefined.'

My project is based on AngularJS, and I've been trying to incorporate a jQuery code for a sticky sidebar feature. Strangely enough, when I tested my code on Plunkr, it works perfectly fine. However, when I try to implement it in my actual project, it ...

Issue with Angular 2 pipe causing unexpected undefined result

Here is a JSON format that I am working with: [{ "id": 9156, "slug": "chicken-seekh-wrap", "type": "dish", "title": "Chicken Seekh Wrap", "cuisine_type": [2140] }, { "id": 9150, "slug": "green-salad", "type": "dish", "title": "Green Sala ...