Obtaining the href value with selenium webdriver in the presence of javascript triggered by a link click

Greetings for taking the time to review my query. In examining a webpage, I have come across more than 200 links and have verified that they are all functioning properly. The challenge arises when extracting the 'href' value as it does not always contain an actual link but rather a JavaScript function being called. Here is an example source:

<a tabindex="8" title="Internal Crossload" target="_self" href="javascript:fnnHomePage('3' , 'WTMS_EXPRESS')">&nbsp;&nbsp;&nbsp;&nbsp;- Internal Crossload </a>

One of the JavaScript functions being invoked is as follows:

<Script>
    /*********************************************************************
    Function Name          : fnnHomePage
    Input Parameter(s)     : transferTypeId
    Output Parameter(s)    : None
    **********************************************************************/
    function fnnHomePage(transferTypeId ,moduleName) {
        if (moduleName == "XXX_EXPRESS")
         {
            document.getElementById("transferTypeId").value=transferTypeId;
            document.getElementById("gadgetType").value="XXX_EXPRESS";
            document.getElementById("moduleName").value="XXX_EXPRESS";
            document.forms[0].action="/XXX/getProposalHomePage.do?transferTypeId="+transferTypeId;
            document.forms[0].submit();
         }
        if (moduleName ==  "CROSSLOAD")
         {
            document.getElementById("transferTypeId").value=transferTypeId;
            document.getElementById("gadgetType").value="CROSSLOAD";
            document.getElementById("moduleName").value="CROSSLOAD";
            document.forms[0].action="/XXX/getCrossLoadHomePage.do?transferTypeId="+transferTypeId;
            document.forms[0].submit();
         }
    }
</Script>

Given this scenario, how can I extract a valid 'Link' and verify its functionality using Selenium WebDriver, considering each link triggers a different 'JavaScript function'? Any insights or recommendations on this matter would be highly valued. Thank you.

Answer №1

A helpful tip is to click on a provided link, which will lead you to a new page generated by a JavaScript function. Once on the new page, retrieve the link by using driver.getCurrentUrl();, then return to your starting point and continue with your tasks as normal.

Hopefully, this method is clear and easy to follow.

Answer №2

Check out this code snippet that allows you to capture and display all the links on a webpage, giving you the ability to easily navigate through them:

 driver.get("https://www.facebook.com");
    List<WebElement> all_links_webpage = driver.findElements(By.tagName("xyz")); 
    System.out.println("Total number of links available: " + all_links_webpage.size());
    int links = all_links_webpage.size();
    System.out.println("List of links available:");
    for(int i=0;i<links;i++)
    {
    if(all_links_webpage.get(i).getAttribute("href").contains("google"))
    {
    String link = all_links_webpage.get(i).getAttribute("href");
    System.out.println(link);
    }   
    }

Give this code a try and see if it helps with your needs.

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

Unable to retrieve the text enclosed between the:: before and after the:: marker

I attempted this using the XPATH finder in Chrome, and it highlighted the element. However, when running my Selenium script, I received the following error: Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: ...

Issues with Angular service functionality

I've been working on an application using Angular for the client side and Node for the server side. I've incorporated some API's into my Angular code, but I'm encountering difficulties with retrieving data from a specific API. Can someo ...

Connecting a Package as a Lerna Package Dependency during Development Journey

In my project, the structure is organized as shown below: packages/ client/ package.json server/ package.json shared/ package.json lerna.json package.json The client's package.json file includes the following depen ...

The effectiveness of Chrome arguments and preferences in Protractor configuration appears to be lacking

Seeking assistance with the following issues. I have already found a workaround for issue 1, so any advice on issue 2 would be highly appreciated: The parameter --start-maximized does not trigger the full window, so my current workaround involves addin ...

What is the correct method for notifying the progress of time using jQuery?

I'm looking for a way to display the processing time of my PHP code using jQuery Here's an example of my PHP code : <?php //some queries to database ?> Below is my jQuery code: $.ajax({ type: "POST", url: "action. ...

The WHATWG URL API allows creation of a new URL using the new URL

I've been experimenting with node and attempting to create an instance of the URL class to utilize its useful properties. Here's what I tried: const { URL } = require('url'); (...) http.createServer((request,response) => { let u ...

Reasons for the failure of file uploads from the React frontend to the backend system

Recently, I embarked on a new project that involves using React for the front-end and Node for the back-end. The main requirement of this project is to upload multiple images from the front-end, with the back-end handling the file uploads to Cloudinary. I ...

Issue: Encounter an Error with Status Code 401 using Axios in React.js

For my login page, I am utilizing a combination of Laravel API and ReactJS frontend. In my ReactJS code, I am using Axios to handle the parsing of the username (email) and password. The login API endpoint is specified as http://127.0.0.1:8000/api/login, wh ...

What are the steps to update your profile picture using Angular?

In my Angular 6 application, I am implementing an image upload feature with the following code: Html: <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> <br/> <input type='file' (change)="onSelec ...

Javascript and JSON: Making Updates to Files

Working on a program, I am faced with an issue while sending a literal variable to local storage using JSON.stringify. My goal is to continuously update the local storage and append to the existing data stored. The problem arises during the parsing of the ...

Could not connect the chrome node to the hub

Below is my docker-compose.yml file that binds Chrome and Firefox nodes to a Hub. version: "3" services: selenium-hub: image: selenium/hub container_name: selenium-hub ports: - "4444:4444" chrome: image: selenium/node-chrome-de ...

Retrieve information about a parent's data related to child events in Vue.js

I'm working with two components nested inside each other. The parent component has a click event that needs to modify the data value of the child component. <template> <div> ..... ..... <my-component :op ...

Issue with jqGrid Multiple Selection

When constructing my jqgrid, I utilize the following code: multiselect: true This enables a check all column. However, clicking the check all checkbox (located at the header) selects all checkboxes, but does not click the checkboxes in each row. You can ...

Hide the chosen option within the following select box using jQuery

How can I hide the selected option in the first select box in the second one using jQuery? Here is my HTML code. <div> <select class="fields-mapping"> <option>Option1</option> <option>Option2</option> <op ...

Exploring the Angular way of searching through arrays of objects

What is the most efficient method for searching a specific parameter within an object array in Angular? I am populating my array using Angular's foreach method: $scope.arrayiwanttosearch = []; angular.forEach(data, function(value, key) { ...

Concerns arise with jQuery grid compatibility in Firefox browsers

I have found an interesting tutorial on draggable image boxes grid at this link. Although everything is functioning properly, I am encountering an issue with the drag function specifically on the home page when using Firefox. As a beginner in jQuery, I am ...

Tips for ensuring a watcher only runs once in each digest cycle, regardless of how many times the property is modified

I am facing an issue with my function $scope.render() that relies on the value of $scope.myProperty to render a chart. Whenever myProperty changes during a digest cycle, I need to ensure that $scope.render() is called only once at the end of the digest cyc ...

Sending a POST requestIs there an issue with performing

Below is my JavaScript code snippet: $('document').ready(function () { $('#filterSub').click(function (evt) { var data = $('form').serialize(); var gender = $('#gender').html(); $.post(& ...

AngularJS - A Guide to Detecting Window Resize and Orientation Changes in Landscape and Portrait Modes

I have attempted various suggestions from Stack Overflow, but none of them have been successful. Essentially, I am seeking to update (re-render) a custom directive whenever the window size changes. This is my code: angular.module('dynamicSlideBox&ap ...

What are the steps to troubleshoot and resolve validation errors while using the paypal-rest-sdk?

I'm currently experimenting with the paypal-rest-sdk to learn how to integrate PayPal checkout into my website. However, I've encountered an issue during testing where whenever I click submit in my form, I receive a "localhost refused to connect" ...