英文:
Need to Find Number of DropDown Options which are not selected
问题
需要找出未选择的下拉选项的数量。
> 网址:http://output.jsbin.com/osebed/2
>
> 任务:
>
> 1. 选择水果中的偶数选项(香蕉和橙子)
> 2. 查找未选择的选项数量(苹果和葡萄)
我尝试了并成功打印出已选择的选项(香蕉和橙子),但无法打印出未选择的选项(苹果和葡萄)。
代码:
public class Ques12 {
public static void main(String[] args) throws AWTException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
Robot robot = new Robot();
driver.get("http://output.jsbin.com/osebed/2");
WebElement Fruits = driver.findElement(By.id("fruits"));
Select select = new Select(Fruits);
select.selectByIndex(0);
select.selectByValue("orange");
List<WebElement> list = select.getOptions();
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for (WebElement i : selectedOptions) {
for (WebElement j : list) {
if ((j.getText()).equals(i.getText())) {
System.out.println("Selected option " + j.getText());
} else {
System.out.println("Not selected option " + j.getText());
}
}
}
}
}
输出:
Selected option Banana
Not selected option Apple
Not selected option Orange
Not selected option Grape
==========================
Not selected option Banana
Not selected option Apple
Selected option Orange
Not selected option Grape
英文:
Need to Find Number of DropDown Options which are not selected.
> URL : http://output.jsbin.com/osebed/2
>
> TASK:
>
> 1. Select Even Option available in fruits (Banana and Orange)
> 2. Find out number of option not selected (Apple and Grape)
I tried it and could able to print Selected options (Banana and Orange) but cannot able to print Not Selected options (Apple and Grape)
Code:
public class Ques12 {
public static void main(String[] args) throws AWTException {
ChromeOptions options = new
ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
Robot robot = new Robot();
driver.get("http://output.jsbin.com/osebed/2");
WebElement Fruits = driver.findElement(By.id("fruits"));
Select select = new Select(Fruits);
select.selectByIndex(0);
select.selectByValue("orange");
List < WebElement > list = select.getOptions();
List < WebElement > selectedOptions = select.getAllSelectedOptions();
for (WebElement i: selectedOptions) {
for (WebElement j: list) {
if ((j.getText()).equals(i.getText())) {
System.out.println("Selected option " + j.getText());
} else {
System.out.println("Not selected option " + j.getText());
}
}
}
}
}
Output:
Selected option Banana
Not selected option Apple
Not selected option Orange
Not selected option Grape
==========================
Not selected option Banana
Not selected option Apple
Selected option Orange
Not selected option Grape
答案1
得分: 1
The corrected logic in your code should be:
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
Robot robot = new Robot();
driver.get("http://output.jsbin.com/osebed/2");
WebElement Fruits = driver.findElement(By.id("fruits"));
Select select = new Select(Fruits);
select.selectByIndex(0);
select.selectByValue("orange");
List<WebElement> list = select.getOptions();
for (WebElement j : list) {
if (j.isSelected()) {
System.out.println("Selected option " + j.getText());
} else {
System.out.println("Not selected option " + j.getText());
}
}
driver.quit();
And it should print:
Selected option Banana
Not selected option Apple
Selected option Orange
Not selected option Grape
英文:
This logic below in your code is wrong
List<WebElement> list = select.getOptions();
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for (WebElement i : selectedOptions) {
for (WebElement j : list) {
Simply use isSelected()and go through complete list only once to check for isSelected and print accordingly
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
Robot robot = new Robot();
driver.get("http://output.jsbin.com/osebed/2");
WebElement Fruits = driver.findElement(By.id("fruits"));
Select select = new Select(Fruits);
select.selectByIndex(0);
select.selectByValue("orange");
List<WebElement> list = select.getOptions();
for (WebElement j : list) {
if (j.isSelected()) {
System.out.println("Selected option " + j.getText());
} else {
System.out.println("Not selected option " + j.getText());
}
}
driver.quit();
Should Print
Selected option Banana
Not selected option Apple
Selected option Orange
Not selected option Grape
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论