2
package com.web.automation;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class dropDown {
WebDriver driver;
    @BeforeMethod
    public void site() throws InterruptedException{
        System.setProperty("webdriver.gecko.driver", "geckodriver");
        driver =  new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://www.amazon.com/");
        }
    
        @AfterMethod
        public void close(){
            driver.close();
            }
        @Test
        public void register() throws InterruptedException{
        Select s = new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']")));
        s.selectByValue("search-alias=alexa-skills");
        }
}

Code Explanation:

I am trying to automate www.amazon.com web page. There is drop down list called "All" in the home page itself. if we click the All dropdown menu there will be different option to choose. Using Selenium automation I am trying to click the drop down and select one of the option.

Select s = new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']")));
s.selectByValue("search-alias=alexa-skills");

Error:

FAILED: register
org.openqa.selenium.ElementNotInteractableException: Element <option> could not be scrolled into view
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To select the option with text as Books from the dropdown you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector and selectByVisibleText():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select#searchDropdownBox")))).selectByVisibleText("Books");
    
  • Using xpath and selectByValue():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='searchDropdownBox']")))).selectByValue("search-alias=stripbooks-intl-ship");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Great! This will be helpful to me in the future. But I think I have located the element of search box instead of drop down button. because drop down is div element. – Krishnaveni Raju Sep 12 '20 at 15:27
  • Since I am following a tutorial, I believe soon I will find the solution to select the drop down option for div tag, because the above code I mentioned will work for "select" tag. Thanks for your response @DebanjanB. – Krishnaveni Raju Sep 12 '20 at 15:30
  • @KrishnaveniRaju Aren't you trying to interact with the same ` – undetected Selenium Sep 16 '20 at 12:58