整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          python爬蟲Selenium庫詳細教程

          我們爬取網頁過程中,經常發現我們想要獲得的數據并不能簡單的通過解析HTML代碼獲取

          1. 使用示例

          2. 詳細介紹

          2.1 聲明瀏覽器對象

          2.2 訪問頁面

          2.3 查找元素

          2.3.1 單個元素

          下面是詳細的元素查找方法

          第二種:

          2.3.2 多個元素

          2.4 元素交互操作

          比如說在搜索框內輸入文字:

          2.5 交互動作

          2.6 執行JavaScript

          比如拖拽下拉

          2.7 獲取元素信息

          2.7.1 獲取屬性

          2.8 Frame

          2.9 等待

          2.9.1 隱式等待

          需要特別說明的是:

          2.9.2 顯式等待

          2.10 瀏覽器的前進/后退

          2.11 對Cookies進行操作

          2.12 選項卡管理

          就可以使用selenium來實現。

          在H5開發中,經常會開發搜索功能,商品列表、訂單列表、客戶列表等等,都需要搜索,所以程序猿(程序媛)們都會遇到這樣的需求,點擊搜索input時,彈出的鍵盤,有“搜索”按鈕,點擊搜索調用接口搜索。今天就來講講怎么搞定這個需求。

          H5中input輸入框如何實現原生鍵盤搜索功能

          html代碼

          <form action="javascript:;" id="searchFrom" onsubmit="searchList">
           <input type="search" value="" placeholder="搜索Javan的博客" />
          </form>
          

          js代碼

          元素綁定方法調用

          function searchList(){
           // do something
          }
          

          jquery監聽

          $('#searchFrom').bind('submit', function () {
           // do something
          });
          

          H5中input輸入框如何實現原生鍵盤搜索功能

          注意事項

          1. action="javascript:;"這里的作用是,防止頁面刷新,如果不寫,頁面會刷新
          2. type="search""input的類型需要是search
          3. input輸入框必須放到form表單中
          4. 這樣寫input框輸入值后,會有清除按鈕,需要改變樣式,或者去除,請看下方代碼
          input[type=search]::-webkit-search-cancel-button{
           -webkit-appearance: none; // 此處只是去掉默認的小×
          }
          

          自定義樣式

          input[type=search]::-webkit-search-cancel-button{
           -webkit-appearance: none;
           position: relative;
           height: 20px;
           width: 20px;
           border-radius: 50%;
           background-color: #EBEBEB;
          }
          input[type=search]::-webkit-search-cancel-button:after{
           position: absolute;
           content: 'x';
           left: 25%;
           top: -12%;
           font-size: 20px;
           color: #fff;
          }
          
          

          公告

          喜歡小編的點擊關注,了解更多資源!

          ebDirver自動化測試框架詳解

          一、 簡介

          1. WebDriver API相對于Selenium Remote Control API來說,雖然同樣是控制瀏覽器,但它的編程接口更加簡潔
          2. WebDriver可以應對那些網頁本身不重新加載的動態網頁。
          3. Selenium Remote Control是采用向瀏覽器注入javascript腳本來控制瀏覽器的,但WebDriver與之不同,它是直接使用瀏覽器內置的自動化支持來控制瀏覽器的。
          4. WebDriver實際上就像它的名字一樣,向上屏蔽各廠商瀏覽器的差異,提供了一個統一的編程API,方便廣大程序員控制瀏覽器的行為。

          二、 webDirver 的 dirver

          1. 即然要屏蔽各廠商瀏覽器的差異,那么各廠商自然需要根據WebDriver規范作出各自的實現。WebDriver官方文檔就列出各實現:HtmlUnit Driver、Firefox Driver、InternetExplorerDriver、ChromeDriver、Opera Driver、iOS Driver、Android Driver。這些Driver各有優缺點及各自適用的場景,具體可看官方文檔說明。其實一看這些名字就知道是什么意思,要控制哪種瀏覽器就需要下載安裝對應的Driver。比如我這里是Mac OSX系統,而且想控制該系統上的Chrome瀏覽器,那么就下載chromedriver_mac32.zip(注意該Driver對你的Chrome瀏覽器有版本要求,要求版本必須是v46-50這個范圍),將該壓縮包里的可執行文件放到PATH環境變量目錄中,比如放到/usr/local/bin目錄中
          2. 各個dirver 的官方文檔說明 https://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-s-drivers

          三、 webDirver 使用前準備

          1. 官方還很貼心地為WebDriver提供了更主流語言的SDK。支持的語言有Java、C#、Python、Ruby、Perl、PHP、JavaScript。但我感覺這種測試相關的編程語言最好還是用腳本語言合適一點,改起來很方便,不需要時時編譯。
          2. Js
          //前提是先安裝好NodeJS
          mkdir test && cd test
          npm init //這里根據提示一步步初始化一個新的NodeJS項目
          npm install selenium-webdriver --save //安裝WebDriver JavaScript SDK的npm依賴
          
          1. Java maven 依賴:
          <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
          <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>3.141.0</version>
          </dependency>
          

          四、 webDirver 使用

          1. 控制瀏覽器
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.chrome.ChromeDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
          import org.openqa.selenium.ie.InternetExplorerDriver;
          /**
           * webDirver 打開各種瀏覽器
           * @author outman
           * @date 2018-11-07
           * */
          public class Demo_02 {
           public static void main(String[] args) {
          // openFirFox();
          // openIE();
           openChrome();
           }
           /**
           * 打開谷歌瀏覽器
           * */
           private static void openChrome() {
           // chromedriver.exe 下載地址 : http://chromedriver.storage.googleapis.com/index.html
           // 注意: 下載驅動時驅動版本要與selenium 版本一致
           System.setProperty("webdriver.chrome.driver","D:\\DevelopSoftware\\chromedriver_win32\\chromedriver.exe");
           WebDriver dirver = new ChromeDriver();
           // 在執行操作是可能會報錯: Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: Chrome version must be >= 68.0.3440.0 
           // 解決: 使chromedriver 版本與chrome 版本相對應 參考chromedriver與chrome 版本映射關系 https://blog.csdn.net/huilan_same/article/details/51896672
           dirver.get("http://www.baidu.com");
           }
           /**
           * 打開IE瀏覽器
           * */ 
           private static void openIE() {
           // IEDriverServer.exe 下載地址 https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
           // 注意: 下載驅動時驅動版本要與selenium 版本一致
           // 可能報錯:Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
           // 解決: 設置internet選項 -- 安全 -- 把所有的啟動保護模式都關閉
           System.setProperty("webdriver.ie.driver", "D:\\DevelopSoftware\\IEDriverServer_x64_3.141.0\\IEDriverServer.exe");
           WebDriver dirver = new InternetExplorerDriver();
           dirver.get("http://www.baidu.com");
           }
           /**
           * 打開火狐瀏覽器
           * @throws InterruptedException 
           * */
           private static void openFirFox() throws InterruptedException {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           //讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           dirver.get("http://www.baidu.com");
           }
          }
          
          1. 定位dom元素
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 定位dom元素
           * 
           * @author outman
           * @date 2018-11-07
           */
          public class Demo_03 {
           public static void main(String[] args) {
           // 獲取驅動并打開瀏覽器
           WebDriver dirver = openFirFox();
           // 定位dom 元素
           findElement(dirver);
           }
           /**
           * 定位dom 元素
           */
           private static void findElement(WebDriver dirver) {
           // 打開百度搜索頁
           dirver.get("http://www.baidu.com");
           // 根據ID 定位元素 搜索框
           WebElement kw = dirver.findElement(By.id("kw"));
           System.out.println("搜索框使用標簽:" + kw.getTagName());
           // 根據class 類名定位 搜索框
           WebElement kw2 = dirver.findElement(By.className("s_ipt"));
           System.out.println("搜索框使用標簽:" + kw2.getTagName());
           // 根據tagName定位 搜索框
           WebElement kw3 = dirver.findElement(By.tagName("input"));
           System.out.println("搜索框使用標簽:" + kw3.getTagName());
           // 根據name定位 搜索框
           WebElement kw4 = dirver.findElement(By.name("wd"));
           System.out.println("搜索框使用標簽:" + kw4.getTagName());
           // 根據鏈接文字定位 頁腳鏈接
           WebElement kw5 = dirver.findElement(By.linkText("把百度設為主頁"));
           System.out.println("鏈接使用標簽:" + kw5.getTagName());
           // 根據鏈接部分文字定位 頁腳鏈接
           WebElement kw6 = dirver.findElement(By.partialLinkText("百度"));
           System.out.println("鏈接使用標簽:" + kw6.getTagName());
           // 根據css選擇器定位 搜索框 css選擇器參考: http://www.w3school.com.cn/cssref/css_selectors.asp
           WebElement kw7 = dirver.findElement(By.cssSelector("#kw"));
           System.out.println("搜索框使用標簽:" + kw7.getTagName());
           // 根據xpath定位 搜索框 xpath語法參考: http://www.w3school.com.cn/xpath/xpath_syntax.asp
           WebElement kw8 = dirver.findElement(By.xpath("http://input"));
           System.out.println("搜索框使用標簽:" + kw8.getTagName());
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 這么多種定位UI元素的辦法,總有一款可以適應你的需求。我個人比較喜歡使用css selector來定位元素。要得到一個元素的css selector也很簡單,只需要使用Chrome的開發者工具查看這個元素,然后在這個元素上右鍵,點擊Copy selector就得到了(當然如有可能最好對得到的css selector簡寫一下)。
          2. 操作dom元素
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 操作dom
           * 
           * @author outman
           * @date 2018-11-07
           */
          public class Demo_04 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExcult(dirver);
           }
           /**
           * 執行操作
           */
           private static void doExcult(WebDriver dirver) {
           // 打開百度
           dirver.get("http://www.baidu.com");
           // 獲取元素 百度一下按鈕
           WebElement sbtn = dirver.findElement(By.cssSelector("#su"));
           // 獲取屬性的值
           System.out.println("按鈕文字:"+sbtn.getAttribute("value"));
           // 獲取元素 頁腳鏈接
           WebElement linkOne = dirver.findElement(By.cssSelector("#setf"));
           // 獲取文本內容
           System.out.println("鏈接文本:"+linkOne.getText());
           //連續獲取元素
           WebElement soutuBtn = dirver.findElement(By.className("fm"));
           WebElement sInput = soutuBtn.findElement(By.tagName("span")).findElement(By.name("wd"));
           System.out.println("搜索框使用標簽:"+sInput.getTagName());
           //向input輸入文字
           sInput.sendKeys("百度一下");
           System.out.println("搜索框內容:"+sInput.getAttribute("value"));
           //清空input 元素內容
           sInput.clear();
           System.out.println("搜索框內容:"+sInput.getAttribute("value"));
           //點擊按鈕
           sInput.sendKeys("百度一下");
           sbtn.click();
           //提交表單
           sInput.clear();
           sInput.sendKeys("百度兩下");
           sbtn.submit();
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 操作窗口
          import org.openqa.selenium.Dimension;
          import org.openqa.selenium.JavascriptExecutor;
          import org.openqa.selenium.Point;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 操作窗口
           * 
           * @author outman
           * @date 2018-11-07
           */
          public class Demo_05 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExcult(dirver);
           }
           /**
           * 執行操作
           */
           private static void doExcult(WebDriver dirver) {
           // 創建js 執行對象
           JavascriptExecutor jsExceut = (JavascriptExecutor) dirver;
           // 導航到百度
           dirver.get("http://www.baidu.com");
           // 創建多個窗口
           String js = "window.open(\"https://www.sogou.com\");";
           String js2 = "window.open(\"http://www.w3school.com.cn/xpath/xpath_syntax.asp\");";
           // 當只傳入js腳本時 , 默認執行該js 的對象時瀏覽器
           jsExceut.executeScript(js);
           jsExceut.executeScript(js2);
           // 設置窗口位置
           dirver.manage().window().setPosition(new Point(100, 100));
           // 設置窗口大小
           dirver.manage().window().setSize(new Dimension(500 , 200));
           // 最大化窗口
           dirver.manage().window().maximize();
           // 全屏
           dirver.manage().window().fullscreen();
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 在窗口或frame 之間切換
          import java.util.Set;
          import org.openqa.selenium.JavascriptExecutor;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 在窗口或Frame 之間切換
           * 
           * @author outman
           * @date 2018-11-07
           */
          public class Demo_06 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExecute(dirver);
           }
           /**
           * 執行操作
           * */
           private static void doExecute(WebDriver dirver) {
           JavascriptExecutor jsExecute = (JavascriptExecutor) dirver;
           String openWindowPre = "window.open('";
           String openWindowSuf = "');";
           String url_1 = "https://www.sogou.com";
           String url_2 = "http://田杰.wang";
           // 打開多個窗口
           dirver.get("http://www.baidu.com");
          // jsExecute.executeScript(openWindowPre+url_1+openWindowSuf);
          // jsExecute.executeScript(openWindowPre+url_2+openWindowSuf);
           // 獲取當前窗口的handle
           String windowHandle = dirver.getWindowHandle();
           System.out.println("當前瀏覽器handle:"+windowHandle);
           // 列出瀏覽器所有窗口的handle
           Set<String> windowHandles = dirver.getWindowHandles();
           System.out.println("所有窗口的handle:"+windowHandles);
           // 獲取當前頁面標題 
           String title = dirver.getTitle();
           System.out.println("當前頁面標題:"+title);
           // 獲取當前頁面源碼
           String pageSource = dirver.getPageSource();
           //System.out.println("當前頁面源碼:"+pageSource);
           // 獲取當前頁面url
           String currentUrl = dirver.getCurrentUrl();
           System.out.println("當前頁面url:"+currentUrl);
           // 聚焦到頂部窗口 或 頂部frame
           WebDriver defaultContent = dirver.switchTo().defaultContent();
           // 返回到當前聚焦的元素 如果沒有聚焦的元素 則返回body
           WebElement activeElement = dirver.switchTo().activeElement();
           // 切換單當前活躍的提示窗 
          // Alert alert = dirver.switchTo().alert(); //沒有找到alert會拋異常
           // 切換到指定窗口
           WebDriver window = dirver.switchTo().window(windowHandle);// 傳入 name 或者handle
           // 切換frame
           WebDriver frame = dirver.switchTo().frame("");
           // 切換到父frame
           WebDriver parentFrame = dirver.switchTo().parentFrame();
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 操作alert
          點擊Alert窗口中的OK:
          driver.switchTo().alert().accept();
          點擊Alert窗口中的Cancel:
          driver.switchTo().alert().dismiss();
          向Alert窗口輸入文字:
          driver.switchTo().alert().sendKeys(‘abcd’);
          
          1. 操作瀏覽器的導航及地址欄
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 操作瀏覽器的導航及地址欄
           * 
           * @author outman
           * @date 2018-11-07
           */
          public class Demo_07 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExecute(dirver);
           }
           /**
           * 執行操作
           * */
           private static void doExecute(WebDriver dirver) {
           //導航到百度
           dirver.get("http://www.baidu.com");
           //導航到搜狗
           dirver.navigate().to("https://www.sogou.com");
           //導航后退
           dirver.navigate().back();
           //導航前進
           dirver.navigate().forward();
           //導航刷新
           dirver.navigate().refresh();
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 操作cookie
          import java.util.Set;
          import org.openqa.selenium.Cookie;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
          /**
           * 操作cookie
           */
          public class Demo_08 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExecute(dirver);
           }
           /**
           * 執行操作
           */
           private static void doExecute(WebDriver dirver) {
           dirver.get("http://www.baidu.com");
           // 添加cookie
           Cookie addCookie = new Cookie("outman", "1317361873dfdfv");
           dirver.manage().addCookie(addCookie);
           // 得到所有的cookie
           Set<Cookie> cookies = dirver.manage().getCookies();
           for(Cookie cookie : cookies) {
           System.out.println("cookie:"+cookie);
           }
           System.out.println("所有cookie:"+cookies);
           // 根據名字獲取cookie
           Cookie outman = dirver.manage().getCookieNamed("outman");
           System.out.println("outman的cookie:"+outman);
           // 根據name刪除cookie 
           dirver.manage().deleteCookieNamed("outman");
           Cookie outman2 = dirver.manage().getCookieNamed("outman");
           System.out.println("outman的cookie:"+outman2);
           // 刪除指定的cookie
           dirver.manage().deleteCookie(new Cookie("outman" , ""));
           Cookie outman3 = dirver.manage().getCookieNamed("outman");
           System.out.println("outman的cookie:"+outman3);
           // 刪除所有cookie
           dirver.manage().deleteAllCookies();
           Set<Cookie> cookies2 = dirver.manage().getCookies();
           System.out.println("所有cookie:"+cookies2);
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 高級用戶接口
          import org.openqa.selenium.By;
          import org.openqa.selenium.Keys;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.firefox.FirefoxDriver;
          import org.openqa.selenium.interactions.Actions;
          /**
           * 高級用戶接口
           * 
           * @author outman
           * @dete 2018-11-07
           */
          public class Demo_09 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExecute(dirver);
           }
           /**
           * 執行操作
           */
           private static void doExecute(WebDriver dirver) {
           dirver.get("http://www.baidu.com");
           Actions action = new Actions(dirver);
           WebElement trnews = dirver.findElement(By.name("tj_trnews"));
           WebElement trxueshu = dirver.findElement(By.name("tj_trxueshu"));
           WebElement kw = dirver.findElement(By.id("kw"));
           // perform() 方法為整個動作完成之后的提交操作
           // 移動鼠標至某個dom元素 (選中頁面 新聞)
          // action.moveToElement(trnews).perform();
           // 鼠標點擊元素
          // action.click(trnews).perform();
           // 鼠標點擊并懸停
          // action.clickAndHold(trnews).perform();
           // 在指定元素上點擊右鍵
          // action.contextClick(trnews).perform();
           // 在指定元素上雙擊
          // action.doubleClick(trnews).perform();
           // 點擊并拖拽 (將資源拖到 指定元素 , 或指定位置)
          // action.dragAndDrop(trnews , trxueshu).perform();
          // action.dragAndDropBy(trnews, 100, 100).perform();;
           // 鍵盤按下
          // action.keyDown(Keys.ENTER).perform();
           // 鍵盤按鍵抬起
          // action.keyUp(Keys.ENTER).perform();
           // 將光標移動至指定位置
          // action.moveByOffset(100, 100).perform();
           // 暫停指定的時間
           for(int i = 0 ; i<10 ;i++) {
           kw.sendKeys(i+"");
           action.pause(1000).perform();
           }
           // 釋放鼠標
           action.release().perform();
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 操作等待
          import java.util.concurrent.TimeUnit;
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.firefox.FirefoxDriver;
          import org.openqa.selenium.support.ui.ExpectedCondition;
          import org.openqa.selenium.support.ui.WebDriverWait;
          /**
           * 操作等待
           * @author outman
           * @date 2018-11-07
           * */
          public class Demo_10 {
           public static void main(String[] args) {
           // 打開瀏覽器
           WebDriver dirver = openFirFox();
           // 執行操作
           doExecute(dirver);
           }
           /**
           * 執行操作
           * */
           private static void doExecute(WebDriver driver) {
           // 顯式等待
           driver.get("http://www.baidu.com");
           WebElement kw = driver.findElement(By.id("kw"));
           kw.sendKeys("webDriver");
          // 顯式等待 針對單個元素 隱式等待針對整個頁面
          // 最大超時時間是10秒
          // 默認每隔500毫秒掃描一次 如果檢測到結果則返回
           WebDriverWait wait = new WebDriverWait(driver, 10, 1);
           //獲取第一條結果
           WebElement resultOne = wait.until(new ExpectedCondition<WebElement>() {
           public WebElement apply(WebDriver driver) {
           return driver.findElement(By.id("1"));
           }
           }).findElement(By.tagName("h3")).findElement(By.tagName("a"));
           System.out.println(resultOne.getText());
           resultOne.click();
           //隱式等待
           //設置頁面加載時間最大為10秒
          // driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
          // driver.get("http://www.baidu.com");
          // 
          // //定位對象設置10秒超時時間 , 10秒還定位不到則拋出異常
          // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          // WebElement kw = driver.findElement(By.id("kw"));
          // 
          // //異步腳本執行超時時間設置為3秒 
          // driver.manage().timeouts().setScriptTimeout(1, TimeUnit.SECONDS);
           }
           /**
           * 打開火狐瀏覽器
           */
           private static WebDriver openFirFox() {
           // geckodriver 下載地址: https://github.com/mozilla/geckodriver
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           // 讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           WebDriver dirver = new FirefoxDriver();
           return dirver;
           }
          }
          
          1. 執行js腳本
          // 創建js 執行對象
          JavascriptExecutor jsExceut = (JavascriptExecutor) dirver;
          // 導航到百度
          dirver.get("http://www.baidu.com");
          // 創建多個窗口
          String js = "window.open(\"https://www.sogou.com\");";
          String js2 = "window.open(\"http://www.w3school.com.cn/xpath/xpath_syntax.asp\");";
          // 當只傳入js腳本時 , 默認執行該js 的對象時瀏覽器
          jsExceut.executeScript(js);
          jsExceut.executeScript(js2);
          
          1. 同時啟動多個測試
          2. 主類
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.WebElement;
          import org.openqa.selenium.support.ui.ExpectedCondition;
          import org.openqa.selenium.support.ui.WebDriverWait;
          /**
           * 同時啟動多個瀏覽器執行測試
           */
          public class Demo_11 {
           public static void main(String[] args) {
           for (int i = 0; i < 5; i++) {
           new Thread(new Runnable() {
           public void run() {
           new Demo_11FirFox().doExecute();
           }
           }).start();
           new Thread(new Runnable() {
           public void run() {
           new Demo_11IE().doExecute();
           }
           }).start();
           new Thread(new Runnable() {
           public void run() {
           new Demo_11Chrome().doExecute();
           }
           }).start();
           }
           }
           public void doExecute(WebDriver driver) {
           driver.get("http://www.baidu.com");
           WebElement sbtn = driver.findElement(By.id("kw"));
           sbtn.sendKeys("webDriver");
           WebDriverWait wait = new WebDriverWait(driver, 10, 1);
           WebElement resultOne = wait.until(new ExpectedCondition<WebElement>() {
           public WebElement apply(WebDriver input) {
           return input.findElement(By.id("1"));
           }
           }).findElement(By.tagName("h3")).findElement(By.tagName("a"));
           // 點擊第一條
           resultOne.click();
           }
          }
          
          1. 谷歌瀏覽器測試類
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.chrome.ChromeDriver;
          public class Demo_11Chrome extends Demo_11 {
           public WebDriver driver = null;
           public Demo_11Chrome() {
           System.setProperty("webdriver.chrome.driver","D:\\DevelopSoftware\\chromedriver_win32\\chromedriver.exe");
           driver = new ChromeDriver();
           }
           public void doExecute() {
           super.doExecute(driver);
           }
          }
          
          1. 火狐瀏覽器測試類
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
          public class Demo_11FirFox extends Demo_11 {
           private WebDriver driver = null;
           public Demo_11FirFox() {
           System.setProperty("webdriver.gecko.driver", "D:\\DevelopSoftware\\geckodriver-v0.23.0-win64\\geckodriver.exe");
           //讀取獲取瀏覽器二進制文件
           System.setProperty("webdriver.firefox.bin", "D:\\應用\\FoxMail\\firefox.exe");
           driver = new FirefoxDriver();
           }
           public void doExecute() {
           super.doExecute(driver);
           }
          }
          
          1. IE瀏覽器測試類
          import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.ie.InternetExplorerDriver;
          public class Demo_11IE extends Demo_11 {
           private WebDriver driver = null;
           public Demo_11IE() {
           System.setProperty("webdriver.ie.driver",
           "D:\\DevelopSoftware\\IEDriverServer_x64_3.141.0\\IEDriverServer.exe");
           driver = new InternetExplorerDriver();
           }
           public void doExecute() {
           super.doExecute(driver);
           }
          }
          

          后記

          感謝各位客官的閱讀 , 如果文章中有錯誤或剖析的不夠透徹還請您能夠給不吝賜教在評論中告訴小編 , 以便小編能夠及時調整文章內容 , 為大家帶來更加優質的文章


          主站蜘蛛池模板: 大帝AV在线一区二区三区| www一区二区三区| 国产精品一区二区av不卡| 国产精品视频免费一区二区三区| 亚洲丶国产丶欧美一区二区三区 | 少妇特黄A一区二区三区| 蜜芽亚洲av无码一区二区三区| 亚洲国产成人精品无码一区二区| 毛片无码一区二区三区a片视频| 亚洲国产欧美一区二区三区| 亚洲国产成人久久一区久久| 久久精品无码一区二区无码| 人妻无码一区二区三区四区| 无码乱码av天堂一区二区| 日韩一区二区三区在线观看| 亚洲码一区二区三区| 国产亚洲日韩一区二区三区| 亚洲va乱码一区二区三区| 伊人激情AV一区二区三区| 一区在线观看视频| 狠狠做深爱婷婷久久综合一区| 国产伦精品一区二区三区| 风流老熟女一区二区三区| 亚洲av无码一区二区三区观看| 国产午夜三级一区二区三| 亚洲av无码成人影院一区| 亚洲国产一区二区a毛片| 国产精品亚洲一区二区无码| 亚洲电影国产一区| 午夜影视日本亚洲欧洲精品一区| 中文字幕一区二区人妻| 国产品无码一区二区三区在线蜜桃 | 无码精品人妻一区二区三区漫画| 一区五十路在线中出| 国产成人一区二区在线不卡| 91精品国产一区| 午夜性色一区二区三区不卡视频| 中文字幕人妻第一区| 国产伦理一区二区三区| 日本高清一区二区三区 | 一区在线观看视频|