Discovering the second value with a matching CSS class using Selenium WebDriver

How can I select the second value from a list item that appears as a drop-down on a web page? In this case, the second value is 'Bellevue'. Refer to the screenshot below:

I am implementing Page Objects and trying to locate elements using @FindBy annotations. Here is my attempted solution:

@FindBy(how=How.CSS,using = "a[id^='ui-id-'][1]")

However, I encountered the following error:

The given selector a[id*='ui-id-'](1) is either invalid or does not result in a WebElement

Alternatively, when I tried:

@FindBy(how=How.CSS,using = "a[id^='ui-id-']"[1])`

I received the error:

the type of expression must be an array but it resolved to string`.

I managed to make it work by using the following code:

WebElement value = driver.findElements(By.cssSelector("a[id^='ui-id']")).get(3);

value.click();

However, I need to implement @FindBy and have been unsuccessful with the get(3) method.

Answer №1

Indexing elements using [] in CSS selectors is not possible.

If you want to achieve a similar result, consider the following:

@FindBy(how=How.CSS,using = "li.ui-menu-item:nth-of-type(2) > a[id^='ui-id-']")

Another option is to use XPath:

@FindBy(how=How.XPATH,using = "(//a[starts-with(@id, 'ui-id-')])[2]")

Answer №2

One way to handle this situation is by implementing a custom method that allows you to access the necessary element without explicitly defining it as a web element using the FindBy annotation. Here's an example:

public void customMethod(WebDriver driver) {
   WebElement element = driver.findElements(By.cssSelector("a[id^='ui-id']")).get(3); 
   element.click();
}

Answer №3

As far as I can remember, XPath can be utilized to achieve a task similar to the following: @FindBy(how=How.XPATH,using = "(//a[contains(@class, 'ui-id-')])[2]") where the [2] at the end indicates the second element retrieved by the preceding XPath.

(apologies for any inaccuracies, this is based on my recollection - I have not verified this specific XPath)

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

Placing an image on the chosen option of a <mat-select> using Angular Material

I have incorporated Angular Material into my Angular 2 project, and I am trying to insert a static image (HTML element) in the selected value of mat-select. Unfortunately, I have been unable to find a solution for this issue. Is there anyone who can ...

Having trouble scraping YouTube views effectively using Selenium

Currently working on a web scraping project for my university assignment, and I've hit a roadblock with extracting data from YouTube. Specifically, I'm struggling with retrieving the number of views. Upon inspecting the source code of the YouTub ...

Creating a text container in PHP for displaying information

As someone who is new to PHP and HTML, I'm curious about the CSS statements needed to create a text box that will display a value from one of my PHP functions. Right now, the function retrieves data from an SQL server and returns it, but I would like ...

Having trouble with CSS basics? Seeing properties change across multiple classes?

My CSS skills are still in the beginner stage and I am currently struggling to troubleshoot an issue with my code. The HTML: <div id="loginForm"> <span class="dottedLink"><a href="resetlogin">Recover login details</a></span ...

Strategies for effectively managing pop-up windows to interact with their contents

Having an issue with handling popups in my test case. The control is not switching to the popup window, instead it's showing the getTitle of the main window. Can someone take a look at the code below? @Test public void testText1() throws Exception { ...

How can you find the "et-custom-css" section in WordPress?

One of the challenges I'm facing with my WordPress page is that some CSS classes are located under <style type="text/css" id="et-custom-css">. Additionally, there is a comment at the top of the CSS saying: /* - - - - - - Additional page info - ...

The ngModel in Angular 2 does not update when a selection is made

Two select options are available, where the second one is dependent on the first: <div class="form-group"> <label class="col-xs-12 col-sm-2 control-label">Vehicle Type:</label> <div class="col-xs-12 col-sm-10"> ...

When scrolling down a webpage, the background color of the content does not stretch along with the page

I've created a webpage with a sticky header and footer, along with a scrollbar on the main content. However, I'm facing an issue where the background color of the content does not extend beyond the viewport when scrolling down. The text is visibl ...

The slicing of jQuery parent elements

Hey there! I recently created a simulated "Load More" feature for certain elements within divs. However, I've encountered an issue where clicking on the Load More button causes all elements in both my first and second containers to load simultaneously ...

How to extract and print content within a div element using Selenium and Python

Looking to extract data from a website that has information nested under a specific div tag. Here is an example: <div> id="searchResults" class="multiple-view-elements" <span>name</name> <span>info</name> <span>info< ...

Incorporating a classList.toggle into a snippet of code

button, p, h1, h2, h3, h4, h5, a{ /* Define specific elements to use "fantasy" font */ font-family: Tahoma; } #main_body{ margin: 0px auto; background-color: #dedede; } #top_body{ /* Remove margin for simplicity */ } #t ...

What is the most effective way to alter the font within this Bootstrap template?

Check out the source code on Github. I've been attempting to modify the font-family for both h1 and body elements, but it's not working as expected. I must be overlooking something simple, but I can't seem to pinpoint the issue. It's d ...

Adding a CSS class to an HTML element using JQuery

I have a collection of tabs, and I want to dynamically add an <hr> element with the class "break-sec" when a certain class is active. This element should be placed within a <span> element with the class "vc_tta-title-text" after the text. Here ...

Looking to replace a background image using javascript?

(apologies for any language mistakes) I have multiple divs with a common background image. I assigned them the same class and set the background image using CSS in style.css which worked perfectly fine. Now, I want to change the background image dynamical ...

I am in need of setting up two rows side by side using the display

I'm trying to have two rows side by side using display grid, but I'm facing some issues. When I use the following CSS... .grid-container { display: grid; grid-template-rows: auto auto; grid-template-columns: auto; grid-column-gap: 100px;} It cre ...

"Enhance the Navbar with a pop of color by changing

https://i.sstatic.net/dHKsV.pngMy Navbar is already defined and working perfectly, but I want to add a full-page background to the Navbar links. Currently, on mobile devices, the link backgrounds do not cover the entire page horizontally and instead look l ...

Is there a way to develop a login form that retrieves information from a Google Sheet database?

Please help me with a solution. I have developed a registration form which effectively saves users' information in a Google Sheet. However, now I am yearning to create a login form that verifies the stored data and redirects the user to a different pa ...

Challenges arise when media queries fail to activate properly

Greetings! I am currently working on enhancing the responsiveness of my webpage using Media Queries. However, I seem to be facing a challenge with the second image not changing as expected when I reduce the browser size. In the HTML, 'top-background- ...

Having trouble with your custom accordion content in React JS not sliding open?

Check out my progress so far with the fully functioning view here This is the structure of the Accordion component: const Accordion = ({ data }) => { return ( <div className={"wrapper"}> <ul className={"accordionList ...

Using Vue.js to dynamically assign CSS classes based on variables

Is it possible to achieve this with a dynamic class like the following? <div :class="'outofstock.item_' + item.id ? 'bg-green-500' : 'bg-red-500' "> I have been struggling to find the correct syntax for this. T ...