需要找到未选择的下拉选项数量。

huangapple go评论72阅读模式
英文:

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(&quot;--remote-allow-origins=*&quot;);
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        Robot robot = new Robot();
        driver.get(&quot;http://output.jsbin.com/osebed/2&quot;);
        WebElement Fruits = driver.findElement(By.id(&quot;fruits&quot;));
        Select select = new Select(Fruits);
        select.selectByIndex(0);
        select.selectByValue(&quot;orange&quot;);

        List &lt; WebElement &gt; list = select.getOptions();
        List &lt; WebElement &gt; selectedOptions = select.getAllSelectedOptions();

        for (WebElement i: selectedOptions) {
            for (WebElement j: list) {

                if ((j.getText()).equals(i.getText())) {
                    System.out.println(&quot;Selected option &quot; + j.getText());
                } else {
                    System.out.println(&quot;Not selected option &quot; + 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&lt;WebElement&gt; list = select.getOptions();
		List&lt;WebElement&gt; 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(&quot;--remote-allow-origins=*&quot;);
		WebDriver driver = new ChromeDriver(options);
		driver.manage().window().maximize();
		Robot robot = new Robot();
		driver.get(&quot;http://output.jsbin.com/osebed/2&quot;);
		WebElement Fruits = driver.findElement(By.id(&quot;fruits&quot;));
		Select select = new Select(Fruits);
		select.selectByIndex(0);
		select.selectByValue(&quot;orange&quot;);

		List&lt;WebElement&gt; list = select.getOptions();
		for (WebElement j : list) {

			if (j.isSelected()) {
				System.out.println(&quot;Selected option &quot; + j.getText());
			} else {
				System.out.println(&quot;Not selected option &quot; + j.getText());
			}
		}
		driver.quit();

Should Print

Selected option Banana
Not selected option Apple
Selected option Orange
Not selected option Grape

huangapple
  • 本文由 发表于 2023年5月7日 18:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定