zoukankan      html  css  js  c++  java
  • Java+selenium之WebDriver的cookie,等待等高级操作(五)

    1. 操作cookie

    1 // 增加一个 name = "name",value="value" 的 cookie   
    2 Cookie cookie = new Cookie("name", "value");   
    3 driver.manage().addCookie(cookie);        
    4 // 得到当前页面下所有的 cookies ,并且输出它们的所在域、name、value、有效日期和路径   
    5 Set<Cookie> cookies = driver.manage().getCookies();   
    6 System.out.println(String.format("Domain -> name -> value -> expiry -> path"));   
    7 for(Cookie c : cookies)   
    8     System.out.println(String.format("%s -> %s -> %s -> %s -> %s",c.getDomain(), c.getName(), c.getValue(),c.getExpiry(),c.getPath())); 
    9 }

      删除 cookie

    1 // 第一种通过 cookie 的 name   
    2 driver.manage().deleteCookieNamed("CookieName");   
    3 // 第二种通过 Cookie 对象(Cookie cookie)    
    4 driver.manage().deleteCookie(cookie); 
    5 // 第三种全部删除   
    6 driver.manage().deleteAllCookies(); 

    2. 等待页面元素加载完成

     1 // (1) 强制等待弊端较多,会造成时间的浪费或者休眠时间不够
     2 Thread.sleep(2000);
     3 
     4 // (2) 加入循环等待优化
     5 long start = System.currentTimeMillis(); 
     6 while (true) {
     7     Thread.sleep(500); 
     8        if (driver.getTitle().indexOf("期望值") != -1)
     9            break; 
    10    if (System.currentTimeMillis() - start >= 10000)
    11            break;
    12 } 

      隐形等待,设置全局元素等待超时时间。隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver 查询Dom一定时间。默认值是0, 但是设置之后,这个时间将在WebDriver 对象实例整个生命周期都起作用。但这里有一个弊端,那就是程序会在设定的时间内一直等待整个页面加载完成,才会执行下一步,有时候个别JS加载比较慢会比较浪费时间。

    1 // 设置隐形等待时间10s
    2 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

      显性等待,使用ExpectedConditions类中自带方法,可以进行显试等待的判断。只有满足显式等待的条件满足,代码才会继续向后执行,如果超过设定的最大显式等待时间, 这程序会抛出异常。如果同时设置隐性等待和显性等待,取最大等待时间。

     1 // 设置显性等待时间,最大等待10秒
     2 WebDriverWait wait = new WebDriverWait(driver, 10);
     3 //(1)页面元素是否在页面上可用和可被单击
     4 WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By locator));
     5 //(2)页面元素处于被选中状态
     6 Boolean boolean = wait.until(ExpectedConditions.elementToBeSelected(WebElement element));
     7 //(3)页面元素在页面中存在
     8 WebElement we = wait.until(ExpectedConditions.presenceOfElementLocated(By locator));
     9 //(4)在页面元素中是否包含特定的文本
    10 Boolean boolean = wait.until(ExpectedConditions.textToBePresentInElement(WebElement element, String text));
    11 Boolean boolean = wait.until(ExpectedConditions.textToBePresentInElement(By locator, String text));//弃用
    12 //(5)页面元素值是否出现
    13 Boolean boolean = wait.until(ExpectedConditions.textToBePresentInElementValue(By locator, String text));
    14 Boolean boolean = wait.until(ExpectedConditions.textToBePresentInElementValue(WebElement element, String text));
    15 //(6)标题是否包含text
    16 Boolean boolean = wait.until(ExpectedConditions.titleContains(String title));
    17 //(7)元素可见
    18 WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(By locator));
    19 //(8)元素不可见消失
    20 Boolean boolean = wait.until(ExpectedConditions.invisibilityOfElementLocated(By locator));

    3. 页面截图

    1 // 得到截图并保存在C盘下 截取页面全图,不管页面多长 
    2 File screenShotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    3 // org.apache.commons.io.FileUtils.copyFile
    4 FileUtils.copyFile(screenShotFile, new File("C:/test.png")); 

    4. 执行JS脚本

    1 // 创建JavascriptExecutor对象
    2 JavascriptExecutor js = (JavascriptExecutor) driver; 
    3 // 设置日期控件的读写属性 
    4 js.executeScript("document.getElementById("fromDate").readOnly=false");  
    5 // 直接为日期控件强行赋值 
    6 js.executeScript("document.getElementById('id').setAttribute('value','2018-05-10');");
    兴趣是最好的老师,知识改变格局,转载请注明出处!
  • 相关阅读:
    Palindrome Linked List 解答
    Word Break II 解答
    Array vs Linked List
    Reverse Linked List II 解答
    Calculate Number Of Islands And Lakes 解答
    Sqrt(x) 解答
    Find Median from Data Stream 解答
    Majority Element II 解答
    Binary Search Tree DFS Template
    188. Best Time to Buy and Sell Stock IV
  • 原文地址:https://www.cnblogs.com/andrew209/p/9021879.html
Copyright © 2011-2022 走看看