I am facing difficulty accessing the "All" drop-down menu on the Amazon.ca webpage using Selenium WebDriver in Java

Instructions:

  • To begin, navigate to the Amazon home page
  • Locate the "ALL" drop-down option at the top left corner of the page
  • Attempt to click on this option using Selenium WebDriver with Java

Sample Code Snippet:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.className("nav-search-scope nav-sprite"))).click();
         WebElement element=driver.findElement(By.className("nav-search-scope nav-sprite"));

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.className("nav-search-scope nav-sprite"))).click();
         WebElement element=driver.findElement(By.className("nav-search-scope nav-sprite"));

Answer №1

I trust that this information will be beneficial for you.

WebElement mainElement = driver.findElement(By.className("nav-search-scope nav-sprite"));
Select select = new Select(mainElement);        
java.util.List <WebElement> optionsList = select.getOptions(); // retrieve all options from the drop down 
int size = optionsList.size();
String [] buttonArray = new String[size];           
for (int k = 0; k < size; k++) {
    buttonArray[k] = optionsList.get(k).getText();
    if(buttonArray[k] == "All"){ 
        select.selectByVisibleText("All");
    }
}

Alternatively, you can utilize the following method to choose the 'All' option:

public static void getDropdownAllOption(WebDriver driver, WebElement mainElement)
{
    Select select = new Select(mainElement);        
    java.util.List <WebElement> optionsList = select.getOptions(); // grab all options from the drop down 
    int size = optionsList.size();
    String [] buttonArray = new String[size];           
    for (int k = 0; k < size; k++) {
        buttonArray[k] = optionsList.get(k).getText();
        if(buttonArray[k] == "All"){ 
            select.selectByVisibleText("All");
        }
    }

}

If you require selecting a drop down by visible text, you can implement the subsequent method:

public static void selectByVisible(WebElement mainElement, String selectedValue) 
{
    WebElement elementToSelect = mainElement;                  
    Select dropdown = new Select(elementToSelect);         
    dropdown .selectByVisibleText(selectedValue);
 }

Answer №2

Give this a shot

driver = new ChromeDriver();
        driver.get("https://www.amazon.ca/");
        WebDriverWait wait = new WebDriverWait(driver, 10);

        driver.findElement(By.className("nav-search-scope")).click();
        Select selects = new Select(driver.findElement(By.id("searchDropdownBox")));

        selects.getOptions().forEach(option -> System.out.println(option.getText() + " is Displayed : " + option.isDisplayed()));
        selects.getOptions().forEach(option -> System.out.println(option.getText() + " is Enabled : " + option.isEnabled()));
        selects.getOptions().forEach(option -> System.out.println(option.getText() + " is Selected : " + option.isSelected()));

        driver.quit();

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

Can we stub these types of functions in any manner?

One file named helperFunction.js contains the following code: module.exports = (arg1, arg2) => { \\function body } To use this function in another file named file.js, you can simply call it like this: let helperFunction = require(' ...

When attempting to run my Selenium tests using the TestNG Framework, both from the command line and Jenkins, I encountered an issue where the tests were not executing. The outcome showed that no tests were run, with no failures or skips recorded

Seeking guidance as a newcomer to Selenium and automation. My tests are functioning correctly in Eclipse when executed with Junit. However, when initiating the test via Jenkins or CLI, the outcome displays: Total tests run: 0, Failures: 0, Skips: 0 I hav ...

Troubleshooting: Issues with jQuery's Class selector

Having trouble setting up an alert to trigger when a specific class anchor tag is clicked inside a div. This is what my HTML section looks like... <div id="foo"> <a class='bar' href='#'>Next</a> </div> And h ...

retrieve the index from the chosen elements

Hey there! I'm facing a situation where I have a div with several child elements. Using jQuery, I want to find the index of a specific element within a certain selector. <div> <div class="red"></div> <div class="red"></ ...

Node.js Export Module Error - importance of separating concerns

I have been attempting to implement separation of concerns by using export module. Everything functions properly when used without separating concerns, but as soon as I try to import generateUrlArray() from const db = require('../db'), nothing se ...

Adding to and retrieving data from an array

I am relatively new to the world of JavaScript and jQuery, and I have spent the last three days working on a script. Despite my efforts to find a solution by searching online, I have been unsuccessful so far. It seems like my search skills are lacking. My ...

Icons positioned absolutely outside of components will not be responsive to user interactions

How can I ensure that the icon on my tab bar can be clicked only when it covers a specific part of the screen, and not when it is in other areas? Any suggestions on how to fix this? Image Below is the code for the tab bar: <View style={{ background ...

Communicating between Java and Erlang using sockets

I am encountering difficulties with socket reading and writing through threads. The server is written in Erlang and the client is in Java. My current approach is as follows: PrintWriter printer = new PrintWriter(socket.getOutputStream(), true); BufferedRe ...

Is it window.jQuery or just jQuery?

(function($) { // plugin code })(window.jQuery); It appears that this code produces almost the same effect as: (function($) { // plugin code })(jQuery); Is it necessary to use window.jQuery instead of just jQuery as the function argument? Does it serve ...

Unlocking Node.js packages within React JS is a seamless process

Currently, I am developing a React Serverless App with AWS. I am looking for ways to incorporate a Node JS specific package into the React JS code without requiring Node JS on the backend. One package that I need access to is font-list, which enables list ...

Removing OTP user input upon pressing the Backspace key can be achieved through the following steps

I've developed an OTP component but I am encountering two specific issues that are posing a challenge. The first problem arises when I hit the Backspace key - I want the input value to be deleted, followed by automatically moving to the previous inpu ...

Guide to using jquery-throttle-debounce with addEventListener

I'm feeling a bit lost and confused, so I'm attempting to simply "imitate what I see without truly understanding what I'm doing"... <script> // This example is functional: $window.scroll($.throttle(50, function(event) { // some ac ...

What is the best way to address (or disregard) a TypeScript error located within an HTML template?

I'm currently working on a VueJS-2 component with the following HTML template: <template> <div> <v-data-table :headers="headers" :items="items" :caption="label" dense hid ...

Transform the text area in preparation for a GET request

Trying to figure out how to pass the text from a textarea into the source attribute of an image tag while retaining all formatting, including line breaks. After some research, it seems that the best way to accomplish this is by base 64 encoding the text a ...

Disabling specific time slots in the mat select functionality

I have a scenario where I need to display time slots at 30-minute intervals using Mat Select. export const TIME=["12:00 AM","12:30 AM","01:00 AM","01:30 AM","02:00 AM","02:30 AM","03:00 AM&qu ...

What's the best way to vertically center a div with a 100% width and a set padding-bottom on the screen?

I am struggling with positioning the following div on my webpage. .div { width:100%; padding-bottom:20%; background-color:blue; } <div class="div"></div> I have been searching for a solution to vertically center this responsive div on my web ...

Is there a way to continue a failed fetch request?

I am curious about the possibility of resuming an incomplete fetch request if it fails due to a non-code-related issue, like a lost network connection. In one of my current projects, we send a large download via AJAX to the client after they log in. This ...

Do you know the steps to writing a directive on a class in Angular Js?

The restrict option is usually configured as: 'A' - for matching only attribute names 'E' - for matching only element names 'C' - for matching only class names 'M' - for matching only comments However, ' ...

Updating the position attribute of a mesh in Three.js using boxGeometry

I am currently working on a project that involves resizing a cube by clicking and dragging its faces. My approach is to update the position attribute on the buffer geometry object, and then either recreate the mesh or set the needsUpdate flag to let it up ...

What is the method to ensure an element is displayed in Selenium WebDriver?

Looking for assistance on how to choose options from a dropdown when the element is not visible and has a boolean attribute? Here's the HTML code snippet: <select id="visualizationId" style="width: 120px; display: none;" name="visualization"> & ...