gpt4 book ai didi

java - 如何使用 selenium 网络驱动程序访问数组的第一个元素并在运行时单击它?

转载 作者:行者123 更新时间:2023-11-29 05:40:07 25 4
gpt4 key购买 nike

我有一个 id="product-size"和项目 S、M、L、XL 的下拉列表。

<select id="product-size" onchange=" addToWishList();">
<option>Select</option>
<option id="2119362" value="4">S</option>
<option id="2119363" value="7">M</option>
<option id="2119364" value="8">L</option>
<option id="2119365" value="4">XL</option>
</select>

我使用了一个数组来存储这些项目,在运行时我需要访问第一个元素“S”。我面临的问题是,我无法在运行时点击第一个元素 S。我写的代码如下:

driver.get("https://m.staging.karmaloop.com/product/The-Infinity-Tee/407819");
WebElement j =driver.findElement(By.id("product-size"));
String text = j.getText();
String[] DDLcount =text.split("\n");
for (int i=1;i<=DDLcount.length-1;i++)
{
driver.findElement(By.xpath(Testconfiguration.size_dropdown_10deep)).click();
Thread.sleep(5000);
driver.findElement(By.name(DDLcount[i])).click();
}

谁能帮我解决这个问题?

最佳答案

根据您提供的代码,您使用的选项选择器无效。

它们似乎没有name 属性

除了修改循环之外,如果不重构 DOM,您还可以使操作更快。

WebElement selectBox = driver.findElement(By.xpath(Testconfiguration.size_dropdown_10deep));
List<WebElement> options = selectBox.findElements(By.tagName("option"));
for ( WebElement option : options )
{
selectBox.click();
option.click();
}

关于java - 如何使用 selenium 网络驱动程序访问数组的第一个元素并在运行时单击它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17918199/

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