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

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

Need to Find Number of DropDown Options which are not selected

问题

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

> 网址:http://output.jsbin.com/osebed/2
>
> 任务:
>
> 1. 选择水果中的偶数选项(香蕉和橙子)
> 2. 查找未选择的选项数量(苹果和葡萄)

我尝试了并成功打印出已选择的选项(香蕉和橙子),但无法打印出未选择的选项(苹果和葡萄)。

代码:

  1. public class Ques12 {
  2. public static void main(String[] args) throws AWTException {
  3. ChromeOptions options = new ChromeOptions();
  4. options.addArguments("--remote-allow-origins=*");
  5. WebDriver driver = new ChromeDriver(options);
  6. driver.manage().window().maximize();
  7. Robot robot = new Robot();
  8. driver.get("http://output.jsbin.com/osebed/2");
  9. WebElement Fruits = driver.findElement(By.id("fruits"));
  10. Select select = new Select(Fruits);
  11. select.selectByIndex(0);
  12. select.selectByValue("orange");
  13. List<WebElement> list = select.getOptions();
  14. List<WebElement> selectedOptions = select.getAllSelectedOptions();
  15. for (WebElement i : selectedOptions) {
  16. for (WebElement j : list) {
  17. if ((j.getText()).equals(i.getText())) {
  18. System.out.println("Selected option " + j.getText());
  19. } else {
  20. System.out.println("Not selected option " + j.getText());
  21. }
  22. }
  23. }
  24. }
  25. }

输出:

  1. Selected option Banana
  2. Not selected option Apple
  3. Not selected option Orange
  4. Not selected option Grape
  5. ==========================
  6. Not selected option Banana
  7. Not selected option Apple
  8. Selected option Orange
  9. 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:

  1. public class Ques12 {
  2. public static void main(String[] args) throws AWTException {
  3. ChromeOptions options = new
  4. ChromeOptions();
  5. options.addArguments(&quot;--remote-allow-origins=*&quot;);
  6. WebDriver driver = new ChromeDriver(options);
  7. driver.manage().window().maximize();
  8. Robot robot = new Robot();
  9. driver.get(&quot;http://output.jsbin.com/osebed/2&quot;);
  10. WebElement Fruits = driver.findElement(By.id(&quot;fruits&quot;));
  11. Select select = new Select(Fruits);
  12. select.selectByIndex(0);
  13. select.selectByValue(&quot;orange&quot;);
  14. List &lt; WebElement &gt; list = select.getOptions();
  15. List &lt; WebElement &gt; selectedOptions = select.getAllSelectedOptions();
  16. for (WebElement i: selectedOptions) {
  17. for (WebElement j: list) {
  18. if ((j.getText()).equals(i.getText())) {
  19. System.out.println(&quot;Selected option &quot; + j.getText());
  20. } else {
  21. System.out.println(&quot;Not selected option &quot; + j.getText());
  22. }
  23. }
  24. }
  25. }
  26. }

Output:

  1. Selected option Banana
  2. Not selected option Apple
  3. Not selected option Orange
  4. Not selected option Grape
  5. ==========================
  6. Not selected option Banana
  7. Not selected option Apple
  8. Selected option Orange
  9. Not selected option Grape

答案1

得分: 1

The corrected logic in your code should be:

  1. ChromeOptions options = new ChromeOptions();
  2. options.addArguments("--remote-allow-origins=*");
  3. WebDriver driver = new ChromeDriver(options);
  4. driver.manage().window().maximize();
  5. Robot robot = new Robot();
  6. driver.get("http://output.jsbin.com/osebed/2");
  7. WebElement Fruits = driver.findElement(By.id("fruits"));
  8. Select select = new Select(Fruits);
  9. select.selectByIndex(0);
  10. select.selectByValue("orange");
  11. List<WebElement> list = select.getOptions();
  12. for (WebElement j : list) {
  13. if (j.isSelected()) {
  14. System.out.println("Selected option " + j.getText());
  15. } else {
  16. System.out.println("Not selected option " + j.getText());
  17. }
  18. }
  19. driver.quit();

And it should print:

  1. Selected option Banana
  2. Not selected option Apple
  3. Selected option Orange
  4. Not selected option Grape
英文:

This logic below in your code is wrong

  1. List&lt;WebElement&gt; list = select.getOptions();
  2. List&lt;WebElement&gt; selectedOptions = select.getAllSelectedOptions();
  3. for (WebElement i : selectedOptions) {
  4. for (WebElement j : list) {

Simply use isSelected()and go through complete list only once to check for isSelected and print accordingly

  1. ChromeOptions options = new ChromeOptions();
  2. options.addArguments(&quot;--remote-allow-origins=*&quot;);
  3. WebDriver driver = new ChromeDriver(options);
  4. driver.manage().window().maximize();
  5. Robot robot = new Robot();
  6. driver.get(&quot;http://output.jsbin.com/osebed/2&quot;);
  7. WebElement Fruits = driver.findElement(By.id(&quot;fruits&quot;));
  8. Select select = new Select(Fruits);
  9. select.selectByIndex(0);
  10. select.selectByValue(&quot;orange&quot;);
  11. List&lt;WebElement&gt; list = select.getOptions();
  12. for (WebElement j : list) {
  13. if (j.isSelected()) {
  14. System.out.println(&quot;Selected option &quot; + j.getText());
  15. } else {
  16. System.out.println(&quot;Not selected option &quot; + j.getText());
  17. }
  18. }
  19. driver.quit();

Should Print

  1. Selected option Banana
  2. Not selected option Apple
  3. Selected option Orange
  4. 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:

确定