【问题标题】:Validating autopopulated textbox using selenium web driver, java使用 selenium webdriver,java 验证自动填充文本框
【发布时间】:2013-01-12 15:32:42
【问题描述】:

我有一个场景,其中有一个文本框,当我在该文本框中键入内容时,它会自动填充。 我需要跟踪自动填充的值并验证它是否包含我在文本字段中输入的字符串。 有人可以帮我解决这个问题吗? 提前致谢

【问题讨论】:

    标签: selenium web driver


    【解决方案1】:

    试试这样的

    以下代码用于获取谷歌网站的自动填充结果。

    driver.get("http://www.google.co.in");
    driver.findElement(By.id("lst-ib")).sendKeys("Test");
    List<WebElement> autoPopulatedList=driver.findElements(By.cssSelector("tr>td>span"));
    for(WebElement ele:autoPopulatedList)
    {
          System.out.println(ele.getText());
          if(ele.getText().contains("Test"))
          {
                 System.out.println("Your case passed..!!");
          }
    }
    

    【讨论】:

      【解决方案2】:
      driver.get(URL); // URL = http://www.google.co.in;
      
      driver.findElement(By.id(id_of_the_element)).sendKeys(value); //Value for the field
      
      List autoPopulatedList=driver.findElements(auto_populate_element_path);
      
      int autoPopulateSize = autoPopulatedList.length(); // Take the auto populate size to compare
      
      int testedSize = 0; //initialize a variable for testing
      
      for(WebElement element : autoPopulatedList){
      
           if(element.getText().contains(value)) //Checking the autopopulated list with the value
      
           {
      
                 testedSize++;  // this will add 1 if the autopopulate contains the value
      
           }
      
      }
      
      if(autoPopulateSize == testedSize){
      
           System.out.println("Autopopulate contains the values");
      }else{
      
       System.out.println("Fail");
      }
      

      【讨论】:

        【解决方案3】:

        只需发送文本抛出 Sendkey ,然后像这样获取字段值:

            driver.findElement(By.id("field ID here")).sendkeys("ABC");
            String strActualValue = driver.findElement(By.id("field ID here")).getAttribute("value");
            System.out.println("Field value is = "+strActualValue +"");
        

        它将打印您通过发送键发送的值以及字段中自动填充的值。

            Result : ABC1234
            ABC is your value 1234 is auto-populated value.
        

        享受吧!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-15
          • 2017-01-29
          • 1970-01-01
          • 2021-07-01
          • 2016-04-09
          • 2013-03-20
          • 1970-01-01
          相关资源
          最近更新 更多