selenium3学习笔记


Selenium3学习笔记

本文所述内容在Windows系统下实践完成,涉及的软件版本信息如下:

软件

版本

位数

Java

1.8.0_152

64位

Eclipse

eclipse-jee-oxygen-1a-win32-x86_64 (4.7.1a)

64位

IntelliJ IDEA

 

 

Maven

3.5.2

 

Selenium

3.8.1

 

Junit

 

 

TestNG

 

 

AutoIt

 

 

Sikuli

 

 

Firefox

55

32位

注:部分软件版本间兼容性十分重要,下文中会提及,使用其他版本时请注意。

safaridriver

  • 通过ms官方的webdriverserver支持Edge浏览器。

  • 只支持ie 9.0版本以上。

  • 通过Mozilla官方的geckodriver来支持firefox。

  • 尽管firefox driver非常的稳定高效,但firefoxdriver毕竟是google实现的。geckodriver的出现是必然的结果,因为最新版本的firefox换引擎了,老的firefox driver应该是不支持新引擎的。

    firefox driver究竟是什么?为什么你一次都没下载运行过?

    这是因为firefox driver包含在selenium各语言版本的分发包里,使用webdriver启动firefox的时候,webdriver会为firefox安装firefox driver扩展。

     

    http://maven.apache.org/

    Selenium

    http://selenium-release.storage.googleapis.com/index.html

    Firefox

    http://ftp.mozilla.org/pub/firefox/releases/

    Gecko driver

    https://github.com/mozilla/geckodriver/releases

    Chrome driver

    http://chromedriver.storage.googleapis.com/index.html

    https://npm.taobao.org/mirrors/chromedriver

    IE driver

    http://selenium-release.storage.googleapis.com/index.html

    Edge driver

    https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

    Opera driver

    https://github.com/operasoftware/operachromiumdriver/releases

    Safari driver

    https://developer.apple.com/library/content/releasenotes/General/WhatsNewInSafari/Introduction/Introduction.html

    PhantomJS driver

    http://phantomjs.org/

    HtmlUnit Driver

    Lower-cased

    As they appear in the HTML

    Yes

    Internet Explorer Driver

    Lower-cased

    As they appear in the HTML

    No

    Firefox Driver

    Case insensitive

    As they appear in the HTML

    Yes

    比如如下页面:

    查找语句为:

    List inputs = driver.findElements(By.xpath("//input"));

    匹配结果为:

    XPath expression

    HtmlUnit Driver

    Firefox Driver

    Internet Explorer Driver

    //input

    1 (“example”)

    2

    2

    //INPUT

    0

    2

    0

    注意:有时元素不必声明某些有默认值的属性,比如input元素的type属性可以缺省,缺省值为text。但是在WebDriver中使用XPath时,你不能期望能够匹配未声明的默认属性。

    • By Class Name

    通过class属性查找。实际使用中相同的class属性一般对应多个元素,所以一般会查找到多个元素,然后取用第一个元素。

    List cheeses = driver.findElements(By.className("cheese"));

    • By Link Text

    通过超链接文本查找超链接。

    WebElement cheese = driver.findElement(By.linkText("cheese"));

    • By Partial Link Text

    通过部分超链接文本查找超链接。

    WebElement cheese = driver.findElement(By.partialLinkText("cheese"));

    • By Css Selector

    通过Css Selector查找。如果浏览器默认原生支持css查询,可参阅w3c css selectors;否则Sizzle被使用。IE6,7和FF3.0都使用Sizzle作为css查询引擎。

    注意并非所有的浏览器都是等效的,有的css在一个中起作用也许在另一个中不会。

    WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));

    • By TagName

    通过元素标签名查找。

    WebElement frame = driver.findElement(By.tagName("iframe"));

    另外,也可以使用窗口句柄,这可能需要迭代所有窗口句柄来获取某个窗口的句柄了:

    for (String handle : driver.getWindowHandles()) {

       driver.switchTo().window(handle);

    }

    在框架间切换也是类似的:

    driver.switchTo().frame("frameName");

     

    WebElement xf = driver.findElement(by);

    driver.switchTo().frame(xf);

     

    driver.switchTo().defaultContent();

    3.3  高级操作

    https://www.w3.org/TR/webdriver/

    https://www.w3.org/testing/Activity

    https://github.com/w3c/webdriver