<tfoot id='yOpMx'></tfoot>

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

        <i id='yOpMx'><tr id='yOpMx'><dt id='yOpMx'><q id='yOpMx'><span id='yOpMx'><b id='yOpMx'><form id='yOpMx'><ins id='yOpMx'></ins><ul id='yOpMx'></ul><sub id='yOpMx'></sub></form><legend id='yOpMx'></legend><bdo id='yOpMx'><pre id='yOpMx'><center id='yOpMx'></center></pre></bdo></b><th id='yOpMx'></th></span></q></dt></tr></i><div id='yOpMx'><tfoot id='yOpMx'></tfoot><dl id='yOpMx'><fieldset id='yOpMx'></fieldset></dl></div>
      1. <legend id='yOpMx'><style id='yOpMx'><dir id='yOpMx'><q id='yOpMx'></q></dir></style></legend>
        • <bdo id='yOpMx'></bdo><ul id='yOpMx'></ul>
      2. 如何通过 Selenium WebDriver 从下拉列表中选择一个选

        时间:2023-09-28
          <bdo id='y3dvy'></bdo><ul id='y3dvy'></ul>
          • <small id='y3dvy'></small><noframes id='y3dvy'>

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

                  本文介绍了如何通过 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 从下拉列表中选择一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                  <tfoot id='25P6c'></tfoot>
                  <legend id='25P6c'><style id='25P6c'><dir id='25P6c'><q id='25P6c'></q></dir></style></legend>

                    <bdo id='25P6c'></bdo><ul id='25P6c'></ul>

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