1. 程式人生 > >centos7環境用selenium開啟chrome瀏覽器java程式碼

centos7環境用selenium開啟chrome瀏覽器java程式碼

前言:centos系統最好用7以上版本,老版本對chrome相容不太好。前面環境安裝配置過程可參考:https://blog.csdn.net/zhuyiquan/article/details/79537623
1、CentOS/RedHat 7以上安裝google-chrome可以完全參考 https://intoli.com/blog/installing-google-chrome-on-centos/ (6及以下版本不適用,一定請大家注意,強調三次)。
2、最新版本Chromedriver安裝
為了匹配chrome版本Google Chrome 65.0.3325.146,下載最新版本的chromedriver 2.3.6 linux64位http://npm.taobao.org/mirrors/chromedriver/2.36/chromedriver_linux64.zip
解壓後部署到/opt/drivers目錄下,嘗試執行:

[[email protected] drivers]# ./chromedriver 
Starting ChromeDriver 2.36.540471 (9c759b81a907e70363c6312294d30b6ccccc2752) on port 9515
Only local connections are allowed.

成功!如果在執行./chromedriver的時候報permission denied錯誤說明許可權問題,為了獲得執行許可權,藉助chmod指令修改檔案許可權即可。如下所示:chomd 777 chromedriver chomd 777為最高許可權,這樣再執行上面指令即可開啟chromedriver了。(如果,Java Selenium執行時chromedriver可能因為找不到localhost報超時異常(Timed out waiting for [

http://localhost:9567/status])那麼要修改/etc/hosts,繫結127.0.0.1 localhost,即可。)
3、chromedriver與chrome各版本對應關係參考:https://www.cnblogs.com/qingqing-919/p/9055285.html
chromedriver各個版本下載地址:http://chromedriver.storage.googleapis.com/index.html
4、上面第二步已經開啟chromedriver服務,埠號是9515,所以java程式碼如下:

		WebDriver driver = null;
    	ChromeOptions chromeOptions = new ChromeOptions();
    	chromeOptions.addArguments("--headless","--no-sandbox","--disable-gpu","--window-size=1290,1080");
    	//chromeOptions.addArguments("headless");//無介面引數
        //chromeOptions.addArguments("no-sandbox");//禁用沙盒
        DesiredCapabilities chromeCap = DesiredCapabilities.chrome();
        chromeCap.setCapability("chromeOptions", chromeOptions);
        try {
			URL remoteAddress = new URL("http://localhost:9515");
			driver = new RemoteWebDriver(remoteAddress, chromeCap);
	        driver.manage().window().maximize();
	        driver.get(openUrl);
	        // 超過20秒即為超時,會丟擲Exception
	        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
	        driver.getPageSource();
	        logger.info("Selenium開啟頁面title:"+driver.getTitle());
	        //等待頁面載入完成
	        Thread.sleep(10 * 1000);
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
            driver.close();
            driver.quit();
        }

引數說明
–headless 必須使用靜默模式,無GUI介面;
–disable-gpu 必須要禁用掉gpu,因為伺服器沒有圖形顯示相關支援;
–window-size 自定義視窗大小,因為瀏覽器的Window大小會決定獲取到元素的可能性。
**注:**如果是在windows環境的話,chromedriver要下載windows版本,然後java程式碼如下:

        WebDriver driver = null;
        logger.info("Selenium開啟url:" + openUrl + " chromeDriverUrl:" + chromeDriverUrl);
        System.setProperty("webdriver.chrome.driver", chromeDriverUrl);
        try {
            driver = new ChromeDriver();
            driver.get(openUrl);
            // 超過20秒即為超時,會丟擲Exception
            driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
            driver.getPageSource();
            logger.info("Selenium開啟頁面title:"+driver.getTitle());
            //等待頁面載入完成
            Thread.sleep(10 * 1000);
        } catch (Exception e) {
            logger.error("getHtmlBySelenium異常:url-->" + openUrl + e);
        } finally {
            driver.close();
            driver.quit();
        }