WebDriverAPI(8)

  判断页面元素是否存在

  测试网址

  http://www.baidu.com

  Java语言版本API实例 

  @Test
  public void testIsElementPresent(){
    driver.manage().window().maximize();
    driver.get(url);
    if(IsElementPresent(By.id("kw"))){
    WebElement searchInputBox = driver.findElement(By.id("kw"));
      if(searchInputBox.isEnabled() == true){
        searchInputBox.sendKeys("百度首页搜索框成功找到");
       }
    }
    else{
      Assert.fail("未找到页面元素");
      }
    }
    private boolean IsElementPresent(By by) {
    try {
      //判断传入的参数是否找到页面元素,找到则返回true
      driver.findElement(by);
      return true;
      } catch (NoSuchElementException e) {
        return false;
    }
  }

猜你喜欢

转载自www.cnblogs.com/z-zzz/p/10475164.html
8