目錄
一、現有eclipse配置TestNG
安裝TestNG插件有兩種方式,一種是離線一種是在線。
1、離線
(1)下載testng安裝包,進入http://beust.com/eclipse下載。
(2)下載zipped文件後,在eclipse菜單欄中點擊“help”,選擇下拉菜單的“Install New Software”。出現如下界面
(3)點擊Add按鈕
(4)在項目上右鍵可以看見如下圖,說明安裝成功
2、在線安裝
(1)直接上圖片,進入Marktplace後搜索TestNG,直接安裝即可
3、安裝會出現問
不管是離線還是在線都會出現安裝過程卡住的現象
注意:安裝卡住解決辦法https://blog.csdn.net/qq_27009225/article/details/103743824
二、配置Selenium WebDriver
1、下載地址
https://selenium.dev/downloads/
下載對應的java版本
2、將下載的文件解壓
出現如下文件
注意:必須把三個文件都導入項目,只導入外面兩個jar包會出現問題報錯信息:java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
3、導入文件
三、下載對應瀏覽器驅動
安裝三大瀏覽器驅動driver
- Chrome驅動chromedriver 下載地址:chromedriver
- Firefox驅動geckodriver 下載地址:geckodriver
- IE驅動IEdriver 下載地址:IEdriver
- Edge驅動edgedriver下載地址:edgedriver
- IEDriverServer的版本號和Selenium的版本號一定要一致
Python查看selenium版本
1、Chrome驅動
-
Chrome驅動每個版本都有各自對應的瀏覽器版本,下面網頁中信息可供參考。
http://chromedriver.storage.googleapis.com/2.43/notes.txt -
下載驅動網址http://chromedriver.storage.googleapis.com/index.html
注意:驅動和瀏覽器版本不對應會報錯: -
如果找不到瀏覽器對應的驅動,可以下載舊版本Chrome:Chrome歷史版本
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:25606/status] to be available after 20012 ms
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107)
at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:190)
... 10 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:140)
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80)
... 11 mor
2、配置ChromeDriver環境變量
- 將webdriver複製到Chrome瀏覽器安裝目錄的Application文件下
當然,配置環境變量不是必選項,但是會省一點事情吧!要是配置環境變量了driver放在那裏就無所謂了,如果配置環境變量不生效可以試着註銷或者重啓
四、調用實例
- 如果你配置了webdriver的環境變量,就不用下面這行代碼
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome Beta\\Application\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver",
// "C:\\Program Files (x86)\\Google\\Chrome Beta\\Application\\chromedriver.exe");
WebDriver wd = new ChromeDriver();
wd.get("https://www.baidu.com");
Thread.sleep(1000);
wd.findElement(By.id("kw")).sendKeys("I Love You!");
Thread.sleep(1000);
wd.findElement(By.id("su")).click();
Thread.sleep(1000);
wd.quit();
四、IE啓動報錯
1、Unexpected error launching Internet Explorer
selenium.common.exceptions.SessionNotCreatedException: Message: Unexpected error launching Internet Explorer. Browser zoom level was set to 250%. It should be set to 100%
2、解決
3、第二種方法
五、啓動Edge報錯
報錯信息:urllib3.exceptions.ProtocolError
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '遠程主機強迫關閉了一個現有的連接。', None, 10054, None))
1、查看edge版本
在最下面
方式一:Edge18之前的版本
下載對應版本的MicrosoftWebDriver
方式二:Edge18之後的版本
在命令行中輸入
DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0
切記:此時確保Python的安裝目錄沒有之前版本的MicrosoftWebDriver,否則Python會默認先找到Python安裝目錄下的MicrosoftWebDriver,一直報錯
六、啓動Firefox報錯
1、Service geckodriver unexpectedly exited
Message: Service geckodriver unexpectedly exited. Status code was: 2
2、下載對應的driver