Web自動化測試-掌握selenium工具用法,使用WebDriver測試Chrome/FireFox網頁(Java

目錄

?一、在Eclipse中構建Maven項目

1.全局配置Maven

2.配置JDK路徑

3.創建Maven項目

4.引入selenium-java依賴

二、Chrome自動化腳本編寫

1.創建一個ChromeTest類

2.測試ChromeDriver

3.下載chromedriver驅動

4.在腳本中通過System.setProperty方法指定chromedriver的地址

5.測試學習通網址登錄功能

三、FireFox自動化腳本編寫

1.新建一個FireFoxTest類

2.指定firefox可執行文件路徑: webdriver.firefox.bin

3.下載geckodriver驅動

4.在腳本中通過System.setProperty方法指定chromedriver的地址

工具:eclipse(2016)、chrome(v.125)

依賴:selenium-java(3.141.59)

驅動:chromedriver(win64 v125)

配置環境:jdk1.8.0、? maven3.5.2

?一、在Eclipse中構建Maven項目

1.全局配置Maven

點擊Windows->Preferences

注意:要先在settinfs.xml中配置阿里云鏡像倉庫,可參考該文章1~3步驟IDEA 使用自定義MAVEN(maven安裝及IDEA配置)_idea 用自定義maven-CSDN博客

同時在installations中add maven路徑

2.配置JDK路徑

同樣是在Preferences中,確認指向的是JDK的路徑而不 是JRE的路徑

3.創建Maven項目

點擊File->New->Project...

勾選Create a simple...

填入組名和項目名,點擊Finish

創建完項目列表如下:

4.引入selenium-java依賴

在Maven官網可以下載:Maven Repository: Search/Browse/Explore (mvnrepository.com)

搜索selenium,選擇Selenium Java

選擇使用度較高的版本,這里選擇了4.18.1

拷貝對應的Maven依賴包

點擊pom.xml粘貼進去,注意要放在<dependencies></dependencies>里面

保存后,后自動生成Maven Dependendies

二、Chrome自動化腳本編寫

1.創建一個ChromeTest類

2.測試ChromeDriver

輸入以下代碼,點擊運行

import org.openqa.selenium.chrome.ChromeDriver;public class ChromeTest {public static void main(String[] args) throws Exception{ChromeDriver driver = new ChromeDriver();}
}

若報以下錯,說明Selenium Java版本過高,需要下載較低版本

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openqa/selenium/chrome/ChromeDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:763)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)at java.net.URLClassLoader.access$100(URLClassLoader.java:73)at java.net.URLClassLoader$1.run(URLClassLoader.java:368)at java.net.URLClassLoader$1.run(URLClassLoader.java:362)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:361)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at com.test.ChromeTest.main(ChromeTest.java:9)

這里我將依賴換成了3.141.59版本

<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version>
</dependency>

保存后,再次運行報錯以下信息,這是正常情況,因為我們還沒有設置Chrome瀏覽器的驅動

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.htmlat com.google.common.base.Preconditions.checkState(Preconditions.java:847)at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)at com.test.WebTest01.main(WebTest01.java:12)
3.下載chromedriver驅動

因為我的Chrome瀏覽器的版本是125,在以下鏈接可以下載對應版本

chromedriver瀏覽器驅動各版本下載(...113、114、115、116、117、118、119、120、121、122、123、124、125、126、127)(原創) - Z哎呀 - 博客園 (cnblogs.com)

將下載解壓的chromedriver.exe復制?

粘貼到項目的resources里面

4.在腳本中通過System.setProperty方法指定chromedriver的地址
// 系統設置Chrome驅動文件的路徑
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();

再次運行,此時彈出Chrome窗口,運行成功

5.測試學習通網址登錄功能
public static void main(String[] args) throws Exception{// 系統設置Chrome驅動文件的路徑System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");ChromeDriver driver = new ChromeDriver();//最大化瀏覽器窗口driver.manage().window().maximize();// 測試學習通網站String url = "https://passport2.chaoxing.com";driver.get(url);Thread.sleep(3000);// 通過F12查看對應id// 輸入手機號WebElement phoneElement = driver.findElement(By.id("phone")); //手機號phoneElement.clear(); //清空文本輸入框中的內容phoneElement.sendKeys("xxxxxxxxxx"); //在文本輸入框中輸入內容String phoneValue = phoneElement.getAttribute("value"); //獲取文本框中已經輸入的內容Thread.sleep(1000);// 輸入密碼WebElement passwordElement = driver.findElement(By.id("pwd"));passwordElement.clear();passwordElement.sendKeys("xxxxxxxxxx");String passwordValue = passwordElement.getAttribute("value");Thread.sleep(1000);//輸出對應值System.out.println(phoneValue);System.out.println(passwordValue);//通過className獲取自動勾選框WebElement checkElement = driver.findElement(By.className("check-input"));// 使用isSelected()方法檢查復選框是否被選中  boolean isSelected = checkElement.isSelected();  // 輸出結果  System.out.println("復選框是否被選中: " + isSelected); // 如單復選框沒有被選中if (!isSelected) {checkElement.click(); //點擊選中}Thread.sleep(3000);// 點擊登錄WebElement loginElement = driver.findElement(By.id("loginBtn"));loginElement.click();}

點擊運行后,將會彈出網址,自動登錄

三、FireFox自動化腳本編寫

1.新建一個FireFoxTest類
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;public class FireFoxTest {public static void main(String[] args) throws Exception{FirefoxDriver driver = new FirefoxDriver();}
}

運行后報錯以下信息,這是正常情況,原因是firefox安裝在其它路徑,不是默認的安裝路徑

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN10
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-VMB9KRO', ip: '10.194.105.24', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121'
Driver info: driver.version: FirefoxDriverat org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:100)at java.util.Optional.orElseGet(Optional.java:267)at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:216)at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:187)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)at com.test.FireFoxTest.main(FireFoxTest.java:10)
2.指定firefox可執行文件路徑: webdriver.firefox.bin

找到Firefox的exe執行文件,添加路徑對應以下代碼,再次運行

//指定firefox可執行文件路徑
System.setProperty("webdriver.firefox.bin","C:\\Users\\86153\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
FirefoxDriver driver = new FirefoxDriver();

再次報錯以下信息,這也是因為沒有配置FireFox驅動

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releasesat com.google.common.base.Preconditions.checkState(Preconditions.java:847)at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:44)at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:167)at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:190)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)at com.test.FireFoxTest.main(FireFoxTest.java:11)
3.下載geckodriver驅動

selenium,geckodriver,firefox對應版本官網參考:

Supported platforms — Firefox Source Docs documentation (mozilla.org)

我的FireFox版本是126,下載對應0.34.0版本

進入網址:https://github.com/mozilla/geckodriver?,點擊“Tags”,點擊0.34.0版本的download,然后選擇對應平臺的壓縮包下載

同理,解壓復制到resources文件里

4.在腳本中通過System.setProperty方法指定chromedriver的地址
//指定firefox可執行文件路徑System.setProperty("webdriver.firefox.bin","C:\\Users\\86153\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
// 系統設置gecko驅動文件的路徑
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();

再次運行,彈出Firefox窗戶,成功!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/20472.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/20472.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/20472.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vi和vim有什么不同?

vi 和 vim 都是流行的文本編輯器&#xff0c;它們之間有以下主要區別&#xff1a; 歷史&#xff1a; vi 是一個非常古老的文本編輯器&#xff0c;最初由 Bill Joy 在 1976 年為 Unix 系統編寫。vim&#xff08;Vi IMproved&#xff09;是 vi 的一個增強版&#xff0c;由 Bram M…

Ubuntu 20.04安裝CMake 3.22.6版本

Ubuntu 20.04通過apt安裝的cmake版本是3.16.3&#xff0c;默認安裝到/usr/bin/cmake路徑。 $ cmake Command cmake not found, but can be installed with:sudo snap install cmake # version 3.29.3, or sudo apt install cmake # version 3.16.3-1ubuntu1.20.04.1See sna…

Multer 文件上傳中間件 和 Busboy表單解析

Multer 是一個node.js中間件&#xff0c;用于處理 multipart/form-data類型的表單數據&#xff0c;主要用于上傳文件。只處理 multipart/form-data 類型的表單數據。 Multer是基于Busboy解析的文件參數信息&#xff0c;獲取fileStream&#xff0c;并通過storage轉存的file.str…

Unity + 雷達 粒子互動(待更新)

效果預覽: 花海(帶移動方向) VFX 實例 腳本示例 使用TouchScript,計算玩家是否移動,且計算移動方向 using System.Collections; using System.Collections.Generic; using TouchScript; using TouchScript.Pointers; using UnityEngine; using UnityEngine.VFX;public …

AI預測福彩3D采取888=3策略+和值012路一縮定乾坤測試6月1日預測第8彈

今天繼續基于8883的大底&#xff0c;使用盡可能少的條件進行縮號。好了&#xff0c;直接上結果吧~ 首先&#xff0c;888定位如下&#xff1a; 百位&#xff1a;6,5,4,7,8,9,1,0 十位&#xff1a;7,8,6,5,9,3,1,0 個位&#xff1a;5,7,6,4,2,…

看廣告賺金幣提現小游戲app開發源碼

開發一個看廣告賺金幣并可以提現的小游戲APP&#xff0c;源碼的搭建涉及到多個方面&#xff0c;包括前端界面設計、后端邏輯處理、數據庫管理以及廣告平臺的對接等。以下是一些建議的步驟和考慮因素&#xff1a; 前端界面設計&#xff1a; 使用HTML5、CSS3和JavaScript等技術…

第十三屆藍橋杯B組c++國賽

A - 2022&#xff1a; 題目&#xff1a; 筆記&#xff1a; 一道經典的dp題&#xff1a; &#xff08;1&#xff09;明確dp數組含義&#xff1a; dp[i][j][k]: 表示前i個數字中選擇j個湊成k的方法數。 &#xff08;2&#xff09;確定狀態轉移方程&#xff1a; dp[i][j][k…

C++中的引用和解引用,及在Lambda中的簡單使用

目錄 摘要 引用&#xff08;Reference&#xff09; 定義 用法 解引用&#xff08;Dereference&#xff09; 定義 用法 Lambda表達式結合引用和解引用 引用結合Lambda表達式 解引用結合Lambda表達式 較為復雜的使用 總結 摘要 在C中&#xff0c;引用&#xff08;Re…

linux 內核哪種鎖可以遞歸調用 ?

當數據被多線程并發訪問(讀/寫)時&#xff0c;需要對數據加鎖。linux 內核中常用的鎖有兩類&#xff1a;自旋鎖和互斥體。在使用鎖的時候&#xff0c;最常見的 bug 是死鎖問題&#xff0c;死鎖問題很多時候比較難定位&#xff0c;并且影響較大。本文先會介紹兩種引起死鎖的原因…

Java-----String類

1.String類的重要性 經過了C語言的學習&#xff0c;我們認識了字符串&#xff0c;但在C語言中&#xff0c;我們表示字符串進行操作的話需要通過字符指針或者字符數組&#xff0c;可以使用標準庫中提供的一系列方法對字符串的內容進行操作&#xff0c;但這種表達和操作數據的方…

溝通程序化(1):跟著鬼谷子學溝通—“飛箝”之術

溝通的基礎需要傾聽&#xff0c;但如果對方聽不進你的話&#xff0c;即便你說的再有道理&#xff0c;對方也很難入心。讓我們看看鬼谷子的“飛箝”之術能給我們帶來什么樣的啟發吧&#xff01; “飛箝”之術&#xff0c;源自中國古代兵法家、縱橫家鼻祖鬼谷子的智慧&#xff0…

SpringBootWeb 篇-深入了解 Spring 異常處理、事務管理和配置文件參數配置化、yml 配置文件

&#x1f525;博客主頁&#xff1a; 【小扳_-CSDN博客】 ?感謝大家點贊&#x1f44d;收藏?評論? 文章目錄 1.0 配置文件 1.1 yml 配置文件 1.2 參數配置化 1.2.1 使用 Value 注解注入單個配置參數 1.2.2 使用 ConfigurationProperties 注解將一組相關配置參數注入到一個類中…

discuz論壇怎么修改備案信息

大家好&#xff0c;今天給大家分享下discuz如何填寫備案信息并且展示在網站首頁。大家都知道國內網站都需要備案&#xff0c;不通過備案的網站上是沒辦法通過域名打開的。大家也可以通過搜索網創有方&#xff0c;或者直接點擊網創有方 查看懸掛備案號后的效果。 首先大家可以看…

如何在CentOS中合理劃分磁盤空間以優化系統性能

目錄 前言 理想的分區方案 為什么需要單獨分區 安全性 性能 管理和維護 穩定性和可靠性 升級和兼容性 結論 前言 在進行CentOS系統的安裝和配置時&#xff0c;合理劃分磁盤空間是確保系統性能、安全性和易于管理的關鍵步驟。本文將探討如何根據系統的硬件配置和預期用途…

安全測試掃描利器-Burpsuite

&#x1f525; 交流討論&#xff1a;歡迎加入我們一起學習&#xff01; &#x1f525; 資源分享&#xff1a;耗時200小時精選的「軟件測試」資料包 &#x1f525; 教程推薦&#xff1a;火遍全網的《軟件測試》教程 &#x1f4e2;歡迎點贊 &#x1f44d; 收藏 ?留言 &#x1…

vscode常用插件及插件安裝方式

一、常用插件 Chinese (Simplified) (簡體中文) Language Pack for Visual Studio Code 說明&#xff1a;中文語言包擴展&#xff08;簡體&#xff09; open in browser 說明&#xff1a;可以在默認瀏覽器或應用程序中打開當前文件 Auto Rename Tag 說明&#xff1a;自動重…

Linux 命令:awk

1. 寫在前面 本文主要介紹 Linux “awk” 命令&#xff1a;“awk” 是另一個強大的文本處理工具&#xff0c;用于處理和操作結構化數據&#xff0c;如日志文件和命令輸出。它可以根據需要為我們打印特定的列值。 公眾號&#xff1a; 滑翔的紙飛機 2. awk 命令 我們能用 awk 做…

Android 控件保持寬高比得幾種方式

文章目錄 Android 控件保持寬高比得幾種方式adjustViewBounds百分比布局ConstraintLayout自定義View Android 控件保持寬高比得幾種方式 adjustViewBounds 僅適用于 ImageView&#xff0c;保持橫豎比。 <ImageViewandroid:layout_width"match_parent"android:l…

動態規劃(Dynamic-Programming)問題講解

動態規劃類問題 從已知子問題的解&#xff0c;推導出當前問題的解 推導過程可以表達為一個數學公式用一維或二維數組來保存之前的計算結果&#xff08;可以進一步降維優化&#xff09; 將當前問題 分解成子問題 &#xff0c;找出遞歸公式&#xff0c;分階段進行求解 求解過程中…

vue3+ts封裝一個button組件

創建一個新的Button組件文件 Button.vue&#xff1a; <template><button :class"buttonClass" :disabled"disabled" click"handleClick"><slot></slot><i v-if"icon" :class"icon"></i&g…