如何从下拉列表循环选择所有列表选项,选择每个选项。
try {
WebElement dropdown = RemindersElements.drpEvent(driver);
List<WebElement> dropdownOptions = driver.findElements(By.xpath("//[@id='leftContent']/div[2]/div[1]/div/select/option"));
System.out.println("Dropdown Size: " + dropdownOptions.size());
// Set value of dropdown using JS
for (int i = 0; i < dropdownOptions.size(); i++) {
String str = dropdownOptions.get(i).getText();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(String.format("arguments[0].value='%s';", str), dropdown);
}
} catch (Exception e) {
e.printStackTrace();
}
public static WebElement drpEvent(WebDriver driver) {
try {
element = driver.findElement(By.xpath("//[@id='leftContent']/div[2]/div[1]/div/select"));
} catch (Exception e) {
throw (e);
}
return element;
}
我想逐个选择所有选项。但是这里selenium没有给出任何错误,并且在上面的步骤之后执行下一步,我无法看到是否选择了任何选项。我想触发附加了该select元素的更改事件。并且它只会更改所选项目,但不会触发随附的更改事件。
更新
try {
WebElement dropdown = RemindersElements.drpEvent(driver);
List<WebElement> dropdownOptions = dropdown.findElements(By.xpath("//option"));
System.out.println("Dropdown Size: " + dropdownOptions.size());
// Set value of dropdown using JS
for (int i = 0; i < dropdownOptions.size(); i++) {
String str = dropdownOptions.get(i).getAttribute("value");
JavascriptExecutor jse = (JavascriptExecutor) driver;
Thread.sleep(1500);
jse.executeScript(String.format("arguments[0].value='%s';", str), dropdown);
Thread.sleep(1500);
}
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
我尝试使用类似<select>
的下拉菜单,就像您在页面中所拥有的那样:
public void foo() {
try {
Select dropdown = new Select(RemindersElements.drpEvent(driver));
System.out.println("Dropdown Size: " + dropdown.getOptions().size());
for (int i = 0; i < dropdown.getOptions().size(); i++) {
String text = dropdown.getOptions().get(i).getText();
dropdown.selectByVisibleText(text);
System.out.println("Option " + (i + 1) + ":" + text);
}
} catch (Exception e) {
e.printStackTrace();
}
}
您可能想要检查的一些事项:
dropdown
创建的RemindersElements.drpEvent(driver)
实例是否有效。打印总选项。根据您的屏幕截图,它应该打印6。
0
开头
参考页面: http://way2automation.com/way2auto_jquery/dropdown/default.html
更新1:
您可以尝试下面列出的其他几件事。只要您的RemindersElements.drpEvent(driver)
返回有效元素,这些就应该有效。
public void bar() {
try {
WebElement dropdown = RemindersElements.drpEvent(driver);
List<WebElement> dropdownOptions = dropdown.findElements(By.xpath("//option"));
System.out.println("Dropdown Size: " + dropdownOptions.size());
// Click the dropdown and click each option one by one
for (int i = 0; i < dropdownOptions.size(); i++) {
dropdown.click();
dropdownOptions.get(i).click();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void baz() {
try {
WebElement dropdown = RemindersElements.drpEvent(driver);
List<WebElement> dropdownOptions = dropdown.findElements(By.xpath("//option"));
System.out.println("Dropdown Size: " + dropdownOptions.size());
// Set value of dropdown using JS
for (int i = 0; i < dropdownOptions.size(); i++) {
String str = dropdownOptions.get(i).getText();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript(String.format("arguments[0].value='%s';", str), dropdown);
}
} catch (Exception e) {
e.printStackTrace();
}
}
更新2:
将其添加到RemindersElements类中:
public static List <WebElement> drpEventOpts(WebDriver driver) {
try {
List <WebElement> elements = driver.findElements(By.xpath("//[@id='leftContent']/div[2]/div[1]/div/select/option"));
} catch (Exception e) {
throw (e);
}
return elements;
}
然后替换这一行:
List<WebElement> dropdownOptions = RemindersElements.drpEventOpts(driver);
答案 1 :(得分:0)
尝试这个
public static void checkDropdownAllOption(WebDriver driver,WebElement lelement)
{
Select oSelect = new Select(lelement);
java.util.List <WebElement> elementCount = oSelect.getOptions();
int iSize = elementCount.size();
String [] arrdropdown= new String [iSize];
for (int j = 0; j < iSize; j++)
{
arrdropdown[j]=elementCount.get(j).getText();
}
WebElement mySelectElm = lelement;
Select mySelect= new Select(mySelectElm);
for (int i = 0; i < arrdropdown.length; i++)
{
mySelect.selectByVisibleText(arrdropdown[i]);
}
}