Selenium - Strategies for locating objects when they lack an ID or Name

Recently, I've delved into using Selenium Webdriver for automation purposes. The particular webpage that I'm looking to automate is infused with CSS styling. My current aim is to interact with a dropdown labeled "Admin", triggering it to display a list of options. From this list, I wish to select the option "User Access."

The snag lies in the fact that the source code of this specific dropdown "Admin" lacks an identifiable ID or Name attribute. Below you will find the relevant excerpt from the code:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false>
        Admin<span class="caret"></span>
    </a>
    <ul class="dropdown-menu" role="menu">

        <li>
            <a href="usersAdmin">
                Users Admin
            </a>
        </li>

        <li>
            <a href="userAccess">
                User Access Admin
            </a>
        </li>

        <li>
            <a href="#">
                Email Object Admin
            </a>
        </li>

    </ul>
</li>

My primary goal at present is to pinpoint and select the value "User Access Admin" within the dropdown. Despite trying to leverage the findElement method to locate this object, the absence of an identifiable ID or name has proven to be a hindrance. Any suggestions on an effective alternative approach for achieving this task? It's worth mentioning that my coding language of choice is Java.

Answer №1

Here is an example of how you can achieve the same result:

JavaScript

const element = driver.findElement(By.cssSelector("Users Admin"));

Ruby

element = driver.find_element(:link_text, "Users Admin");

PHP

$element = $driver->findElement(WebDriverBy::linkText("Users Admin"));

Answer №2

Is the CSS code provided below comprehensive enough?

a[href='userAccess']

Answer №3

If you're looking to access the dropdown menu and specifically pick the "Users Admin" option, you can achieve this by following these steps:

// Locate the dropdown menu and click on it
driver.findElement(By.linkText("Admin")).click();

// Find the desired element and choose it by clicking on it
driver.findElement(By.linkText("Users Admin")).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

Ways to update the title of a Magento widget

I've added a widget to showcase new products on my Home page and it's displaying perfectly. However, I want to get rid of the title "New Product" that comes with it. This pesky little thing needs to go! https://i.sstatic.net/P0NeS.jpg I'm ...

Add extra space to the dimensions of the div by incorporating padding

Showing my issue with an accompanying image: I am looking for a solution to include padding in the width: 50%; calculation. Currently, the first div's width is actually 50% + 2em because the padding was not factored in initially. Is there a way to i ...

Is there a way to maintain the values in an ArrayList in C# even when a page is refreshed or reloaded?

Is there a way to retain the values in an array list even after the page has loaded? I need these stored values for verification later on. I attempted using Http Cookie set but couldn't locate it. Can someone please assist? for (int i = 1; i <= ...

Navigating to the initial tab with Selenium Web Driver in Java

I'm currently using Selenium WebDriver with Java to automate a feature where clicking on a link opens a new tab. My goal is to create a global method to handle switching between tabs. I have a separate method for performing assertions on the second ta ...

Compatibility of Bootstrap 4 with collapsible elements and display utilities

I have noticed an issue with Bootstrap 4 display utilities not functioning properly with the .collapse class, a feature that worked seamlessly with Bootstrap 3. Despite my efforts to research this problem, I have been unable to find similar inquiries regar ...

Customizing CSS Styles in ASP.NET MVC for Multilingual Websites

We are looking to make our ASP-MVC website multicilingual and support different languages. One challenge I am facing is how to apply distinct style-sheets for each language, especially when dealing with right-to-left (RTL) languages like Hebrew or Arabic c ...

Exploring the techniques for displaying and concealing div elements with pure JavaScript

Here's an example of code I wrote to show and hide div elements using pure JavaScript. I noticed that it takes three clicks to initially hide the div elements. After that, it works smoothly. I was attempting to figure out how to display the elements ...

Setting the memory limit for OOM Killer specifically for the Chrome browser

Chrome is showing an error message: gfp_mask=0xd0, order=0, oom_score_adj=300 During my testing with the headless Chrome browser and Selenium, I encountered the above issue. ...

Skipping a step on a test in Serenity-Cucumber when it fails

In my Maven project, I utilize Java, Selenium WebDriver, JUnit, and Serenity with the Screenplay pattern. One of the challenges I'm facing is dealing with a login pop-up that appears sporadically. How can I incorporate a validation step for the pop-u ...

Enhancing browsermob precision with HAR files

I am currently utilizing browsermob in conjunction with selenium to generate a har file and then attempting to analyze the contents of that file. While examining the file, I have noticed timing information such as: 'startedDateTime': '2016-0 ...

Asserting that there are multiple elements being returned

I need assistance creating a method that can return two elements simultaneously. Here is the code I have so far: this.Wait.Until(ExpectedConditions.ElementExists(By.Id("mm_date_8"))); this.Wait.Until(ExpectedConditions.ElementExists(By.Id("dd_date_8"))) ...

Modify the background color of column headers in a Material UI DataGrid as you scroll down vertically

In my React(NextJS) project using material UI, I have implemented tables (DataGrid component from material UI) with distinct background colors in the column headers, as shown below: https://i.sstatic.net/zd3Iu.png However, I encountered an issue where ra ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

"Utilize jQuery to target and highlight specific HTML elements when redirecting

I am working with a list of elements and I want to be able to redirect the focus to a specific element using a URL. <div id="20"></div> <div id="21"></div> <div id="22"></div> // example url http://localhost: ...

Implementing jQuery stylesheet using a bookmarklet

Here's a question that may seem silly, but I'm looking to quickly apply CSS styles to a webpage by clicking on a bookmarklet. It will definitely save me time :) For example, this code does the trick: javascript:void( jQuery( ".fancybox-overlay- ...

Increase jQuery value by X each time it is clicked

I'm trying to create a custom next/prev navigation where clicking the "next" button will animate the margin left by X on each click until reaching the end (-320px), with a similar method for the back button. Here is the JavaScript code I've been ...

``I am experiencing issues with the Ajax Loader Gif not functioning properly in both IE

I am brand new to using ajax and attempting to display a loading gif while my page loads. Interestingly, it works perfectly in Firefox, but I cannot get it to show up in Chrome or IE. I have a feeling that I am missing something simple. The code I am using ...

Struggling to get the max-width property to function properly with a Bootstrap multi-level dropdown menu

I have a multi-level Bootstrap dropdown menu that I am customizing based on the design featured in this bootsnipp. However, I am encountering an issue where some of my menu items are too lengthy, so I need to restrict their width to a maximum of 400px. Ad ...

Make a div with absolute positioning overflow outside of a div with relative positioning that is scrollable

I am facing an issue with two columns positioned side by side. The right column contains a tooltip that overflows to the left on hover. Both columns are scrollable and relatively positioned to allow the tooltip to follow the scroll. However, the tooltip is ...

When writing CSS, ensure there is no space between selectors and classes of elements

Can you explain the distinction between using a css selector (like p) and assigning a class to an element (.cat)? p.cat{ } Vs. p .cat{ } Appreciate your help! ...