標籤:ceo getc add fileutils 賦值 let cut cookies document
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 // 設定隱形等待時間10s2 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)標題是否包含text16 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.copyFile4 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‘);");
Java+selenium之WebDriver的cookie,等待等進階操作(五)