What are some ways to broaden the list and select values that are constantly changing?

I am currently trying to find a solution for extracting the target of Add New from Prod2 in the provided code snippet.

In order to retrieve this target, I need to first click on ptv-icon ptv-collapse-icon. The challenge lies in the fact that the XPath changes every time the script is executed.

Is there a method available to directly locate and extract the target Add New without relying on the changing XPath?

Another issue I encountered is that the element Add New only becomes visible once I have clicked on ptv-icon ptv-collapse-icon.

<li class="ptv-listitem" data-id="1">
<div class="ptv-item">
<span class="ptv-icon ptv-expand-icon"></span>
<span class="ptv-text">
<span class="tree-text">Prod1</span>
<span class="tree-counts"></span>
</span>
</div>
</li>
<li class="ptv-listitem" data-id="2">
<div class="ptv-item">
<span class="ptv-icon ptv-collapse-icon"></span>
<span class="ptv-text ptv-selected">
<span class="tree-text">Prod2</span>
<span class="tree-counts"></span>
</span>
</div>
<ul class="ptv-list ptx-expanded">
<li class="ptv-listitem" data-id="-1">
<div class="ptv-item">
<span class="ptv-text">
<span class="tree-text">**Add New**</span>
<span class="tree-counts"></span>
</span>
</div>
</li>
</ul>
</li

Answer №1

(If you're coding in Java) Here's a suggestion: Use xpath to target specific elements with unchanging text, like so:

// Click on prod2
driver.findElement(By.xpath(".//li[contains(.,'Prod2')]//span[@class = 'ptv-icon ptv-expand-icon']").click();

// Click on add new
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(".//li[contains(.,'Add New')]"))).click();

Answer №2

To choose the specific item, you can target an element with two classes of ul and one class of span:

WebDriver.findElement(By.cssSelector("ul.ptv-list.ptx-expanded span.tree-text")).click()

This indicates selecting a span element that has the class tree-text, located within a ul element that possesses both the classes ptv-list and ptx-expanded.

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

Executing automated tests on web applications using Selenium Web Driver in conjunction with Azure Web

I recently developed a C# Selenium script that successfully opens a browser and captures a screenshot. The code functions perfectly on my local laptop, however, when attempting to deploy it on Azure WebJobs, the code doesn't run as expected. This coul ...

Is it time to advance to the next input field when reaching the maxLength?

In my Vue form, I have designed a combined input field for entering a phone number for styling purposes. The issue I am facing is that the user needs to press the tab key to move to the next input field of the phone number. Is there a way to automaticall ...

The function chartobject-1.render() is returning an error message stating: "Unable to locate the specified container within the document. This issue is occurring in the

I've encountered an issue while trying to integrate FusionCharts into my Vue.js project. Every time I attempt to render the charts within my components, I consistently run into this error. Here's a snippet of one of the components: <template&g ...

How can I stop a page from refreshing after a submission?

Currently, I am working on a PHP programming project that involves interacting with a database. I am facing an issue where upon submitting form data to be inserted into the database on the same page, if the page is reloaded, it shows an error. I have been ...

Updating the configuration settings for CKEditor 5 using Reactjs

I followed the configuration provided in another answer at But, I am facing an issue: Failed to compile. ./src/components/cards/CreateCard.js Line 59:22: Parsing error: Unexpected token, expected "}" 57 | editor={ClassicEditor} 58 | ...

Issue found in Next.js 14: Hydration Error | Caution: Client Components can only receive plain objects from Server Components

I'm currently in the process of creating an application using Next.js 14, TypeScript, Mongoose, and MongoDB. In this app, I retrieved user data from my database and displayed them on cards along with relevant information like tags represented by badg ...

The Android Ruby WebDriver encountered an EOFError at the end of the file

When testing my code with an Android emulator, everything runs smoothly. However, when I switch to using a real Android device (specifically tested on Samsung Galaxy S3 and HTC Wildfire), I encounter the following error: EOFError: end of file reached Just ...

Three.js: Plane visibility fluctuates with time

I've been working on a Three.js project where I created a rotating plane. However, I encountered an issue where the plane doesn't display half of the time. To better illustrate this problem, I have created a demonstration in this fiddle. ...

The collapse feature of the navbar-toggle is not showing up

My navbar is experiencing issues with the navbar-toggle collapse class. When I reduce the size of the page, the button meant to appear does not show up, even though other collapsing elements are working properly. <nav class="navbar navbar-inverse navba ...

Is there a way to manipulate a website's HTML on my local machine using code?

I am currently working on a project to create a program that can scan through a website and censor inappropriate language. While I have been able to manually edit the text using Chrome Dev tools, I am unsure of how to automate this process with code. I ha ...

What could be causing ngif to disregard priority levels?

I have developed a unique custom directive with a priority of 1000. Within the compile function of the directive, I am removing the ng-if attribute from the element. My belief is that since ng-if has a lower priority of 600, it should not undergo compilati ...

Distinguishing factors between Python objects and JSON objects

At first glance, it may seem that Python has native support for JSON. However, the one exception is that JSON is able to store JavaScript functions. My dilemma is this: I require passing JSON to a Python file via the terminal. Is it advisable to use eval( ...

I'm experiencing an issue where an annoying new tab keeps popping up whenever I click on a link, which is definitely

I've encountered a problem with my automation using selenium-webdriver. Each time I click on a link, it unexpectedly opens in a new tab even though the links do not have the _blank attribute set to them. This causes my automation to fail as it remains ...

Attempting to dynamically link my "ng-true-value" in AngularJS

Working with values retrieved from a database, my goal is to include a checkbox, load the description, and provide 2 text boxes – one editable and the other read-only. Both text boxes initially display values from the database. When the checkbox is chec ...

Add a variety of input values to a div in designated spots

Objective: The aim is to allow the user to connect form fields with specific locations within a content from another div, and then add the text entered in individual input fields to those corresponding locations. Here is the link to the fiddle HTML: < ...

Utilizing Lodash to extract properties, dividing arrays, and obtaining distinct values

I found a more effective method to accomplish the task in my JavaScript project by utilizing the Lodash library. The approach involves extracting properties, splitting arrays, and obtaining unique values. var taskobj = [ {'taskno':'a&apos ...

Adjust the "dimensions of text, applications, and other elements" with Selenium in Python

I am currently utilizing the Selenium library in my Python automation project. The project was initially built with a specific "Scale and Layout" configuration on Windows 10, as shown in the image below: https://i.sstatic.net/5pISR.png However, not all l ...

Node for Angular forms workflow

I'm on the hunt for workflow nodes with forms that open when the user clicks on them. While I've come across a few options, not all of them are open source. Can you point me towards some open source (simple and basic) alternatives? Here's w ...

Leveraging AJAX with React Router

Currently tackling a project utilizing React Router, encountering some challenges with data flow. Each page triggers an AJAX call to fetch data for the component. These calls have been placed within componentDidMount: // The following code is in ES6 comp ...

Formik's handleSubmit function appears to be malfunctioning

I have encountered a puzzling issue with Formik implementation in my two components. Despite implementing Formik in the same way for both components, I am facing a problem where `handleSubmit` works in one component but not in the other. You can check out ...