Using regular expressions in selenium webdriver for css or xpath

I'm dealing with a code snippet

driver.findElement(By.cssSelector("#lmv-dialog-add-id-_newFundAllocation-targetAllocation-88-F03   >td.tright > a.lmv-show")).click();

The number 88 in the code is flexible. How can I adjust the code so it works regardless of whether it's 88, 99, 12, or any other number?

Furthermore, there are three XPath elements with similar naming:

id('lmv-dialog-add-id-_newFundAllocation-targetAllocation-88-F01')/x:td[3]/x:a
id('lmv-dialog-add-id-_newFundAllocation-targetAllocation-88-F02')/x:td[3]/x:a
id('lmv-dialog-add-id-_newFundAllocation-targetAllocation-88-F03')/x:td[3]/x:a

I need to click on the third item. The numerical value (88) here is variable and I want this action to work for any number that appears in its place.

Answer №1

Test out this xpath pattern

"//*[contains(@id,'lmv-dialog-add-id-_newFundAllocation-targetAllocation')]/td[3]/a[@class='lmv-show']"

Answer №2

If you want to target elements using XPath, you can experiment with a combination of the starts-with() and ends-with() functions:

//*[
     starts-with(@id, 'lmv-dialog-add-id-_newFundAllocation-targetAllocation-') 
        and 
     ends-with(@id, '-F11')
   ]
/td[contains(@class, 'tright']
/a[contains(@class, 'lmv-show']

Just a note: The example above demonstrates a simplified way to imitate a CSS class selector in XPath. For further options, you may refer to the following links as needed.

  • Selecting a css class with xpath
  • How can I find an element by CSS class with XPath?
  • CSS Selector to XPath conversion

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

Animation does not trigger before switching pages

I'm attempting to create an animation that occurs before the page transitions. After finding a jQuery script on this site and tweaking it to match my HTML, I realized the code works in a fiddle but not on my actual page. Any assistance would be greatl ...

Elements in Bootstrap Container class failing to align properly

Hello Everyone, I am attempting to achieve the same layout as demonstrated in [original image] where the "browse collection" div and the "request brochure" div are aligned with the four items above them. My approach involves using Bootstrap, and I assume ...

Connecting event listeners to offspring elements, the main element, and the entire document

My request is as follows: I have multiple boxes displayed on a webpage, each containing clickable divs and text. When a user clicks on a clickable div, an alert should appear confirming the click. Clicking on the text itself should not trigger any action. ...

Is it possible to obtain older versions of Selenium jars for download?

Is there a way to download previous versions of selenium jars, such as version 2.53? I am only seeing the option to download selenium 3.0 on seleniumHQ. ...

"Error: The .cssText method in Javascript is not working properly

I have been encountering an issue where the .cssText is not updating the HTML element referred to as .unveil. I implemented an event that when the container for .unveil is clicked, the .unveil element should become visible. However, upon clicking the .unve ...

Show jQuery block and make it fade in

I'm attempting to put together a simple tab system where each tab is supposed to appear using a fadeIn effect. In the code below, the .field element is initially hidden with CSS. Although the tabs are displayed correctly, the desired fadeIn effect is ...

Using the open file dialog feature within a Java Selenium test case

I'm currently working on an angular application and writing test cases using selenium. I encountered a scenario within one of my test suites where I need to trigger the opening of a Windows FileInput dialog by clicking a button, then selecting a file ...

How to trigger a JavaScript function using a button click

I've been struggling to figure out why my JavaScript code isn't functioning properly within Ionic. Can anyone guide me on how to store a number in a variable, display that variable, and increment it when a button is clicked? I have successfully ...

How can I prevent list items in jQuery Mobile from being truncated with ellipses?

Here is the list I am working with: <ul id="linksList" data-role="listview" data-inset="true" data-filter="true"> <!-- Dynamic contents! --> </ul> This list pulls its data from a local XML file (RSS feed). I am looking f ...

When scrolling, the menu is being overlapped by the Carousel in HTML5, CSS3, and

I am currently working on a One Page HTML webpage that needs to be responsive, and the requirement is to use Bootstrap. I have gone through all the tags but can't seem to identify the issue. One problem I encountered is that when I scroll down the pa ...

selenium.common.exceptions.WebDriverException: Notification: Process ended abruptly with status 1

version: firefox : Mozilla Firefox 61.0 geckodriver : geckodriver v0.20.1 I attempted to run the following code snippet only: from selenium import webdriver browser = webdriver.Firefox() However, I encountered the following error: Traceback (most recent ...

Finding the title of a link by hovering over it

While working on a particular page, I encountered an issue where some links only become visible after hovering over a specific link. It seems like there is a parent-child relationship between these links, causing Selenium WebDriver to have trouble identify ...

Arranging text in a horizontal position inside a grid container

My goal is to align the h4 tag "some other head" horizontally in my grid layout. Currently, they are not aligned as desired. Is there a way to achieve this alignment within the grid box? Thank you! Below is an example of my code: Current Code: https://c ...

Java's UnreachableBrowserException in Selenium

System.setProperty("webdriver.chrome.driver","D:/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://link"); driver.findElement(By.cssSelector("#username")).sendKeys("id"); driver.find ...

How can I implement 'blocked tails' using the :after pseudo-element?

Apologies, I'm uncertain of the right term for these elements. I have a module that reveals another box with information when hovered over. I want them to be visually connected by an angled rectangle or tail to indicate their association. Something a ...

The dimension of HTML on an IOS web application

After successfully designing a web app that works well on desktop browsers and iPad Safari, I encountered an issue when trying to install it as a hybrid app using cordova 3.3.0. The problem arises with the height not displaying properly. Despite including ...

Display a list in the modal with blank names when they are not visible

I am encountering an issue with a popup dialog that contains a table of items. The table displays a scrollbar when there are more than 10 items. I attempted to retrieve the names of all 17 items in the list, but only managed to get 10 names. Unfortunatel ...

I currently have two responsive menus and I'm trying to figure out how to modify the javascript so that when one menu is opened, the other

I am facing an issue with my responsive menus on a webpage, similar to the example provided in the jsfiddle link below. Currently, when one menu is open and I click on another, both remain open. How can I modify the JavaScript code so that when one menu op ...

How can I change the hover color of the navigation bar buttons in the Bootstrap freelancer template?

Having an issue with the hover color of the main navigation button menu while using the official freelancer template: https://github.com/BlackrockDigital/startbootstrap-freelancer The main button is displayed on narrower screens and should have a hover c ...

Positioning elements within a DIV using CSS

After using Position: Absolute for a while, I have realized that it's not working for my needs. I want my application to have a fluid layout that can adapt to different resolutions. My goal is to achieve the following: The left side features 3 calen ...