gpt4 book ai didi

java - java selenium中的下拉选择问题

转载 作者:行者123 更新时间:2023-11-30 07:14:43 26 4
gpt4 key购买 nike

我正在尝试自动执行结帐场景。从购物车的下拉菜单中选择值(value)时遇到问题。请查找步骤:

  1. 搜索项目并选择该项目
  2. 选择尺寸和数量
  3. 点击“添加到购物车”
  4. 前往购物车
  5. 尝试更新数量。从下拉列表中选择 10+,然后输入数量> 10,然后单击更新。我在第 5 步中遇到问题。它只是单击数量,但没有下拉列表显示来选择选项。也没有错误。

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>

最佳答案

我能够使用下面的测试获得数量下拉菜单来选择一个值。我通过几种方式修改了您的初始实现:

功能修改

  • 将提供的 By.xpath 查找更新为 By.id(如果可能)
  • Select 装饰器中封装下拉 WebElement 引用,并使用 byVisibleText 方法进行选择。
  • 删除了对 Thread.sleep 的调用,以代替 WebDriverWait,这可以在查找之间更一致地应用计时条件。

外观修改

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

  • 所有By查找均已提取到命名变量。

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

关于java - java selenium中的下拉选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com