The Rock
The Rock

Reputation: 23

Drop down selection issue in java selenium

I am trying to automate checkout scenario. Having issue in selecting value from drop down in cart. Please find steps:

  1. Search item and select the item
  2. Select size and quantity
  3. Click on add to cart
  4. Go to cart
  5. Try to update the quantity. Select 10+ from drop down and enter qty> 10 and click on update.I am having issue with step 5.It just clicks qty but no drop down display to select option. No error also.

https

 driver.get("https://www.amazon.com/");
 driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
 driver.manage().window().maximize();
 driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")).sendKeys("football");
 Thread.sleep(2000);
 driver.findElement(By.xpath("//*[@id='issDiv0']")).click();
 driver.findElement(By.xpath("//*[@id='result_0']/div/div/div/div[2]/div[2]/a/h2")).click();
 driver.findElement(By.xpath("//*[@id='native_dropdown_selected_size_name']")).sendKeys("Junior");
 Thread.sleep(3000);
 driver.findElement(By.xpath("//*[@id='quantity']")).sendKeys("2");
 driver.findElement(By.xpath("//*[@id='add-to-cart-button']")).click();
 driver.findElement(By.xpath("//*[@id='hlb-view-cart-announce']")).click();
 driver.findElement(By.xpath("//*[@id='a-autoid-2-announce']/span[2]")).click()​;
 List<WebElement> elements = driver.findElements(By.className("a-dropdown-item quantity-option"));
 System.out.println(elements.size());
 for (WebElement el : elements) {
 System.out.println(el.getText());
 }

<div class="a-popover-wrapper">
<div class="a-popover-inner" style="height: auto; overflow-y: auto; min-width: 75px; width: auto;">
<ul class="a-nostyle a-list-link" aria-multiselectable="false" role="application" tabindex="-1">
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_0" aria-checked="true" role="option" tabindex="0">
<a id="dropdown1_0" class="a-dropdown-link a-active" data-value="{"stringVal":"1"}" href="javascript:void(0)" tabindex="-1"> 1 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_1" role="option" tabindex="0">
<a id="dropdown1_1" class="a-dropdown-link" data-value="{"stringVal":"2"}" href="javascript:void(0)" tabindex="-1"> 2 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_2" role="option" tabindex="0">
<a id="dropdown1_2" class="a-dropdown-link" data-value="{"stringVal":"3"}" href="javascript:void(0)" tabindex="-1"> 3 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_3" role="option" tabindex="0">
<a id="dropdown1_3" class="a-dropdown-link" data-value="{"stringVal":"4"}" href="javascript:void(0)" tabindex="-1"> 4 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_4" role="option" tabindex="0">
<a id="dropdown1_4" class="a-dropdown-link" data-value="{"stringVal":"5"}" href="javascript:void(0)" tabindex="-1"> 5 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_5" role="option" tabindex="0">
<a id="dropdown1_5" class="a-dropdown-link" data-value="{"stringVal":"6"}" href="javascript:void(0)" tabindex="-1"> 6 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_6" role="option" tabindex="0">
<a id="dropdown1_6" class="a-dropdown-link" data-value="{"stringVal":"7"}" href="javascript:void(0)" tabindex="-1"> 7 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_7" role="option" tabindex="0">
<a id="dropdown1_7" class="a-dropdown-link" data-value="{"stringVal":"8"}" href="javascript:void(0)" tabindex="-1"> 8 </a>
</li>
<li class="a-dropdown-item quantity-option" aria-labelledby="dropdown1_8" role="option" tabindex="0">
<a id="dropdown1_8" class="a-dropdown-link" data-value="{"stringVal":"9"}" href="javascript:void(0)" tabindex="-1"> 9 </a>
</li>
<li class="a-dropdown-item quantity-option quantity-option-10" aria-labelledby="dropdown1_9" role="option" tabindex="0">
<a id="dropdown1_9" class="a-dropdown-link" data-value="{"stringVal":"10"}" href="javascript:void(0)" tabindex="-1"> 10 + </a>
</li>
</ul>
</div>
</div>

Upvotes: 1

Views: 1259

Answers (3)

Jeremiah
Jeremiah

Reputation: 1145

I was able to get quantity dropdown to choose a value using the test below. I've modified your initial implementation in a couple of ways:

Functional Modifications

  • Updated provided By.xpath lookups to By.id (where possible)
  • Wrapped dropdown WebElement references in Select decorators and used the byVisibleText method to make a selection.
  • Removed calls to Thread.sleep in place of a WebDriverWait, which applies timing conditions more consistently among the lookups.

Cosmetic Modifications

  • User-entry data has been extracted to named String variables

  • All By lookups have been extracted to named variables.


    @Test
    public void buyFootballTest() {

    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
    driver.get("https://www.amazon.com/");
    driver.manage().window().maximize();

    // Entry values.
    String searchForFootball = "football";
    String footballSizeVisibleText = "Junior";
    String purchaseQtyVisibleText = "2";

    // "By" locators used for process.
    By searchFieldBy = By.id("twotabsearchtextbox");
    By searchFieldFirstSuggestionBy = By.id("issDiv0");
    By firstResultTitleBy = By.xpath("//*[@id='result_0']/div/div/div/div[2]/div[2]/a/h2");
    By footballSizeDropdownBy = By.id("native_dropdown_selected_size_name");
    By purchaseQtyDropdownBy = By.id("quantity");
    By addToCartBy = By.id("add-to-cart-button");

    // This will wait a MAXIMUM of 15 seconds, but will end early if conditions are met.
    WebDriverWait noThreadSleep = new WebDriverWait(driver, 15);
    noThreadSleep.pollingEvery(250, TimeUnit.MILLISECONDS);

    // Enter the search term
    noThreadSleep.until(ExpectedConditions.visibilityOfElementLocated(searchFieldBy)).sendKeys(searchForFootball);
    noThreadSleep.until(ExpectedConditions.elementToBeClickable(searchFieldFirstSuggestionBy)).click();

    // Select the Football we want to buy
    noThreadSleep.until(ExpectedConditions.elementToBeClickable(firstResultTitleBy)).click();
    WebElement sizeDropdown = noThreadSleep.until(ExpectedConditions.visibilityOfElementLocated(
        footballSizeDropdownBy));
    Select sizeSelect = new Select(sizeDropdown);
    sizeSelect.selectByVisibleText(footballSizeVisibleText);

    // Choose to buy two.
    // driver.findElement(purchaseQtyDropdownBy).sendKeys(purchaseQty);
    WebElement qtyDropdown = noThreadSleep.until(ExpectedConditions.visibilityOfElementLocated(
        purchaseQtyDropdownBy));
    Select qtySelect = new Select(qtyDropdown);
    qtySelect.selectByVisibleText(purchaseQtyVisibleText);

    // Add to cart
    noThreadSleep.until(ExpectedConditions.elementToBeClickable(addToCartBy)).click();

    /* Continue doing things.*/
    }

Upvotes: 1

Anand Thanappan
Anand Thanappan

Reputation: 93

Hey I think you missed that <a> tag inside the <li> tag. Try the below code replacing your 'List elements' statement.

List<WebElement> elements = driver.findElements(By.cssSelector(".a-dropdown-item.quantity-option>a"));

Or you can use the below css selector value to pick all the dropdown values.a[id^="dropdown1_"].

Hope this will clear the issue. Make sure the application waits till the dropdown elements to show up in the screen.

Upvotes: 0

VVV
VVV

Reputation: 68

Please try the below code

new Actions(driver).moveToElement(webelementofdropdown).sendKeys("10+").build().perform();

Upvotes: 0

Related Questions