selenium環境搭建
- 通過maven安裝
在idea中創建maven專案
打開pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvn.demo</groupId>
<artifactId>MyMvnPro</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
</project>
瀏覽器驅動
- 下載瀏覽器驅動
- Chrome瀏覽器驅動:chromedrivertaobao備用地址
- IE瀏覽器驅動:IEDriverServer
- Edge瀏覽器驅動:MicrosoftWebDriver
- 瀏覽器設定驅動
- 手動重建一個放置驅動的檔案夾例如:chromedriver
- 然后在電腦-屬性-系統設定-高級-變數里面配置環境變數
- 驗證瀏覽器驅動
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
……
WebDriver driver = new ChromeDriver(); //Chrome瀏覽器
WebDriver driver = new FirefoxDriver(); //Firefox瀏覽器
WebDriver driver = new EdgeDriver(); //Edge瀏覽器
WebDriver driver = new InternetExplorerDriver(); // Internet Explorer瀏覽器
WebDriver driver = new OperaDriver(); //Opera瀏覽器
WebDriver driver = new PhantomJSDriver(); //PhantomJS
……
selenium元素定位
- selenium定位方法
selenium提供了八種定位方法
- id
- name
- class name
- tag name
- link text
- partial link text
- xpath
- css selector
這八種分別在java selenium中對應
- findElement(By.id())
- findElement(By.name())
- findElement(By.className())
- findElement(By.tagName())
- findElement(By.linkText())
- findElement(By.partialLinkText())
- findElement(By.xpath())
- findElement(By.cssSelector())
- 定位方法的用法
<input id="kw" class="s_ipt" name="wd" >
- 通過id定位
driver.findElement(By.id("kw")) - 通過name定位
driver.findElement(By.name("wd")) - 通過class name定位
driver.findElement(By.className("s_ipt")) - 通過tag name定位
driver.findElement(By.tagName("input")) - 通過xpath定位
driver.findElement(By.xpath("//*[@id='kw']"))
driver.findElement(By.xpath("//*[@name='wd']"))
driver.findElement(By.xpath("//input[@class='s_ipt']"))
driver.findElement(By.xpath("/html/body/form/span/input"))
driver.findElement(By.xpath("//span[@class='soutu-btn']/input"))
driver.findElement(By.xpath("//form[@id='form']/span/input"))
driver.findElement(By.xpath("//input[@id='kw' and @name='wd']"))
- 通過css定位
driver.findElement(By.cssSelector("#kw")
driver.findElement(By.cssSelector("[name=wd]")
driver.findElement(By.cssSelector(".s_ipt")
driver.findElement(By.cssSelector("html > body > form > span > input")
driver.findElement(By.cssSelector("span.soutu-btn> input#kw")
driver.findElement(By.cssSelector("form#form > span > input")
webDriver常用方法
- webDriver常用方法
- clear() 清除文本,
方法用于清除文本輸入框中的內容 - click() 單擊元素,
可以用來單擊一個元素,但是這個元素是可以被單擊的 - submit() 方法用于提交表單,
- getSize() 回傳元素尺寸,
- getText()回傳元素文本,
- getAttribute(name) 獲得屬性值,
- isDisplayed() 設定該元素是否用戶可見,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/375860.html
標籤:其他
下一篇:Postman的pm物件(一)
