做自動化時經常會遇到不兼容的問題,比如以下簡單的腳本,主要是打開瀏覽器,然后最大化窗口,打開百度,輸入內容搜索,代碼如下:
package com.gs.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class demo1 { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "E:\\ware\\selenium\\chromedriver_win32\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars");// 設置chrome瀏覽器的參數,使其不彈框提示(chrome正在受自動測試軟件的控制) WebDriver dr = new ChromeDriver(options); dr.manage().window().maximize();// 最大化瀏覽器 dr.get("https://www.baidu.com");// 打開要測試的網址,比如百度 dr.findElement(By.id("kw")).sendKeys("hello");// 輸入搜索關鍵字 System.out.print("打開網頁"); try { Thread.sleep(6000); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } dr.close();//關閉瀏覽器 }
selenium 3.7版本放大瀏覽器窗口報錯如下
報錯:Exception in thread "main" org.openqa.selenium.WebDriverException: disconnected: unable to connect to renderer
查看報錯,腳本運行到窗口最大化時出現錯誤的,經過仔細分析,發現chrom版本是62,與chromedriver不兼容的問題造成的。
解決方法:
去如下網址下載最下的chromedriver:
https://npm.taobao.org/mirrors/chromedriver/
下載后在運行以上腳本,問題順利解決。
最后附上java工程jar包圖: