What is the method for locating a particular link within a concealed row of a table solely based on linkText/linkName, without resorting to css-selectors or xpath?

Looking for a unique link in a dynamic table

Each row in the table expands to reveal more information when clicked. However, since the rows and IDs are constantly changing, finding a specific link by its text is challenging without using xpath.

Attempts with JavascriptExecutor have been unsuccessful, as clicking on the link by text without xpath or css-selector has proven difficult. Additionally, narrowing down the search to a specific table on the page poses another challenge, especially if there are multiple links with the same name.

One approach involved creating a collection of all rows in the table:

   WebElement table = $("table locator");
    List<WebElement> allRows = table.findElements(By.tagName("tr"));

    for(int i=1;i<allRows.size();i++) {

        WebElement table1 = $("table locator");
        List<WebElement> allRows1 = table1.findElements(By.tagName("tr"));
    allRows1.get(i).findElement(By.linkText("linkText"));

The challenge remains in creating a collection of hidden elements within each row.

Is there a way to find a hidden link strictly by its text without an absolute path? Can the DOM be iterated through by link name alone, or is a loop that clicks through each row until the link is found the only solution?

PS: Apologies for any language barriers.

Answer №1

There are three different methods you can use.

  1. Scan through all visible tr elements
  2. Review hidden tr elements one by one
  3. If the link has been present in the source code from the start, locate it along with its corresponding parent element (row)

Approach #1:

WebElement table = $("table locator");
List<WebElement> dataRows = table.findElements(By.cssSelector("tr[class*='data-row']"));
for (int i = 0; i < dataRows.size(); i++) {
     WebElement singleDataRow = dataRows.get(i);
     singleDataRow.click();
}
//All VISIBLE tr elements have now been clicked. Let's find our link
WebElement link = table.findElement(By.linkText("linkText"));
link.click();

Approach #2:

WebElement table = $("table locator");
List<WebElement> dataRows = table.findElements(By.cssSelector("tr[class*='actions']"));
for (int i = 0; i < dataRows.size(); i++) {
    WebElement singleDataRow = dataRows.get(i);
    singleDataRow.click();
}
//All HIDDEN tr elements have been clicked. Now let's find and click the link
WebElement link = table.findElement(By.linkText("linkText"));
link.click();

Approach #3:

WebElement link = table.findElement(By.linkText("linkText"));
WebElement parentElementOfLink = link.findElement(By.xpath("./*"));
//This is likely our desired tr element
parentElementOfLink.click(); //Clicks on the target row

UPDATE:

ENHANCED Approach #1:

WebElement table = $("table locator");
List<WebElement> dataRows = table.findElements(By.cssSelector("tr[class*='data-row']"));
for (int i = 0; i < dataRows.size(); i++) {
     WebElement singleDataRow = dataRows.get(i);
     singleDataRow.click();

     //We must locate the link after clicking each `tr` element.
     try {
         WebElement link = table.findElement(By.linkText("linkText"));
         link.click();
         break;
     } catch (NoSuchElementException e) {
         //Ignore the exception. It means the link does not exist in this row, so we continue iterating
     }
}

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

org.apache.http.conn.HttpHostConnectException: Unable to establish connection to host at 127.0.0.1 on port 7055

A problem has arisen with Selenium Webdriver(2.53) and Java (jdk 7). Mozilla Firefox ESR (45.2.0) crashes immediately upon opening a page, displaying the following error message: An Error Has Occurred org.openqa.selenium.remote.UnreachableBrowserExceptio ...

Display a div element for a specified amount of time every certain number of minutes

I am currently utilizing AngularJS, so whether the solution involves AngularJS or pure JS does not make a difference. In the case of using AngularJS, I have a parameter named isShowDiv which will determine the switching between two divs based on the follow ...

There seems to be a syntax error in the script where an expression was expected but

I have attempted numerous times to solve this issue but have been unsuccessful. Here is the code I have tried: <?php while($crow = mysqli_fetch_assoc($cres)) { echo ' <div class="item" onc ...

Excessive indentation detected within the website's formatting

There seems to be an issue with the mobile version of the website, possibly related to flexbox, SVG, or width settings that are unclear to me. Inspecting with Devtools reveals indents from the SVG and text itself. Check out the website here My goal is to ...

JavaScript conversion of arrays to JSON data structures

Here is the code snippet along with the variable 'polygon': var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var map; var bermudaTriangle; var directionsPoints; var example; var rez; function initialize() { ...

What is the best way to update a section of an HTML file without changing the entire "inner.html"?

I'm working on a counter that can either count backwards or in ascending order based on requirements. The counter is functioning correctly, but I want to add text to accompany each number as it counts. The issue is that the text changes every time the ...

Instructions for transferring the model to an ASP.NET controller from a ASP.NET Razor view utilizing jQuery

I've created a Razor view that takes in data. Instead of using the post method directly in the Form, I'm opting to first notify the User about saving. To achieve this, I've set up a save button to trigger a jQuery function which then calls t ...

Modify the name of the document

I have a piece of code that retrieves a file from the clipboard and I need to change its name if it is named image.png. Below is the code snippet where I attempt to achieve this: @HostListener('paste', ['$event']) onPaste(e: ClipboardE ...

Transmit the bound data (using ng-model) to a custom AngularJS directive

/*I am looking to define the maxDate as vmEndDate*/ app.directive('myDatepicker', function ($parse) { return function (scope, element, attrs, controller) { var ngModel = $parse(attrs.ngModel); alert(element.va ...

Working with SASS imports in an isomorphic React app: best practices

Currently, my React app is set up with SSR support as follows: React app Express server that compiles the React app using webpack Using babel-node to run the Express app with ES6 features Everything works fine until I added CSS modules to the React app, ...

Rails 7 is missing the Toast element from Bootstrap 5

Having some trouble with implementing Bootstrap 5 Toast in Rails 7 for flash messages. Here is the code I am currently using: # application.html.erb <head> ... <%= javascript_importmap_tags %> <script> const toastElList = document.que ...

What is the best way to adjust the minimum width of the HTML5 progress element?

There is a similar question which addresses input elements having a minimum width determined by the size parameter. I am attempting to create a layout with an inline-flex element with a flex-direction: column property like this: .item { border: 1px s ...

Creating a line that connects Point A to Point B using HTML, CSS, and JavaScript

Essentially, I am looking to create a line that connects point A (0vh|0vw) to point B (50vh|50vw). Initially, I considered using a div with a border and a 1px height that is rotated to achieve the desired effect. However, the issue arises when the size ...

Preventing the selection of rows when collapsing tree_mode nodes in free-jqgrid

I'm currently utilizing free-jqgrid 4.15.2 for navigation purposes in a tree mode setting. The issue I am facing is that when users collapse a node, the default action is to immediately select it. My goal is to allow users to hide sections of the menu ...

Krajee Bootstrap File Input, receiving AJAX success notification

I am currently utilizing the Krajee Bootstrap File Input plugin to facilitate an upload through an AJAX call. For more information on the AJAX section of the Krajee plugin, please visit: Krajee plugin AJAX The JavaScript and PHP (CodeIgniter) code snippe ...

Height Miscalculation: Chrome and FF encounter window dimension

There is a large application with numerous pages. When I use the console to execute console.log($(window).height()) on any page within the application, it returns the expected result: the height of the window, not the document. For instance: $(window).he ...

Vuejs v-for nested loops

After spending countless hours researching, I am determined to solve this problem. My objective is to create a questionnaire similar to a Google Form, with question groups, questions, and answers. The structure of my data looks like this: question_group: ...

What causes an error in RSVP Deferred when a promise is invoked multiple times?

What causes an error in RSVP Deferred when the promise is called twice? There appears to be a distinction between deferred.promise.then().finally() and deferred.promise.then(); deferred.promise.finally(). Why is this? RSVP.on('error', functio ...

"Troubleshooting: Next.js useEffect and useState hooks fail to function properly in a

Working on my project in nextjs, I've implemented the useEffect and useState hooks to fetch data: export default function PricingBlock({ data }) { const [pricingItems, setPricingItems] = useState() const [featuredItem, setFeaturedItem] = useState( ...

Why don't updates made in the database reflect in real time?

Currently, I am diving into the world of Firestore's real-time functionality. Below is a snippet of my code where I am fetching data: useEffect(() => { let temp = []; db.collection("users") .doc(userId) .onSnapshot((docs) =&g ...