1

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>

5
  • Please provide more information. Are you getting an exception, is it just not doing what you think it should? What is the specific condition you're seeing? Commented Jul 27, 2016 at 23:22
  • Its not doing below condition Commented Jul 27, 2016 at 23:32
  • Its not doing condition " clicking on the qty and selecting any other value from drop down". Tried using Select as well but dint work driver.findElement(By.xpath("//*[@id='hlb-view-cart-announce']")).click(); WebElement mySelectElm =driver.findElement(By.xpath("//*[@id='a-autoid-2-announce']")); Select select = new Select(mySelectElm); select.selectByVisibleText("9"); Commented Jul 27, 2016 at 23:41
  • Could you share your drop down HTML?? Commented Jul 28, 2016 at 1:01
  • i have added the drop down html. Commented Jul 28, 2016 at 4:01

3 Answers 3

1

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.*/
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Please try the below code

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.