(四)selenium打開和關閉瀏覽器


一、Selenium簡介

 

Selenium3.0主要變更特性:

①移除seleniumRC

②FireFox和Safari推出了自己的driver(geckodriver 和 Safaridriver)

③Selenium3 支持 IE9+,Selenium2 支持 7-11

④全面擁抱java8

 

 二、Selenium實現自動打開瀏覽器

1.打開火狐瀏覽器

注意:火狐48以下版本可以直接使用webdriver自帶的進行打開瀏覽器

          火狐48以上版本則需要使用版本對應的geckodriver

public class OpenBrowserTest {
    @Test
    /*火狐是默認安裝在C盤的啟動方法*/
 /*   public void openFirefox1(){
        WebDriver webDriver = new FirefoxDriver();
    }*/
    /*火狐不是默認安裝在C盤 需要寫上路徑*/
    public void openFirefox2(){
        //指定firefox 安裝路徑
        System.setProperty("webdriver.firefox.bin","F:\\Program Files (x86)\\Firefox\\Mozilla Firefox\\firefox.exe");
        //啟動firefox瀏覽器
        WebDriver webDriver = new FirefoxDriver();
    }
}

2.打開chrome瀏覽器

右上角->幫助-->關於chrome-->查看當前chrome版本-->到指定的地址    去下載對應的ChromeDriver

附chromedriver與chrome的對應關系表:

chromedriver版本 支持的Chrome版本
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
v2.32 v59-61
v2.31 v58-60
v2.30 v58-60
v2.29 v56-58
v2.28 v55-57
v2.27 v54-56
v2.26 v53-55
v2.25 v53-55
v2.24 v52-54
v2.23 v51-53
v2.22 v49-52
v2.21 v46-50
v2.20 v43-48
v2.19 v43-47
v2.18 v43-46
v2.17 v42-43
v2.13 v42-45
v2.15 v40-43
v2.14 v39-42
v2.13 v38-41
v2.12 v36-40
v2.11 v36-40
v2.10 v33-36
v2.9 v31-34
v2.8 v30-33
v2.7 v30-33
v2.6 v29-32
v2.5 v29-32
v2.4 v29-32

 

映射表轉自:

http://blog.csdn.net/huilan_same/article/details/51896672

從對應的地址下載 與自己的瀏覽器對應的 chromedriver版本:http://chromedriver.storage.googleapis.com/index.html

 

 project-->右鍵-->new--->Directory-->drivers-->把當前下載的拉進來--->則這個目錄為寫在webdriver內的property的目錄。

 

 @Test
    public void openChrome(){
        /*將下載好的chrome驅動拉進來,輸入驅動路徑*/
            System.setProperty("webdriver.chrome.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\chromedriver.exe");
            WebDriver webDriver = new ChromeDriver();
    }

 

3.打開IE瀏覽器

進入指定頁下載對應版本的IEDriver 進入索引頁,首先選擇版本號,IEDriverServer的版本號和Selenium的版本號一定要一致 建議選win32 如下:

IEDriverServer_Win32_2.53.0.zip

下載地址:http://selenium-release.storage.googleapis.com/index.html

操作與下載chromedriver類似,下載完后解壓,解壓后路徑復制進對應的driver地址。

@Test
    public void openIE(){
        /*將下載好的IE驅動拉進來建議下載與webdriver一樣的32位的版本,輸入驅動路徑*/
        System.setProperty("webdriver.ie.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\IEDriverServer.exe");
        WebDriver webDriver = new InternetExplorerDriver();
    }

啟動ie報錯:

Started InternetExplorerDriver server (32-bit)
2.53.0.0
Listening on port 27578
Only local connections are allowed

org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 150%. It should be set to 100% (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 3.17 seconds
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'DESKTOP-UA0J10K', ip: '192.168.0.102', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver

針對windows vista和windows 7上的IE7或者更高的版本,必須在IE選項設置的安全頁中,4個區域的啟用保護模式的勾選都去掉(或都勾上),即保持四個區域的保護模式是一致的。如下圖所示:

 右上角設置->internet選項->安全->

 

 點擊窗口最上面-->選擇顯示菜單欄-->點擊左上角-->工具--->查看當前顯示是否為100%
瀏覽器的縮放比例必須設置為100%,這樣元素定位才不會出現問題。

針對IE10和更高的版本,必須在IE選項設置中的高級頁中,取消增強保護模式。

 開始”→“運行”命令,在“運行”對話框的“打開”欄中輸入“regedit 命令”(windows+R)然后單擊“確定1、查找到“項”——“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace”
 
 
 針對IE11,需要修改注冊表。如果是32位的windows,key值為
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE
64位的windows,key值為
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE
如果key值不存在,就添加。之后在key內部創建一個iexplorer.exe,DWORD類型,值為0。

 

 4.windows10 才自帶的瀏覽器 edge,不是windos10是沒有這個瀏覽器的
不同的edge版本 下載的 對應版本也是不一樣。
右上角....----> 設置 -->  拉到最下面查看當前edge的版本--17134

上對應的網站下載對應的driver

 https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

往下拉,可以看到對應版本下載,放到指定文件夾

 @Test
    public void openEdge(){
        System.setProperty("webdriver.edge.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\MicrosoftWebDriver.exe");
        WebDriver webDriver =new EdgeDriver();
    }

 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class OpenBrowserTest {
    @Test
    /*火狐是默認安裝在C盤的啟動方法*/
   /* public void openFirefox1(){
        WebDriver webDriver = new FirefoxDriver();
    }*/
    /*火狐不是默認安裝在C盤 需要寫上路徑*/
    public void openFirefox2(){
        //指定firefox 安裝路徑
        System.setProperty("webdriver.firefox.bin","F:\\Program Files (x86)\\Firefox\\Mozilla Firefox\\firefox.exe");
        //啟動firefox瀏覽器
        WebDriver webDriver = new FirefoxDriver();
    }
    @Test
    public void openChrome(){
        /*將下載好的chrome驅動拉進來,輸入驅動路徑*/
            System.setProperty("webdriver.chrome.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\chromedriver.exe");
            WebDriver webDriver = new ChromeDriver();
    }
    @Test
   /* DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
    ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
    WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);*/
    public void openIE(){
        /*將下載好的IE驅動拉進來建議下載與webdriver一樣的32位的版本,輸入驅動路徑*/
        System.setProperty("webdriver.ie.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\IEDriverServer.exe");
        WebDriver webDriver = new InternetExplorerDriver();
    }
    @Test
    public void openEdge(){
        System.setProperty("webdriver.edge.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\MicrosoftWebDriver.exe");
        WebDriver webDriver =new EdgeDriver();
    }

}

 

 三、selenium關閉瀏覽器

前提是先下載了對應的瀏覽器的driver並能進行正常打開瀏覽器

關閉當前窗口

driver.close();

關閉當前窗口並退出

driver.quit();

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ClosedBrowserTest {
    @Test
    public void closedChrome() throws InterruptedException {
        //設置chrome路徑
        System.setProperty("webdriver.chrome.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\chromedriver.exe");
        //實例化chromeDriver
        WebDriver driver = new ChromeDriver();
        //等待5000ms,馬上開馬上關閉則不明顯
        Thread.sleep(5000);
        //關閉當前窗口
        //driver.close();
        //完全退出瀏覽器 避免瀏覽器資源被耗盡,一般只會使用driver.close();
        driver.quit();
    }
}

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM