selenium学习

selenium环境搭建

  1. 通过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>

浏览器驱动

  1. 下载浏览器驱动
    • Chrome浏览器驱动:chromedrivertaobao备用地址
    • IE浏览器驱动:IEDriverServer
    • Edge浏览器驱动:MicrosoftWebDriver
  2. 浏览器设置驱动
    • 手动重建一个放置驱动的文件夹例如:chromedriver
    • 然后在电脑-属性-系统设置-高级-变量里面配置环境变量
  3. 验证浏览器驱动
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元素定位

  1. 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())
  1. 定位方法的用法
<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常用方法

  1. webDriver常用方法
  • clear() 清除文本。
    方法用于清除文本输入框中的内容
  • click() 单击元素。
    可以用来单击一个元素,但是这个元素是可以被单击的
  • submit() 方法用于提交表单。
  • getSize() 返回元素尺寸。
  • getText()返回元素文本。
  • getAttribute(name) 获得属性值。
  • isDisplayed() 设置该元素是否用户可见。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>