Exploring elements using dynamic xpaths in selenium

When working with Selenium WebDriver, I am facing difficulties selecting the

<input class="cs-autocomplete-input" tabindex="">
element due to its fluid ID. This is because there are multiple items with the same class name on the web page, making it challenging to pinpoint the specific element. Is there a way to effectively locate elements like this despite their dynamic IDs? Using
By.className("cs-autocomplete-input")
doesn't work in this case.

<div class="content">
    <div id="fluid-id-bhlpjo3p-1026">
        <input type="button" class="csc-repeatable-add-bhlpjo3p-1022 cs-repeatable-add" value="+">
        <ul class="cs-repeatable">
            <li class="csc-repeatable-repeat-bhlpjo3p-1022 clearfix cs-repeatable-repeat show">
                <input type="radio" class="csc-repeatable-primary-bhlpjo3p-1022 show cs-repeatable-primary" name="primary-fields.sponsors" value="true">
                <input type="text" class="csc-exhibition-sponsor cs-repeatable-content" id="repeat::.csc-exhibition-sponsor" name="repeat::.csc-exhibition-sponsor" style="display: none;">
                <input class="cs-autocomplete-input" tabindex="">
                <a href="#" class="cs-autocomplete-closebutton" title="Cancel edit, and return this field to the most recent authority value" style="display: none;"></a>
                <input type="button" value="" class="csc-repeatable-delete-bhlpjo3p-1022 cs-repeatable-delete" disabled="">
            </li>
        </ul>
    </div>

For reference, the xpath of this div is

//*[@id="primaryTab"]/div/div[3]/div[1]/div[2]/div[2]/div[1]/div/div[2]

Answer №1

If you're looking for a solution, consider trying the following snippet of code:

browser.findElement(By.id("primaryTag")).findElement(By.cssSelector(".cs-autocomplete-input[tabindex='']"))

Take a look at how the CSS is implemented below.

#someId .my-input[tabindex=''] {
  background: red;
}

/* Additional styles for better readability — please disregard */
input {
  display: block;
  margin-bottom: 1em;
  padding: 0.5em 1em;
}
<div id="someId">
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="" type="text" value=' me' />
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="" type="text" value='me' />
</div>

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

How can I show the localStorage value in an HTML5 template using Angular2?

I have two keys stored in localStorage and I want to display one of them on my template. I'm having trouble accessing these values. I even created an interface specifically for storing the value of the currentUser key from localStorage. How should I g ...

Adding Text Above a Material Icon in Angular: A Guide

Here is some sample HTML code to consider: <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <mat-icon class="fa fa-folder" style="font-size:48px;"><span style="color: re ...

Adding HTML content inside an iFrame at a specific cursor position in Internet Explorer

Does anyone know a method to insert HTML into an iFrame specifically for IE? I have been using execCommand insertHtml in Chrome and Firefox, but it doesn't seem to work in IE. I was able to paste HTML into a content editable div using pasteHTML, howe ...

Tips for downloading an Excel file without triggering a pesky pop-up window in Windows

Currently, I am facing an issue with downloading a spreadsheet using java and Selenium in headless mode. The test cases are executing perfectly fine when running on my local Windows computer, but not on the Linux server (CI/CD) where I need them to run. C ...

What is the best way to duplicate a CSS shape across a horizontal axis?

I am working on decorating the bottom of my page with a repeated triangle design. The image provided only displays one triangle, but I would like to extend it across the entire horizontal div. Here is a screenshot of my progress so far: https://i.stack.im ...

Ignored are the white spaces within a pre tag

From what I understand, the 'pre' tag is supposed to collapse all white spaces and tabs into one space. However, I have noticed that this doesn't happen for me. I'm curious as to why this may be the case. Could it possibly be dependent ...

Invoke the session on a different page and incorporate it into your JavaScript code

My php files, ajax.php and index.php, contain a mix of php code, html, and javascript. I am developing a quiz website where questions are retrieved from a database using sql in ajax.php and displayed on index.php through an ajax method. The user's sco ...

Creating a javascript function to update content on click

Recently, I've been designing a webpage and encountered an issue. I want the text in a specific area to change whenever a user clicks on a link. Below is the code snippet related to the section I want to modify using a JavaScript function. <div id ...

Experiencing a problem with cucumber and selenium wherein the driver consistently turns null in a straightforward test scenario

I am facing a challenge with my test automation framework. I have noticed that when directly finding elements inside the step definitions class, it works fine. However, when I try to implement a base class along with page object model and page factory, it ...

Verify the entered information in the input field and showcase it in the display box after pressing the ENTER key

I need to display selected values of a tree structure in a show box on the other side of the page using code. However, I also need to include HTML input boxes in some lines of the tree structure so that users can input data. The challenge is getting the in ...

Make sure that the webpage does not display any content until the stylesheet has been fully loaded by

I am seeking to utilize ng-href for loading different Themes. One issue I am encountering is that the unstyled content appears before the stylesheet is applied. I have created a Plunker where I made changes to Line 8 in the last 3 Versions for comparison ...

"Upload a video file and use JavaScript to extract and save the first frame as an image

I have a webpage where users can upload a video file, and the page will generate a thumbnail based on a timestamp provided by the user. Currently, I am focusing on generating the thumbnail from the FIRST frame of the video. Here is an example of my progr ...

Steps for setting up Windows7 to run Selenium with Firefox for Test-Driven Development (TDD) using Python

I am currently setting up my system (Windows 7 Pro 64 bit, Python 3.5 through Anaconda) to utilize Firefox with selenium for the purpose of following along with the book Test Driven Development with Python. Python continues to throw the error message WebDr ...

The element cannot be located by Selenium using either the ID or xpath

As I dive into the world of web automation using Selenium with Python, I encounter some challenges when trying to log in to a specific website for the first time. Despite practicing with simple examples and going through the Selenium documentation, things ...

Looking to reduce the size of a logo image within a container as you scroll down a webpage?

I've been working on creating a logo section for my website, but I'm struggling to make it shrink as users scroll down and expand back to its original size when they scroll up. I've tried various JavaScript and jQuery functions without succe ...

The out directory is lacking Styled JSX integration for Next.js

I have a straightforward project in NextJs and I am looking to serve the files via NginX. Here are the dependencies listed in my package.json file: "dependencies": { "isomorphic-unfetch": "^2.0.0", "next": "^7.0.2", "next-routes": "^1.4.2", ...

Personalized form outlines

I'm designing a Valentine's Day theme for my website and I was wondering if it's possible to create custom form borders, like a heart shape, without having to hard-code everything. I could always insert an image where needed, but that seems ...

What is the best way to ensure that the input search bar, microphone icon, and two small boxes adapt to different screen

Upon submitting my exercise for The Odin Project, I was under the impression that everything looked perfectly aligned and centered on my MacBook Pro 15-inch screen. However, when I viewed the completed webpage on a larger computer screen at work, I noticed ...

Scrapy with integrated Selenium is experiencing difficulties

I currently have a scrapy Crawlspider set up to parse links and retrieve html content successfully. However, I encountered an issue when trying to scrape javascript pages, so I decided to use Selenium to access the 'hidden' content. The problem a ...

How come padding-bottom isn't effective in increasing the position of an element?

My goal is to adjust the position of the orange plus sign (located in the lower right corner of the screen) so that it aligns better with the other icons, directly below them. I've tried using padding-bottom, but it doesn't seem to work, while pa ...