<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 从下拉列表中选择一个选项

        时间:2023-06-27
          <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 从下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 Selenium 在 Chrome 中启用弹出窗口 下一篇:ChromeDriver(Capabilities 功能)已弃用

                  相关文章

                    <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'>

                    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>