Maven+selenium入門教程

一、環(huán)境搭建

官網(wǎng)需要下載selenium對應(yīng)的jar包,谷歌瀏覽器及對應(yīng)的驅(qū)動

官網(wǎng)下載地址:http://www.seleniumhq.org/download/

瀏覽器驅(qū)動:https://www.npmjs.com/package/selenium-webdriver

2、jdk環(huán)境變量

jdK下載地址:https://www.oracle.com/technetwork/java/javaee/downloads/index.html

環(huán)境變量配置參考:https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html

3、maven中pom文件配置需要的jar包支持

如果是java Project,直接導(dǎo)入下載的jar包就行了

二、Selenium WebDriver運行原理

使用eclipse打開WebDriver.class文件會發(fā)現(xiàn)里面支持許多方法的調(diào)用

三、常見API用法

1.1打開瀏覽器

想要瀏覽器完成界面元素的操作,首頁要通過WebDriver打開瀏覽器,WebDriver為上面下載的

System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\chromedriver.exe");

webdriver=new ChromeDriver();

/*設(shè)置隱私等待讓瀏覽器正常運行*/

webdriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

打開firefox瀏覽器:

? ? ? ?     WebDriver driver = new FirefoxDriver();?

打開IE瀏覽器

? ? ? ?     WebDriver driver = new InternetExplorerDriver ();

打開chrome瀏覽器

       WebDriverdriver = new ChromeDriver();

1.2.1 ?最大化瀏覽器

  WebDriver driver = new FirefoxDriver();

  driver.manage().window().maximize();

1.2.3 關(guān)閉瀏覽器

WebDriver driver = new FirefoxDriver();

  driver.close();

  driver.quit();

1.3??打開測試頁面

driver.get("http://www.google.com");

1.4 ?頁面元素定位

Webdriver提供下面兩種方法來定位頁面元素,參數(shù)是By對像,最常用是By.id和By.name查找。

findElement   定位某個元素,如果沒有找到元素會拋出異常:NoSuchElementException

findElements ? ? 定位一組元素

?例如需要定位如下元素:

  <input class="input_class" type="text" name="passwd" id="passwd-id" />?

By.id:

      WebElement element = driver.findElement(By.id("passwd-id"));

By.name:

      WebElement element = driver.findElement(By.name("passwd"));

By.xpath:

      WebElement element =driver.findElement(By.xpath("http://input[@id='passwd-id']"));?

By.className

      WebElement element = driver.findElement(By.className("input_class"));

By.cssSelector

      WebElement element = driver.findElement(By.cssSelector(".input_class"));

By.linkText:

      //通俗點就是精確查詢

      WebDriver driver = new FirefoxDriver();

      driver.get("http://www.baidu.com/");?

      WebElement element = driver.findElement(By.linkText("百科"));

By.partialLinkText:

      //這個方法就是模糊查詢

      WebDriver driver = new FirefoxDriver();

      driver.get("http://www.baidu.com/");?

      WebElement element = driver.findElement(By.partialLinkText("hao"));

By.tagName:

      WebDriver driver = new FirefoxDriver();

      driver.get("http://www.baidu.com/");

      String test= driver.findElement(By.tagName("form")).getAttribute("name");

      System.out.println(test);?

1.5??如何對頁面元素進行操作

1.5.1 輸入框(text field or textarea)

WebElement element = driver.findElement(By.id("passwd-id"));

element.sendKeys(“test”);//在輸入框中輸入內(nèi)容:

element.clear();     ??//將輸入框清空

element.getText();   ??//獲取輸入框的文本內(nèi)容:?

1.5.2下拉選擇框(Select)

Select select = new Select(driver.findElement(By.id("select"))); ?

select.selectByVisibleText(“A”);

select.selectByValue(“1”);?

select.deselectAll();

select.deselectByValue(“1”);

select.deselectByVisibleText(“A”);

select.getAllSelectedOptions();

select.getFirstSelectedOption();?

1.5.3單選項(Radio Button)

WebElement radio=driver.findElement(By.id("BookMode"));

radio.click();     ? //選擇某個單選項

radio.clear();     ?//清空某個單選項

radio.isSelected();  //判斷某個單選項是否已經(jīng)被選擇

1.5.4多選項(checkbox)

WebElement checkbox = driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear();

checkbox.isSelected();

checkbox.isEnabled();

1.5.5按鈕(button)

WebElement btn= driver.findElement(By.id("save"));

btn.click();      //點擊按鈕

btn.isEnabled ();  //判斷按鈕是否enable

1.5.7彈出對話框(Popup dialogs)

Alert alert = driver.switchTo().alert();

alert.accept();  //確定

alert.dismiss();  //取消

alert.getText(); //獲取文本

1.5.8表單(Form)

  Form中的元素的操作和其它的元素操作一樣,對元素操作完成后對表單的提交可以:

  WebElement approve = driver.findElement(By.id("approve"));

  approve.click();

  approve.submit();//只適合于表單的提交

1.5.9截圖保存圖片文件

1.6??WindowHandle和 Frames之間的切換

例如從登陸頁面跳轉(zhuǎn)到index頁面,此時WindowHandle就會發(fā)生變化,通過封裝webdriverde的getWindowsHandle方法就可解決該問題

再就是Frames的切換,后臺系統(tǒng)用的比較多

源碼就是這樣的,套兩層frameset,里面放frame

<html>

<frameset rows="*" cols="150,*">

? <frame src="/example/html/frame_a.html" id="left">

? <frameset rows="50,*">

? <frame src="/example/html/frame_b.html" id="top">

? <frame src="/example/html/frame_c.html" id="main">

</frameset>

</frameset>

</html>

當進入到這個頁面時,driver停留在最大的frameset上,如果需要操作left頁面就需要使用

webdriver.switchTo().frame(left);

如果left頁面操作完需要操作top或者main的頁面時,需要先從frame返回到frameset再

到子frameset的frame中,思路如下

webdriver.switch_to_parent_frame();

webdriver.switchTo().frame(main);

從main返回到left時就需要

webdriver.switch_to_parent_frame();

webdriver.switch_to_parent_frame();

webdriver.switchTo().frame(main);

1.7??表格(Table)

先找到table,再找到對應(yīng)列或者行,賦值給list,for循環(huán)取出值

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容