[英]How to access the first element of an array and click the same at runtime using selenium web driver?
I have a drop list as id="product-size" and the items S,M,L,XL. 我有一个下拉列表作为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>
I have used an array to store these items and at Runtime I need to access the first element 'S'.The problem I am facing is ,I was not able to click on the first element S at runtime. 我使用了一个数组来存储这些项目,在运行时我需要访问第一个元素'S'。我面临的问题是,我无法在运行时单击第一个元素S. I have written the code as follows :
我编写的代码如下:
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();
}
Can anyone help me to sort out this problem ? 任何人都可以帮我解决这个问题吗?
From the code you've supplied, you're using an invalid selector for the Options. 从您提供的代码中,您使用选项的无效选择器。
They don't appear to have a name
attribute 它们似乎没有
name
属性
Aside from modifying the loop, you could make the operation faster if the DOM isnt reconstructed. 除了修改循环外,如果不重建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();
}
By locator = By.id("product-size");
Select select = new Select(webdriver.findElement(locator));
You can you use any of the three following options to select an item from dropdown 您可以使用以下三个选项中的任何一个从下拉列表中选择项目
select.selectByIndex(index); // Give Index as parameter
select.selectByValue(value); // Give the value of the option tag
select.selectByVisibleText(value); // Give the visible text as parameter
WebElement element =driver.findElement(By.id("product-size"));
Select sel = new Select(element);
List<WebElement> items = sel.getOptions();
boolean stringExits = false;
for(int i =0; i<items.size(); i++)
{
String text = items.get(i).getText();
if(text.equals("S"))
{
stringExists = true;
break;
}
}
if(stringExits)
{
System.out.println("The string exists");
}else
{
System.out.println("The string does not exist");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.