網(wǎng)站開發(fā)功能需求表百度關(guān)鍵詞優(yōu)化軟件
一.定義
?用來做web自動化測試的框架.
二.特點(diǎn)
1.支持各種瀏覽器.
2.支持各種平臺(操作系統(tǒng)).
3.支持各種編程語言.
4.有豐富的api.
三.工作原理
四.搭環(huán)境
1.對照Chrome瀏覽器版本號,下載ChromeDriver,配置環(huán)境變量,我直接把.exe文件放在了jdk安裝路徑的bin文件夾下了(jdk配置了環(huán)境變量).
2.創(chuàng)建mavem項(xiàng)目,在pom.xml文件中引入Selenium依賴.
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.7.2</version>
</dependency>
3.創(chuàng)建啟動類,用百度進(jìn)行測試.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;public class Main {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");WebDriver webDriver = new ChromeDriver(options);webDriver.get("https://www.baidu.com");}
}
如果正常運(yùn)行,則環(huán)境搭配好了.
五.css選擇器
1.id選擇器: #id
2.類選擇器: .class
3.標(biāo)簽選擇器: 標(biāo)簽
4.后代選擇器: 父級選擇器, 子級選擇器.
注意:兩種選擇器,建議使用CSS選擇器,因?yàn)樾矢?
六.Xpath選擇器
1.絕對路徑: /html/......(效率低,不常用).
2.相對路徑: //......
a.相對路徑+索引
//form/span[1]/input
注意: 數(shù)組下標(biāo)從1開始.
b.相對路徑+屬性值
//input[@class="s_ipt"]
c.相對路徑+通配符
//*[@*="s_ipt"]
d.相對路徑+文本匹配
?//a[text()="新聞"]
七.WebDriver的常用方法
1.click: 點(diǎn)擊.
2.sendKeys: 在對象上模擬鍵盤輸入.
3.clear: 清除對象輸入的文本內(nèi)容.
4.(不推薦使用)submit: 提交,和click作用一樣,但是有弊端,如果點(diǎn)擊的元素放在非form標(biāo)簽中,此時(shí)submit會報(bào)錯(比如超鏈接(a標(biāo)簽)).
5.text: 用于獲取元素的文本信息.
6.getAttribute: 獲取屬性值.
以上所有內(nèi)容的代碼練習(xí)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import java.util.List;import static java.lang.Thread.sleep;public class Main {public static void main(String[] args) throws InterruptedException {// 測試是否通過的標(biāo)致boolean flag = false;ChromeOptions options = new ChromeOptions();//允許所有請求options.addArguments("--remote-allow-origins=*");WebDriver webDriver = new ChromeDriver(options);// 1.打開百度首頁webDriver.get("https://www.baidu.com/");String title = webDriver.getTitle();String url = webDriver.getCurrentUrl();if (url.equals("https://www.baidu.com/") && title.equals("百度一下,你就知道")) {System.out.println("title和url正確");} else {System.out.println("title和url不正確");}// 2.兩種定位元素的方式: 1.cssSelector 2.Xpath// 使用瀏覽器,按F12,選中要測試的位置,在代碼中拷貝.// 找到百度搜索輸入框// 第一種: cssSelectorWebElement element = webDriver.findElement(By.cssSelector("#kw"));// 第二種: Xpath//WebElement Element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));// 3.輸入信息element.sendKeys("別克君越艾維亞");// 4.找到百度一下按鈕// 5.點(diǎn)擊按鈕webDriver.findElement(By.cssSelector("#su")).click();sleep(2000);// 6.校驗(yàn)List<WebElement> elements = webDriver.findElements(By.cssSelector("a"));for (int i = 0; i < elements.size(); ++i) {if(elements.get(i).getText().contains("別克君越") || elements.get(i).getText().contains("艾維亞")) {System.out.println("測試通過");flag = true;break;}}if (!flag) {System.out.println("測試不通過");}// 清空輸入框element.clear();sleep(1500);// 在輸入框中重新輸入內(nèi)容element.sendKeys("別克威朗");webDriver.findElement(By.cssSelector("#su")).submit();// 獲取屬性值:百度一下System.out.println(webDriver.findElement(By.cssSelector("#su")).getAttribute("value"));}
}
八.等待
1.強(qiáng)制等待(sleep): 一直等待到規(guī)定時(shí)間.
2.智能等待:?設(shè)置的等待時(shí)間是最長的等待時(shí)間,如果完成了任務(wù),會停止.
a.隱式等待(webDriver.manage().timeouts().implicitlyWait())
b.顯示等待: 指定某個任務(wù)進(jìn)行等待.
區(qū)別: 隱式等待是等待頁面上所有因素加載進(jìn)來,如果規(guī)定時(shí)間內(nèi)沒有加載進(jìn)來,就會報(bào)錯.而顯示等待并不關(guān)心是否加載進(jìn)來所有的元素,只要在規(guī)定時(shí)間內(nèi),在所有被加載進(jìn)來的元素中包含指定的元素,就不會報(bào)錯.
3.代碼練習(xí)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;import java.time.Duration;public class Main2 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");// 判斷元素是否可以被點(diǎn)擊// 隱式等待
// driver.manage().timeouts().implicitlyWait(Duration.ofDays(5));// 顯示等待WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(3000));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(7) > a")));}
}
九.瀏覽器操作
1.后退: webdriver.navigate().back();
2.刷新:?webdriver.navigate().refresh();
3.前進(jìn):?webdriver.navigate().forward();
4.滾動條操作:?使用js腳本
劃到最底端:?
((JavascriptExecutor)driver).executeScript("document.documentElement.scrollTop=10000");
5.最大化:?driver.manage().window().maximize();
6.全屏:?driver.manage().window().fullscreen();
7.設(shè)置長寬:?driver.manage().window().setSize(new Dimension(600, 800));
8.代碼
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import static java.lang.Thread.sleep;public class Main3 {public static void main(String[] args) throws InterruptedException {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾維亞");driver.findElement(By.cssSelector("#su")).click();sleep(1500);// 回退driver.navigate().back();sleep(1500);// 刷新driver.navigate().refresh();sleep(1500);// 前進(jìn)driver.navigate().forward();sleep(1500);// 滾動,使用js腳本// 劃到最底端((JavascriptExecutor)driver).executeScript("document.documentElement.scrollTop=10000");sleep(1500);// 最大化driver.manage().window().maximize();sleep(1500);// 全屏driver.manage().window().fullscreen();sleep(1500);// 最小化driver.manage().window().minimize();// 設(shè)置長寬driver.manage().window().setSize(new Dimension(600, 800));}
}
十.鍵盤
1.control + a:?
driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
2.代碼
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import static java.lang.Thread.sleep;public class Main4 {public static void main(String[] args) throws InterruptedException {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾維亞");// 鍵盤操作// control + Adriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");sleep(1500);// control + Xdriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");sleep(1500);// control + Vdriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");sleep(1500);}
}
十一.鼠標(biāo)
代碼:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;import static java.lang.Thread.sleep;public class Main5 {public static void main(String[] args) throws InterruptedException {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");driver.findElement(By.cssSelector("#kw")).sendKeys("君越艾維亞");driver.findElement(By.cssSelector("#su")).click();sleep(1500);// 鼠標(biāo)操作WebElement element = driver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));Actions actions = new Actions(driver);sleep(1500);actions.moveToElement(element).contextClick().perform();}
}
十二.特殊場景
1.定位一組元素:?
勾選復(fù)選框
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import java.util.List;public class Main6 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接driver.get("???");List<WebElement> elements = driver.findElements(By.cssSelector("input"));// 遍歷elements,如果vtype值是checkbox就點(diǎn)擊// 使用for (int i = 0; i < elements.size(); ++i) {if (elements.get(i).getAttribute("type").contains("checkbox")) {elements.get(i).click();}}}
}
2.多框架定位: 在iframe中的a標(biāo)簽使用常規(guī)方法無法定位
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;public class Main7 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接driver.get("???");// 對iframe底下的a標(biāo)簽進(jìn)行操作,不能直接定位,需要先切換// 輸入id號,找到指定的iframedriver.switchTo().frame("f1");driver.findElement(By.cssSelector("???")).click();}}
3.下拉框處理:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;public class Main8 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接driver.get("???");// 獲取下拉框的元素WebElement element = driver.findElement(By.cssSelector("???"));Select select = new Select(element);// 可以通過多種方式定位,常用以下兩種// 1.下標(biāo)定位(下標(biāo)從0開始計(jì)數(shù))select.deselectByIndex(0);// 2.根據(jù)value值定位select.deselectByValue("???");}
}
4.彈窗處理: 針對alert
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;public class Main9 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接driver.get("???");// 點(diǎn)擊彈窗driver.findElement(By.cssSelector("button")).click();// 取消彈窗driver.switchTo().alert().dismiss();// 點(diǎn)擊彈窗driver.findElement(By.cssSelector("button")).click();// 輸入內(nèi)容driver.switchTo().alert().sendKeys("張三");// 點(diǎn)擊確認(rèn)driver.switchTo().alert().accept();}
}
5.上傳文件
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;public class Main10 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接driver.get("???");// 上傳文件driver.findElement(By.cssSelector("???")).sendKeys("此處填寫文件路徑");}
}
十三.補(bǔ)充
1.關(guān)閉瀏覽器
a)driver.quit();
退出瀏覽器,清空緩存(如cookie).
b)driver.close();
關(guān)閉當(dāng)前正在操作的頁面(不是最新的頁面,要看當(dāng)前正在操作的頁面)
c)代碼
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import static java.lang.Thread.sleep;public class Main11 {public static void main(String[] args) throws InterruptedException {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();sleep(1500);//driver.close();driver.quit();}
}
2.切換窗口
a)driver.getWindowHandle();
獲取頁面句柄,不是最新的頁面,是當(dāng)前正在操作的頁面.
b)Set<String> windowHandles = driver.getWindowHandles();
獲取所有頁面的局部,最后一個就是最新頁面的句柄.
c)代碼:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import java.util.Set;public class Main12 {public static void main(String[] args) {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");// 點(diǎn)擊新的頁面driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();System.out.println(driver.getWindowHandle());String handle = null;Set<String> windowHandles = driver.getWindowHandles();for (String str : windowHandles) {handle = str;System.out.println(str);}}
}
運(yùn)行結(jié)果:?
3.截圖
a)去maven中央倉庫找common-io依賴(Apache Commons IO)
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>
b) File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
? ? FileUtils.copyFile(screenshotAs, new File("D://picture/123.png"));
c)代碼
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;import java.io.File;
import java.io.IOException;import static java.lang.Thread.sleep;public class Main13 {public static void main(String[] args) throws IOException, InterruptedException {ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");// 創(chuàng)建驅(qū)動WebDriver driver = new ChromeDriver(options);// 連接百度driver.get("https://www.baidu.com/");driver.findElement(By.cssSelector("#kw")).sendKeys("別克君越艾維亞");driver.findElement(By.cssSelector("#su")).click();sleep(1500);File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(screenshotAs, new File("D://picture/123.png"));}
}