Guide for identifying the dynamically changing last 'li' element within a 'Ul' using a Selenium driver

<ul class="drillDownMenu l_drillDown" style="left: -498px;">
<li class="hasSubs">
<a id="RAL10" href="javascript:;">
<ul class="active">
<li class="hasSubs">
<li class="hasSubs">
<a id="AL101117" href="javascript:;">AR Invoices</a>
<ul class="displayed active">
<li>
<a id="FAL10111726" onclick="LoadQueryWindow(this,'104')">AR Invoices</a>
</li>
<li>
<a id="FAL10111727" onclick="LoadQueryWindow(this,'134')">All AR Invoices 1</a>
</li>
</ul>
</li>

I am attempting to utilize the following driver statement:

driver.findElement(By.xpath("//*[contains(@id,'FAL10111727')]")).click();

However, this is selecting the first element, whereas I need to select the last element in the list.

Thank you

Answer №1

Give this xpath expression a shot:

(//ul[ @class='drillDownMenu l_drillDown']//li)[last()]

This expression selects all li elements that are direct descendants of the highest level ul, and then chooses the last element from that selection.

Answer №2

According to the HTML code you shared,

If you want to select the last 'li' element that is dynamically changing each time, specifically All AR Invoices 1, you can use this XPath:

driver.findElement(By.xpath("//ul[@class='displayed active']//following::li[last()]")).click();

However, it seems like the <li> tags might not be clickable directly, so you may need to click on the inner <a> tag within the <li> instead. You can do this by using the following XPath:

driver.findElement(By.xpath("//ul[@class='displayed active']//following::li[last()]/a")).click();

Answer №3

After some experimentation, I found success with the following code:

driver.findElement(By.cssSelector("ul.displayed li:last-child a")).click();

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

organize a data set using react's hook system

Trying to sort an array using react hooks but the state is not updating. Any help on what could be going wrong? import React, { useState } from "react"; import ReactDOM from "react-dom"; import "./styles.css"; const dogs = [{ name: "fido", age: 22 }, ...

Develop a fresh behavior on-the-fly

Here is the HTML code snippet: <div class="bold knowmore login" id="j-6"> <span>...</span> </div> and this jQuery script: $(function(){ $(".login").on("click", function(){ console.log('login clicked!'); $(" ...

Creating CSS for a specific class on a single webpage can be achieved by targeting that class

Here is my custom style: <style> ul { padding:0 0 0 0; margin:0 0 0 0; } ul li { list-style:none; margin-bottom:25px; } ul li img { cursor: poin ...

Angular 2 System.config map leading to a 404 error message

I am encountering a 404 error in the browser console while attempting to map the Auth0 module from node_modules in my Angular 2 project using the system.config in the index file. Index File <!-- 2. Configure SystemJS --> <script> System.con ...

A guide on clicking through elements stored in a list using Selenium

I am attempting to retrieve all elements by partial link text and then clicking on each one to gather information from the subsequent page. My initial thought was to click on each link, go back to the previous page, and repeat the process for the remaining ...

Adjust parameters using JavaScript and three.js in real-time

I am not well-versed in JavaScript and I am facing an issue. I need to dynamically change the parameter passed to a function written in JavaScript, but the code is structured in a way that is unfamiliar to me. Therefore, I am seeking assistance. On my web ...

An object containing an ArrayList of Rectangles is being passed

Hey there, I'm encountering an error in the console: no suitable method found for intersects(Object) if((EnemyBarrels.get(i).getHitBox()).intersects(buildG.getStepD().get(i))){ ^ method Rect ...

Utilize Ajax to invoke a function simultaneously with another Ajax call that includes preventDefault to submit the data

I am currently implementing two AJAX calls within my form. The first call is used to dynamically update the options in the second select element based on the value selected in the first select element. This call reaches out to a PHP page to process the dat ...

Is there a way to ensure that both new Date() and new Date("yyyy-mm-dd hh:mm:ss") are initialized with the same timezone?

When utilizing both constructors, I noticed that they generate with different timezones. Ideally, they should be in the same timezone to ensure accurate calculations between them. I attempted to manually parse today's date and time, but this feels li ...

Executing Selenium Application on a machine separate from the one where it was originally developed

After developing a Console App using VS2015 that utilizes Selenium WebDriver to test various websites, I am now looking to deploy it onto a server for remote execution. Despite already installing the app on the server, I encountered an error stating that ...

Using Javascript to save a numeric value and accessing it on a different webpage

I'm encountering an issue with a specific feature on my website. I want users to click on a hyperlink that will redirect them to an application form page. The challenge is ensuring that the reference number (a 5-digit code displayed as a header) is st ...

Looking to upload a .json file into MongoDB using Java programming

I'm new to MongoDB and I have it up and running. My task is to insert the Student.json file into MongoDB using Java code instead of mongoimport. Student.java public class Student { @Id private ObjectId Id; private long studentId; private String st ...

How can TypeScript rules be incorporated into a Next.js project without compromising next/core-web-vitals?

In my current NextJS project which is in typescript, I have the following configuration in my .eslintrc.json: { "extends": "next/core-web-vitals" } Now, I want to include additional typescript rules, such as enforcing the rule of n ...

Establishing the types of object properties prior to performing a destructuring assignment

Consider a scenario where a function is utilized to return an object with property types that can be inferred or explicitly provided: const myFn = (arg: number) => { return { a: 1 + arg, b: 'b' + arg, c: (() => { ...

Minimize the cyclomatic complexity of a TypeScript function

I have a typescript function that needs to be refactored to reduce the cyclometric complexity. I am considering implementing an inverted if statement as a solution, but it doesn't seem to make much of a difference. updateSort(s: Sort) { if (s.ac ...

Using trackBy in conjunction with the async pipe

Attempting to utilize the async pipe along with *ngFor to exhibit an array of items obtained asynchronously. <ul> <li *ngFor="let item of items | async; trackBy: trackPost"> {{item.text}} </li> </ul> ngOnInit() { // a si ...

How can I get a song to start playing on Last.FM?

I am working on creating an Android app that utilizes the Last.FM API to play music, but I am struggling to find the right methods that will allow me to access mp3 files. So far, I have only been able to retrieve track, artist, and chart information from ...

"Unlocking the Power of Python Selenium for Web Automation: Mastering the Art of Click

My current project involves automating the downloads of specific files for my organization. I recently discovered Browser Automation with Selenium, which has allowed me to inject user credentials into a webpage. However, I am now faced with the challenge ...

loading a module's dependencies seamlessly with RequireJS

Currently, I am working with Knockout and Require in my project. I have isolated some Knockout handlers into a separate module that I want to utilize. While there is no specific JavaScript code relying on this module, it is referenced in the data-bind attr ...

Struggling to display a chart using angular-chart

I am facing an issue with rendering my chart. I have followed the instructions provided on the GitHub page of angular-chart.js. I have created a plunker to showcase my problem: http://plnkr.co/edit/x7XJhxxvYMzWr3u7lBcJ?p=preview Although I can access and ...