<legend id='grHpo'><style id='grHpo'><dir id='grHpo'><q id='grHpo'></q></dir></style></legend>
    <bdo id='grHpo'></bdo><ul id='grHpo'></ul>
<i id='grHpo'><tr id='grHpo'><dt id='grHpo'><q id='grHpo'><span id='grHpo'><b id='grHpo'><form id='grHpo'><ins id='grHpo'></ins><ul id='grHpo'></ul><sub id='grHpo'></sub></form><legend id='grHpo'></legend><bdo id='grHpo'><pre id='grHpo'><center id='grHpo'></center></pre></bdo></b><th id='grHpo'></th></span></q></dt></tr></i><div id='grHpo'><tfoot id='grHpo'></tfoot><dl id='grHpo'><fieldset id='grHpo'></fieldset></dl></div>
    1. <tfoot id='grHpo'></tfoot>

      <small id='grHpo'></small><noframes id='grHpo'>

      1. 如何通过 Selenium WebDriver 从下拉列表中选择一个选项

        How to select an option from a dropdown through Selenium WebDriver(如何通过 Selenium WebDriver 从下拉列表中选择一个选项)
          <tbody id='vUJUC'></tbody>

          <small id='vUJUC'></small><noframes id='vUJUC'>

          <legend id='vUJUC'><style id='vUJUC'><dir id='vUJUC'><q id='vUJUC'></q></dir></style></legend>
        1. <i id='vUJUC'><tr id='vUJUC'><dt id='vUJUC'><q id='vUJUC'><span id='vUJUC'><b id='vUJUC'><form id='vUJUC'><ins id='vUJUC'></ins><ul id='vUJUC'></ul><sub id='vUJUC'></sub></form><legend id='vUJUC'></legend><bdo id='vUJUC'><pre id='vUJUC'><center id='vUJUC'></center></pre></bdo></b><th id='vUJUC'></th></span></q></dt></tr></i><div id='vUJUC'><tfoot id='vUJUC'></tfoot><dl id='vUJUC'><fieldset id='vUJUC'></fieldset></dl></div>

                  <bdo id='vUJUC'></bdo><ul id='vUJUC'></ul>
                  <tfoot id='vUJUC'></tfoot>
                • 本文介绍了如何通过 Selenium WebDriver 从下拉列表中选择一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下 Inspected element Id 用于 UI 屏幕中带有几个字段的下拉菜单.

                  I have the following Inspected element Id for a dropdown with a few fields in a UI screen.

                  下拉值:

                  • 列表项1
                  • 列表项2
                  • 列表项3

                  检查的元素 ID:

                  <select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1">
                  

                  在某些情况下,下拉列表将不包含任何值.

                  There will be cases when the drop down will hold no values.

                  我需要显示一个 sysout 日志,只有当这个下拉列表有至少一个值时.

                  I need to display a sysout log, only when this drop down has atleast one value.

                  有人可以建议我如何将其纳入我的 Selenium 测试吗?

                  Can someone please suggest how can I incorporate this in my Selenium testing?

                  目前,我有以下代码来检查服务器是否已启动并测试登录.

                  Currently, I have this following code that checks whether the server is up and tests a login.

                  package testPackage;
                  
                  import org.openqa.selenium.By;
                  import org.openqa.selenium.WebDriver;
                  import org.openqa.selenium.chrome.ChromeDriver;
                  import org.openqa.selenium.chrome.ChromeOptions;
                  
                  public class TestClass {
                  
                      public static void main(String[] args) {
                  
                          ChromeOptions options = new ChromeOptions();
                          options.addArguments("headless");
                  
                          System.setProperty("webdriver.chrome.driver", "D:\Softwares\chromedriver_win32\chromedriver.exe");
                  
                          WebDriver driver = new ChromeDriver();
                          driver.get("https://bigdata/steward.jsp");
                  
                          if (driver.getTitle().equalsIgnoreCase("Login")) {
                                  serverStatus = "UP";
                          } else {
                              serverStatus = "DOWN";
                          }
                          System.out.println("Server is " + serverStatus + ".");
                  
                          if (serverStatus.equalsIgnoreCase("UP")) {
                              driver.findElement(By.id("username")).sendKeys("username");
                              driver.findElement(By.id("password")).sendKeys("password");
                              driver.findElement(By.id("login")).click();
                  
                              String newUrl = driver.getCurrentUrl();
                  
                              if (newUrl.equalsIgnoreCase("https://bigdata/error.jsp")) {
                                  System.out.println("Incorrect username/password.");
                              } else {
                                  System.out.println("Logged in successfully.");
                              }
                          } else {
                              System.out.println("Login could not be done.");
                          }
                          driver.quit();
                      }
                  }
                  

                  推荐答案

                  根据上述案例,可以使用以下代码检查下拉列表中的元素数量并相应地打印 sysout 日志.在 wait.until 行中,根据下拉菜单中的默认选项,第二个参数数字"应替换为 0 或 1.即如果下拉列表为空,则使用 0.如果下拉列表有默认选项--Select--",则使用 1.所以基本上你要做的是,等待下拉列表中是否有任何选项,而不是默认内容.您可以根据您的应用程序加载时间更改等待时间.

                  As per the case mentioned, following code can be used to check the number of elements in the dropdown and print sysout log accordingly. In the wait.until line, the 2nd argument 'number' should be replaced with either 0 or 1 according to the default options present in the dropdown. i.e If dropdown is empty use 0. If the dropdown has a default option as '--Select--', then use 1. So basically what you will be doing is, waiting if there is any option in the dropdown, more than the default content. You can change the wait time according to your application load time.

                  try {new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"), number));
                              System.out.println("Drop down has at least one value present");
                          } catch (Exception e) {
                              System.out.println("No options in the drop down");
                          }
                  

                  另一种方式可能是

                  List<WebElement> list_Items=driver.findElements(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"));
                          if(list_Items.size()>1){
                              System.out.println("Drop down has at least one value present");
                          }
                          else{
                              System.out.println("No options in the drop down");
                          }
                  

                  这篇关于如何通过 Selenium WebDriver 从下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)
                    <bdo id='s8pyo'></bdo><ul id='s8pyo'></ul>

                    • <legend id='s8pyo'><style id='s8pyo'><dir id='s8pyo'><q id='s8pyo'></q></dir></style></legend>

                        <small id='s8pyo'></small><noframes id='s8pyo'>

                            <tbody id='s8pyo'></tbody>
                          1. <i id='s8pyo'><tr id='s8pyo'><dt id='s8pyo'><q id='s8pyo'><span id='s8pyo'><b id='s8pyo'><form id='s8pyo'><ins id='s8pyo'></ins><ul id='s8pyo'></ul><sub id='s8pyo'></sub></form><legend id='s8pyo'></legend><bdo id='s8pyo'><pre id='s8pyo'><center id='s8pyo'></center></pre></bdo></b><th id='s8pyo'></th></span></q></dt></tr></i><div id='s8pyo'><tfoot id='s8pyo'></tfoot><dl id='s8pyo'><fieldset id='s8pyo'></fieldset></dl></div>
                          2. <tfoot id='s8pyo'></tfoot>