Navigate a collection of items using Partial Link Text in Selenium

I'm in need of assistance with conducting an investigation and making improvements.

My objective is to collect all the <a> tags containing the text Annual Report as part of their content, and then iterate through them as they are originally located within a table (2nd column per row).

Here is the outline of the steps I plan to follow:

  • Click on each <a> tag to open a new window
  • Extract specific values from the newly opened window
  • Close the window
  • Proceed to the next element (<a> tag) and repeat the process

The code snippet below represents my current attempt, however, it does not seem to be functioning properly. I am facing difficulties with clicking on the initial element.

var reportLinks = driver.findElements(By.partialLinkText('Annual Report'));

for(var i = 0; i < reportLinks.length; i++){
    reportLinks[i].click();
}

Answer №1

Here's my approach to solving this problem.

//Use this code snippet to determine the number of elements present
var countLinks = driver.findElements(By.xpath('//tr/td/a[contains(text(),'Annual Report')]'));

//Click on each element using a for loop
for(var i = 0; i < countLinks.length; i++){
driver.findElement(By.xpath('//tr[' + i + ']/td/a[contains(text(),'Annual Report')]')).click();
}

The reason behind this method is that the Annual Report element is not directly inside the parent container, but within another container.

Answer №2

I successfully implemented the solution below:

for (let i = 1; i < 51; i++) { 

    let element = driver.findElement(By.xpath('//*[@id="dataList"]/table/tbody/tr[' + i + ']/td[1]/a'));
    driver.executeScript("arguments[0].click();", element);

}

By extracting the xpath from the webpage using Inspect Element feature in Google Chrome and passing it as an argument to the executeScript method, I was able to achieve the desired outcome.

You can learn more about this process through the following link: Is there a way to get the xpath in google chrome?

Thank you for your assistance!

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

Transform an Axios promise into a standard JSON array

I'm encountering some difficulties with a function that is supposed to return data. The function is intended to return JSON data, but instead, it is returning a promise. Below is the code for the function: import axios from 'axios'; cons ...

Switch out regex with an equal number of characters

I am looking for a way to replace specific content using a regex with the same number of characters but replacing them with a character of my choice. For instance: content: "abcdefghi*!?\"asd"; should be transformed into: content: "--------------- ...

Approximating Values with JavaScript in Selenium

Hi there! I've been exploring selenium IDE and encountered an issue related to asserting an approximate value. My challenge is to validate a numeric value within an element identified by its id, where the value is in numerical format with commas separ ...

Using JavaScript to dynamically retrieve element IDs

Within the content on my page, there are multiple tables displaying product information along with their respective authors. Additionally, there is a div that contains hyperlinks for each author. Currently, I am linking the authors' names to hyperlink ...

How do prototype, the $.extend method, and the concept of "return this" all connect with each other?

I've been assigned a legacy project, and I find myself puzzled by the purpose of this code. define(['jquery', 'components/BaseComponent', 'bootstrap'], function( $, BaseComponent, bootstrap ) { 'use strict&a ...

What is the process behind executing the scripts in the jQuery GitHub repository when running "npm run build"?

Check out the jQuery repository on GitHub. Within the jQuery repo, there is a "build" folder. The readme.md mentions the npm command: npm run build This command triggers the execution of scripts in the build folder to complete the building process from ...

locate several elements based on their unique identifiers

I am currently attempting to locate numerous elements using their individual ids. The elements have the following names: dcm-reservation-limit-multiple-input-generic-X where X represents the number of elements, such as: dcm-reservation-limit-multiple-in ...

How about making an Ajax request for each item in the array?

I have a task to perform Ajax calls for each item in an array and then trigger another function once all the calls are completed. Adding complexity, I am incorporating Papa Parse into making the Ajax call. This is the code snippet: getCsvData: function( ...

Is it possible to dynamically create an interface using an enum in TypeScript through programmatically means?

Recently, I defined an enum as shown below: enum EventType { JOB, JOB_EXECUTION, JOB_GROUP } Now, I am in need of creating an interface structure like this: interface EventConfigurations { JOB: { Enabled?: boolean; }; JOB_EXECUTION: { ...

The local character array within a function maintains the same value across different invocations

Could someone shed light on why the first call to printf in the number() function prints the string from the previous call of token[]? I have omitted function commands that do not relate to strings. Should more code be necessary, please inform me. PS: I ...

Sending image id as a parameter to a MySQL query in PHP using jQuery

Having trouble passing image ids to mysql queries. I've tried the following code but keep getting an Undefined index x error. I've checked multiple Stack Overflow answers without success. echo "<img src=http://localhost/ss/img/".$p['pat ...

A comprehensive guide on converting an array of custom objects into JSON using Swift 4

I am currently in the process of preparing parameters to be sent to a server. This involves converting my array of custom objects into JSON format. Here is what I have attempted: let data = try? JSONSerialization.data(withJSONObject: fastForm.route, opti ...

Guide on launching a fresh tab with an extension and selenium

Is there a way to simulate pressing a combination of buttons using Selenium in Chrome? I have an extension that opens a new tab when I press control + shift + x, but I'm not sure how to trigger this shortcut using Selenium. I've tried several met ...

Tips for updating a specific portion of a component in react.js

import React,{useEffect} from 'react'; import CloudTables from '@cloudtables/react'; import { useState } from 'react'; function DataGridTable ({ input1Value, input2Value }) { return ( <div className="con ...

Tips for populating a multidimensional array using a loop

Here is an example of code with arrays : $cities = array( 'New York', 'Los Angeles', 'Chicago', 'Houston' ); Now, I have a task at hand where I need your assistance: I want to populate the cities arr ...

Avoid having to refresh the page on create-react-app after uploading an image or file

Currently, my application is set up with 'create-react-app' and I am retrieving images from a folder within the 'src' directory. However, when a new image is uploaded through a form submission, it causes the page to reload as the image ...

Is there a workaround in TypeScript to add extra details to a route?

Typically, I include some settings in my route. For instance: .when('Products', { templateUrl: 'App/Products.html', settings: { showbuy: true, showex ...

Turn off Babel's strict mode when transpiling JavaScript files

Currently, I am facing an issue while trying to transpile a set of JavaScript files using Babel. Since Babel operates in strict mode by default, it triggers a syntax error whenever there is a conflict like the use of the delete keyword. The solution I am s ...

Ensuring the image is properly sized to fit the screen and enabling the default zoom functionality

I am looking to achieve a specific behavior with an image, where it fits the viewport while maintaining its aspect ratio and allowing for zooming similar to how Chrome or Firefox handle local images. Here are the details of my project: The image I have is ...

``Please note that the Sinon spy.called method is currently not

Context In my setup, a small server receives data from a machine. Each time a message is received, a function in a dispatcher object is called, which simply logs everything it receives to the console. Issue While I can see the logs in the console, Sinon ...