Selenium学习9--显示等待,判断页面元素是否存在

html代码如下:

<html lang="en">
 <head>
 <title>your favorite fruits</title>
 </head>
 <body>
    <p>select ur perfer fruit</p>
    <br>
    <select name='fruit'>
        <option id='peach' value='taozi'>peach</option>
        <option id='watermelon' value='xigua'>watermelon</option>
    </select>
    <br>
    <input type='checkbox'>Do you like fruits?</input>
    <br><br>
    <input type=text" id="text" value="Watermelons are so sweet this year!">This is Input box</input>
 </body>
</html>
@Test
 public void testExplicitWait(){
      driver.get("E:\\Java\\selenium_data\\html\\obviouswait.html");
      //声明一个WebDriverWait对象,设定复发条件的最长时间待定10s
      WebDriverWait wait = new WebDriverWait(driver,10);
      //调用ExpectedConditions的titleContains方法判断页面title属性是否包含"fruits"字段
      wait.until(ExpectedConditions.titleContains("fruits"));

      WebElement select = driver.findElement(By.xpath("//option[@id='peach']"));
      //elementToBeSelected(Element element)判断"peach"是否被选中
      wait.until(ExpectedConditions.elementToBeSelected(select));

      //elementToBeClickable(String )判断复选框对象是否可以被点击
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p")));

      //presenceOfElementLocated 判断元素是否存在
      wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//x")));

      //textToBePresentInElement判断字样是否在元素中显示
      WebElement p = driver.findElement(By.xpath("//p"));
      wait.until(ExpectedConditions.textToBePresentInElement(p, "select ur perfer fruit"));
  }

自定义显示等待

 @Test
  public void testCustomExplicitWait(){
      driver.get("E:\\Java\\selenium_data\\html\\obviouswait.html");
      try{
          //显示等待判断是否可以从页面获取文字输入框对象, 如果可以获取, 则执行后续测试逻辑
          WebElement textInputBox = (new WebDriverWait(driver,10).until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver driver) {
                return driver.findElement(By.xpath("//*[@type='text']"));
            }
        }));
          Assert.assertEquals("Watermelons are so sweet this year!", textInputBox.getAttribute("value"));

          Boolean containTextFlag = (new WebDriverWait(driver,10).until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driver) {
                return driver.findElement(By.xpath("//p")).getText().equals("perfer fruit");
            }
        }));
          Assert.assertTrue(containTextFlag);



      }catch(Exception e){
          e.printStackTrace();
      }
  }

判断页面元素是否存在

  private boolean isElementPresent(By by){
      try{
          driver.findElement(by);
          return true;
      }catch(Exception e){
          return false;
      }

  }

  @Test
  public void testIsElementPresent(){
      driver.get("http://www.sogou.com");
      if(isElementPresent(By.id("query"))){
          WebElement searchInputBox = driver.findElement(By.id("query"));
          if(searchInputBox.isEnabled()){
              searchInputBox.sendKeys("元始金章");
          }
      }else{
          Assert.fail("元素没有找到,zz");
      }
  }
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值