Tips for choosing an option from a custom dropdown menu in CSS using Selenium WebDriver and Java

I'm attempting to programmatically select an item in a CSS dropdown using Selenium, specifically on the Google hotel review page. On this particular page here, I am trying to select the most recent reviews instead of the default "most useful" sorting.

Based on my research, I have attempted to use the following code:

 Select select = new Select(driver.findElement(By.xpath("//*[@id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
 select.deselectByIndex(1);

However, I encountered an exception (

org.openqa.selenium.support.ui.UnexpectedTagNameException
) which states:

Element should have been "select" but was "g-dropdown-menu"

My question is, is there a way to simulate a click on a CSS dropdown element like this using Selenium web driver?

Answer №1

Explanation:

When working with Selenium Java API, the Select class is only suitable for dropdown menus that use the HTML select tag. If you are dealing with dropdowns implemented in a different way, such as using a JQuery dropdown plugin, the Select class won't work. In these cases, you will need to click on the dropdown to display the options and then manually choose the desired option.

Resolution:

public void selectSortby(String sortBy) {
  // Click on the dropdown to expand the options
  driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
  // Choose the desired option
  driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).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

Difficulty adjusting table heights in Dreamweaver, Struggling to set table height with <p style declarations

I'm currently designing an email signature in Dreamweaver using a table. However, I encountered an issue when I added the <p=style tag to customize my font size and color - it caused my table heights to change unexpectedly. Despite trying various s ...

Tips for manipulating lists within Python using selenium technology?

My task involves entering data into a web portal using entries from an excel file that I have imported into Python and converted into lists for easy access. To explain my approach in the code: I first locate and input the values of the first three elemen ...

Vuetify Container with a set maximum width that remains fixed

I recently started developing a web application using Vue.js and Vuetify (https://vuetifyjs.com/en/). Currently, I have a basic layout with 3 columns. However, I noticed that the width of these columns is restricted to a maximum of 960px due to some defau ...

Automating desktop tasks with Winium Java to streamline processes and increase efficiency

Is there a way to automatically close tabs if I have more than 5 open? I am currently using selenium to open different tabs, but now I need to implement a function that will automatically close any additional tabs beyond the limit of 5. I attempted to us ...

Why does JUnit not complete when mocking a method that returns a CompletableFuture object?

public class A{ @Autowired B b; public List<String> method1(){ CompletableFuture<List<String>> completableFuture = b.getCompletableFutureList(); // additional processing // returning a list of strings ...

What is the reason behind not initializing array elements in an enhanced for loop?

When I iterate through an array using a normal for-loop, each element is initialized as expected: Object[] objs = new Object[10]; for (int i=0;i<objs.length;i++) objs[i] = new Object(); However, when I use a for-each loop, the array el ...

The responsive class seems to be malfunctioning within the Laravel 5.3 framework

I have a project that involves Laravel and Vue.js. In my Laravel project, I am importing Bootstrap CSS using the following line: // Bootstrap @import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap"; This import is done in the app.scss file. ...

Handling exceptions in a pre-filter for controllers (Spring Security)

I am facing an issue regarding exceptions at the Spring security level. For authorization, I utilize the SM_USER header, and DelegateRequestMatchingFilter is used for validation to determine if the SM_USER is required. The problem arises when there is no ...

The v-checkbox appears much larger in size and has a different row size when compared to the v-radio

Currently, I am working on an application using Vuejs 3 with Vuetifyjs 3, and I have encountered an issue regarding the row size difference between a v-checkbox and v-radio when set to density="compact". The discrepancy in line height can be seen in the im ...

Element that appears to be hidden

Looking to add a unique touch with a "chat bubble" design on my webpage. Below is the code I have so far, but I seem to be facing issues with getting the triangle shape to display. Any suggestions on what might be causing this problem? .playernamechat ...

Forms gently float alongside one another

My goal is to have my two forms displayed side by side, but currently they are appearing stacked on top of each other. Below is the code I am using: <id ='AvailabilityFormAM'><form action='availability.jsp' name='availFo ...

Creating an HTML design with divs: a trio of pictures that adjusts in size to match the viewport (sl

As a newcomer to HTML and CSS, I find myself nearing completion of my website, with the final obstacle being an image slider in the background. The Challenge: The images are not perfectly centered within the viewport. Specifically, the first image needs ...

Tips for quickly accessing Selenium elements immediately upon availability, instead of waiting for the entire page to fully load

driver = webdriver.Chrome() #driver.set_page_load_timeout(10) driver.get("sitename.com") driver.find_element_by_id("usernameId").send_keys("myusername") Not setting a page load time proved to be counterproductive as the element was retrieved before the ...

Adjust the button's color even after it has been clicked

My goal is to update the button's color when it's clicked. I found some examples that helped me achieve this, but there's an issue - once I click anywhere outside of the button, the CSS class is removed. These buttons are part of a form, and ...

MariaDB hibernate naming convention (UPPER_SNAKE_CASE) for optimal database management

As I work on my application, I utilize flyway for data migration. In order to adhere to a consistent naming convention, all table names are defined in UPPER_SNAKE_CASE (for example: users -> USERS, candidateGroups -> CANDIDATE_GROUP). I have configur ...

What is the approach for incorporating JavaScript variables in Jade templates for writing inline CSS styles?

Struggling to inject CSS code dynamically? You're not alone. style(type='text/css') #header a#logo { background:url(constants.logo) no-repeat; } @media only screen and (-webkit-min-device-pixel-ratio: 1.5) { #header a#logo { ...

Even with noCursorTimeout set, I'm still encountering a "mongo cursor not found"

Encountering a persistent issue with my Java MongoDB client, where I am consistently receiving the below exception: Caused by: com.mongodb.MongoCursorNotFoundException: Query failed with error code -5 and error message 'Cursor 105639140478 not found ...

How can I scroll within a div using Selenium IDE?

<div id="aaa"> [lots of text] SHOW ME</div> #aaa { width: 100px; height: 100px; overflow: scroll; } http://jsfiddle.net/3NXtJ/ Can a scroll be implemented to display the text "SHOW ME" when scrolling down in the designated div? I have bee ...

Issue with long text in a resizable table using jQuery tablesorter 2.31.1

My issue is that when I try to resize the columns in tablesorter, they snap back into place. The column width set with resizable_widths does not apply correctly until a column is manually resized. Despite setting the width of all cells to 120px, any cells ...

Problems with implementing JavaScript code in a WebView

I am currently working on an android WebView project where I have managed to change the background color to orange with this code snippet. @Override public void onPageFinished(WebView view, String url) { wv.loadUrl("jav ...