下载java selenium中的选择问题

时间:2016-07-27 23:12:40

标签: java selenium-webdriver drop-down-menu

我正在尝试自动化结帐方案。从购物车中的下拉菜单中选择价值时遇到问题。请找到步骤:

  1. 搜索项目并选择项目
  2. 选择尺寸和数量
  3. 点击添加到购物车
  4. 转到购物车
  5. 尝试更新数量。从下拉列表中选择10+并输入qty> 10并点击update.I我遇到了第5步的问题。它只是点击数量,但没有下拉显示选择选项。没错也。
  6. 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>

3 个答案:

答案 0 :(得分:1)

我可以通过下面的测试获得数量下拉菜单以选择值。我已经通过几种方式修改了您的初始实施:

功能修改

  • 将提供的 By.xpath 更新提供给 By.id (如果可能)
  • 选择装饰器中的包装下拉列表 WebElement 引用,并使用 byVisibleText 方法进行选择。
  • 删除了对线程的调用。睡眠代替 WebDriverWait ,这会在查找中更一致地应用计时条件。

化妆品修改

  • 用户输入数据已提取到命名的 String 变量

  • 所有查询已被提取到命名变量。

    @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.*/
    }

答案 1 :(得分:0)

请尝试以下代码

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

答案 2 :(得分:0)

嘿,我认为您错过了<a>标记内的<li>标记。请尝试使用以下代码替换“List elements”语句。

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

或者您可以使用以下css选择器值来选择所有下拉值。a[id^="dropdown1_"]

希望这将解决问题。确保应用程序等待,直到下拉元素显示在屏幕上。