首先創建Maven項目
在pom.xml裏面加上,這個是selenium目前最新的Maven依賴包
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-5</version>
</dependency>
其次下載3個瀏覽器需要的selenium的驅動程序
我這裏已經下載了目前最新的驅動,前提所有的瀏覽器版本是最新的;需要的可以點擊這裏下載3種都有。
鏈接:https://pan.baidu.com/s/18M1Fya1ZJbEpy0SskI9nqQ 提取碼:0naa
如果不想下載,有第二種方法,前提瀏覽器要有這個selenium的插件(僅限谷歌和火狐,IE沒有插件),而且谷歌還必須要有賬號才能打開谷歌網上應用店,如下圖所示:
一、Chrome的實現代碼
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome_Browser {
@Test
public void f() throws InterruptedException {
//寫代碼操作瀏覽器,模擬人工測試:api
// System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
// 1、 創建一個Chrome驅動
WebDriver driver = new ChromeDriver();
// 2、輸入百度網站進行訪問
driver.get("http://www.baidu.com");
// 3、找到百度輸入框(元素定位,定位一個元素)
// 根據id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 說明確實找到這個輸入框
System.out.println(input.getAttribute("maxlength"));
// 4、往輸入框輸入關鍵字
input.sendKeys("CSDN");
// 5、點擊百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//絕對路徑
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相對路徑
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 獲得輸入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("輸入框的值:" + actualkeywords);
// 斷言: 驗證結果符合我的預期,符合就不打印;不符合,就把不同結果打印出來
Assert.assertEquals("CSDN", actualkeywords);
// 等待時間 3秒
Thread.sleep(3000);
//退出瀏覽器
driver.quit();
}
}
二、FireFox的實現代碼
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFox_Browser {
@Test
public void f() throws InterruptedException {
//寫代碼操作瀏覽器,模擬人工測試:api
// System.setProperty("webdriver.firefox.driver", "src/main/resources/geckodriver.exe");
// 1、firefox驅動
WebDriver driver = new FirefoxDriver();
// 2、輸入百度網站進行訪問
driver.get("http://www.baidu.com");
// 3、找到百度輸入框(元素定位,定位一個元素)
// 根據id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 說明確實找到這個輸入框
System.out.println(input.getAttribute("maxlength"));
// 4、往輸入框輸入關鍵字
input.sendKeys("CSDN");
// 5、點擊百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//絕對路徑
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相對路徑
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 獲得輸入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("輸入框的值:" + actualkeywords);
//// 驗證結果符合我的預期,符合就不打印;不符合,就把不同結果打印出來
Assert.assertEquals("CSDN", actualkeywords);
// 等待時間 3秒
Thread.sleep(3000);
//退出瀏覽器
driver.quit();
}
}
三、IE瀏覽器實現代碼
這個比較麻煩,所以有點特殊,會有安全提醒,最好把所有的殺毒軟件關閉,不然可能會提示攔截信息。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* @author ZF
* @Description TODO
* @Date 2020/4/9 20:29
*/
public class IE_Browser {
@Test
public void test() throws InterruptedException {
// 1、驅動文件找不到的異常
System.setProperty("webdriver.ie.driver", "src/main/resources/IEDriverServer.exe");
// 創建一個對象,用來設置創建ie驅動時的各種設置
DesiredCapabilities capabilities = new DesiredCapabilities();
// 2、取消IE安全設置(忽略IE的Protected Mode的設置)
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
// 3、忽略瀏覽器的頁面縮放設置
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
// 4、設置一個初始化頁面,防止window對象丟失
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, true);
// IE驅動
WebDriver driver = new InternetExplorerDriver(capabilities);
// 2、輸入百度網站進行訪問
driver.get("http://www.baidu.com");
// 等待時間 3秒
Thread.sleep(3000);
driver.quit();
}
}
總結:
這些代碼我沒少趟坑,特別是IE,很多博客說需要在安全設置把那個啓用保護模式勾選去掉,其實根本沒必要去掉,經驗:所有的設置都沒必要,只需要把360殺毒軟件關閉就行了,防止提示惡意軟件。下圖就是勾選了也能打開,還有所有瀏覽器讓設置什麼環境變量的,也都不需要,只需要按照我這個全網最簡單的博客,就都可以用的包括很多代碼python也都適用的。下圖就是證明那些博客不用設置的最好證據。