zoukankan      html  css  js  c++  java
  • Selenium:元素等待的4种方法

    1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用。

    2.隐示等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。
    WebDriver dr = new FirefoxDriver();           
    dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    3.使用javascript 
    WebElement element = driver.findElement(By.xpath(test));
    ((JavascriptExecutor)driver).executeScript("arguments[0].style.border="5px solid yellow"",element);  
    4.显示等待,推荐使用显示等待

    WebDriverWait wait = new WebDriverWait(dr, 10);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("kw")));

    显式等待 使用ExpectedConditions类中自带方法, 可以进行显试等待的判断。 

    显式等待可以自定义等待的条件,用于更加复杂的页面等待条件

    (1)页面元素是否在页面上可用和可被单击:elementToBeClickable(By locator)

    (2)页面元素处于被选中状态:elementToBeSelected(WebElement element)

    (3)页面元素在页面中存在:presenceOfElementLocated(By locator)

    (4)在页面元素中是否包含特定的文本:textToBePresentInElement(By locator)

    (5)页面元素值:textToBePresentInElementValue(By locator, java.lang.String text)

    (6)标题 (title):titleContains(java.lang.String title)

    只有满足显式等待的条件满足,测试代码才会继续向后执行后续的测试逻辑

    如果超过设定的最大显式等待时间阈值, 这测试程序会抛出异常。 

    WebDriverWait wait = new WebDriverWait(driver,5); 

    wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(""))); 

  • 相关阅读:
    Appdelegate 跳转其他页面 获取当前屏幕显示的viewcontroller 获取当前屏幕中present出来的viewcontroller 获取当前导航控制器
    React-Native 环境部署
    关于GCD的那些事
    二,Runtime进行动态添加方法
    一, Runtime 交换方法
    Runtime 概念
    Mac Office安装及破解
    iOS 规范之宏
    规范之UITableViewCell
    Linux 命令
  • 原文地址:https://www.cnblogs.com/fenqing3108/p/6237814.html
Copyright © 2011-2022 走看看