Having difficulty navigating the dropdown menu with Selenium Webdriver

https://i.sstatic.net/um1dw.pngI need help navigating the dropdown list with the following code:

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id
> =\\\"s2id_autogen4_search\\']"))).click();   
    driver.findElement(By.xpath("//*[@id
> =\"s2id_autogen4_search\"]")).click();   
    driver.findElement(By.id("s2id_autogen4_search")).sendKeys("New
> York");  

The HTML for the dropdown I am trying to automate is:

<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-4" id="s2id_autogen4_search" placeholder="" aria-activedescendant="select2-result-label-68">

Detailed HTML structure:

[HTML content here]

Answer №1

When discussing typing to trigger a drop down, consider using the following code:

Code :

WebElement drop_down = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id*='id_autogen'][type='text'][role='combobox']")));  
drop_down.click();
drop_down.sendKeys("New York");  

UPDATE1 :

This code will successfully interact with the drop-down after you first click on it.

Remember, you must click on the drop down before utilizing this code:

List<WebElement> options = driver.findElements(By.cssSelector("ul#select2-results-4.select2-results[role='listbox'] li span")); 
            for(WebElement option : options) {
                if(option.getText().trim().contains("New York")) {
                    option.click();
                }
            }

Screenshothttps://i.sstatic.net/NLNk5.png

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 extract a value from an object with no properties using jQuery

In order to perform statistical calculations like Averages (Mean, Median, Mode) on data extracted from Datatables, I need the automatic calculation to remain accurate even when the table is filtered. While I have managed to obtain the desired values, extra ...

The response body in Superagent is unknown until the next line is executed

While working on testing my express API using Jest and Supertest, I encountered an issue that I cannot seem to resolve: const id = await request.post('/program') .send(body) .expect('Content-Type', /json/) .expect(201).body ...

Deactivate the backface culling feature when loading an imported .obj file in three.js

I'm struggling with disabling wireframe rendering on the rear side of my vehicle. Any ideas on how to deactivate backface culling in wireframe mode for three.js? Check out this link for a helpful image ...

Rotating an HTML caret using CSS does not pivot from the center point

Is there a way to adjust my simple caret rotation to make it rotate from the center? What changes should I make? const Wrap = styled.div` width: 100px; background: grey; span { display: block; transform: ${(props) => (props.isOpen ? " ...

Consolidate and then transfer to a different database

How can I insert the data returned from my aggregate (controller function) into another collection called User? Here is the schema for the User: const userSchema = new mongoose.Schema({ firstName: { type: String, required: [true, &ap ...

Determine whether a subdomain is present within an ajax file in php

Looking for assistance with checking the existence of a 'Subdomain' in a file called '\example\sites'. Here's the code: $form = array() ; $form['name'] = "install"; $form['action'] = "?step=2"; $for ...

I am trying to set a background image specifically for one page in my application, but I am struggling to make it work. As a newcomer, I am still learning how to

I've been working on the code for a login page, but I'm having trouble setting a background image for the page. I know how to set it for the whole application by modifying the app component, but when I try to do the same for this specific compone ...

Move the alt attribute from the image to the paragraph text using jQuery or JavaScript and replace it

Struggling to crack the code conundrum that has been taunting me for what seems like an eternity! <div id="slider"> <ul> <!-- --> <li><a target="_blank" href="ANOUK-2014-De-l ...

Trouble with Sum Array Function

I've encountered an issue while trying to calculate the sum of a dynamic array. Instead of adding up the numbers, they are just being displayed next to each other. I attempted to sum 1+2+3+4+5, but the output shows as 012345 instead of 15. let userIn ...

Discover the locations where a mesh and a plane intersect using Three JS

After creating a three.js scene with a plane intersecting a mesh, I am trying to retrieve an array of points where the mesh's edge crosses the plane. Despite searching for solutions, I haven't found anything helpful so far. Please refer to the i ...

Toggle display of fields based on radio button selection in ASP.NET

I am new to C# asp.net and I am working on creating an online registration form. I am facing a challenge with the following issue. Could someone assist me, please? There are 5 fields in this problem: 1) Category: radio button with options for St ...

The recently added item in nestedSortable remains stationary and does not shift positions

const menu = $("ol.menu").nestedSortable({ handle: '.move', items: 'li', placeholder: 'placeholder', opacity: 0.7, toleranceElement: '> i', c ...

What is the best way to selectively remove and then later reinsert two items from an array in

I am facing a scenario where I have two buttons and an array to work with. The buttons are labeled "OR" and "AND", and the array consists of 7 items in a Dropdown list. When the user clicks on the "OR" button, it should remove 2 items from the array. Co ...

Utilizing Vue.js 2 to dynamically update data through directives

Is it possible to change a data property from a directive using single file components? For instance, consider the following code... export default { name: 'app', data: function() { return { is_loading: true ...

Retrieving a value in the app component from a map using Angular

I have been struggling to display the values of ValueM, ValueR, and product in my app.component.html file. Can anyone offer a solution or tip to help me move forward? Thank you very much. app.component.ts forkJoin( this.service1.method1(filter1), ...

Can JSON encoding in a URL pose a risk of XSS attacks?

To ensure my application has URL-friendly capabilities, I am storing its context as a JSON within the URL. This results in something like: http://mysite.dev/myapppage/target#?context={%22attr1%22%3A{%22target_id-0%22%3A{%22value%22%3A%223%22%2C%22label%2 ...

Implement a jQuery DataTable using JSON data retrieved from a Python script

I am attempting to create an HTML table from JSON data that I have generated after retrieving information from a server. The data appears to be correctly formatted, but I am encountering an error with DataTable that says 'CIK' is an unknown para ...

Next.js: How to retrieve route parameter within getServerSideProps

I need to retrieve data from my Supabase table using the ID provided in the URL slug, for example localhost:3000/book/1, and then display information about that specific book on a page built with Next.js. Table https://i.stack.imgur.com/t5z7d.png book/[ ...

Extracting the Instagram followers of a specific account

Is it possible to scrape followers from an Instagram account using Selenium? I am attempting to do so with the following URL: I am encountering a problem where I am unable to scroll down a popup page, with the only option being to navigate back. Below is ...

Unusual Behavior of JavaScript When Interacting with Bootstrap Sliders

I sought guidance from a web guru to enhance my code for creating a bootstrap slider per list item within a JavaScript for loop. However, the behavior of the slider has become inconsistent. At times it functions perfectly, while on other occasions it gene ...