转:Selenium Webdriver使用技巧(一)
上一篇 / 下一篇 2014-05-05 11:44:04 / 个人分类:Selenium
转自:http://blog.csdn.net/bwgang/article/details/7902526
对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。
Java代码
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxBinary;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;
public class OpenBrowsers {
public static void
main(String[] args) {
//打开默认路径的firefox
WebDriver diver = new FirefoxDriver();
//打开指定路径的firefox,方法1
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\MozillaFirefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
//打开指定路径的firefox,方法2
File pathToFirefoxBinary = newFile("D:\\Program Files\\Mozilla
Firefox\\firefox.exe");
FirefoxBinary firefoxbin = newFirefoxBinary(pathToFirefoxBinary);
WebDriver driver1 = newFirefoxDriver(firefoxbin,null);
//打开ie
WebDriver ie_driver = new InternetExplorerDriver();
//打开chrome
System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
System.setProperty("webdriver.chrome.bin",
"C:\\Documents
and Settings\\gongjf\\Local Settings"
+"\\ApplicationData\\Google\\Chrome\\Application\\chrome.exe");
}
}
打开一个浏览器后,我们需要跳转到特定的url下,看下面代码:
Java代码
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class OpenUrl {
publicstatic void
main(String []args){
Stringurl = "http://www.51.com";
WebDriverdriver = new FirefoxDriver();
//用get方法
driver.get(url);
//用navigate方法,然后再调用to方法
driver.navigate().to(url);
}
}
测试完成后,需要关闭浏览器
Java代码
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class CloseBrowser {
publicstatic void
main(String []args){
Stringurl = "http://www.51.com";
WebDriverdriver = new FirefoxDriver();
driver.get(url);
//用quit方法
driver.quit();
//用close方法
driver.close();
}
}
有时候我们需要返回当前页面的url或者title做一些验证性的操作等。代码如下:
Java代码
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class GetUrlAndTitle {
publicstatic void
main(String []args){
Stringurl = "http://www.google.com";
WebDriverdriver = new FirefoxDriver();
driver.get(url);
//得到title
Stringtitle = driver.getTitle();
//得到当前页面url
StringcurrentUrl = driver.getCurrentUrl();
//输出title和currenturl
System.out.println(title+"\n"+currentUrl);
}
}
getWindowHandle() 返回当前的浏览器的窗口句柄
getWindowHandles() 返回当前的浏览器的所有窗口句柄
getPageSource() 返回当前页面的源码
从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承RemoteWebDriver。
selenium-webdriver提供了强大的元素定位方法,支持以下三种方法。
单个对象的定位方法
多个对象的定位方法
层级定位
定位单个元素
在定位单个元素时,selenium-webdriver提示了如下一些方法对元素进行定位。
By.className(className))
By.cssSelector(selector)
By.id(id)
By.linkText(linkText)
By.name(name)
By.partialLinkText(linkText)
By.tagName(name)
By.xpath(xpathExpression)
注意:selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素List,如果不存在符合条件的就返回一个空的list。
当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。
下面的例子定位了51.com首页上class为"username"的li。
Java代码
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
public class ByClassName {
public static void main(String[] args) {
WebDriver driver =
new FirefoxDriver();
driver.get("http://www.51.com");
WebElement element
=driver.findElement(By.className("username"));
System.out.println(element.getTagName());
}
}
输出结果:
Java代码
Li
51.com首页的帐号输入框的html代码如下:
Java代码
<input id="passport_51_user"type="text"
value="" tabindex="1" title="用户名/彩虹号/邮箱"
name="passport_51_user">
在下面的例子中用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。
Java代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class ByUserId {
/**
* @param args
*/
publicstatic void
main(String[] args) {
//TODO Auto-generated method stub
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
WebElementelement = dr.findElement(By.id("passport_51_user"));
System.out.println(element.getAttribute("title"));
}
}
输出结果:
Java代码
用户名/彩虹号/邮箱
51.com首页的帐号输入框的html代码如下:
Java代码
<input id="passport_51_user"type="text"
value="" tabindex="1" title="用户名/彩虹号/邮箱"
name="passport_51_user">
使用name定位
Java代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class ByUserId {
/**
* @param args
*/
publicstatic void
main(String[] args) {
//TODO Auto-generated method stub
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
WebElemente =
dr.findElement(By.name("passport_51_user"));
System.out.println(element.getAttribute("title"));
}
}
输出结果:
Java代码
用户名/彩虹号/邮箱
51.com首页的帐号输入框的html代码如下:
Java代码
<input id="passport_51_user"type="text"
value="" tabindex="1" title="用户名/彩虹号/邮箱"
name="passport_51_user">
使用css定位
Java代码
WebElement e1 =dr.findElement(By.cssSelector("#passport_51_user"));
51.com首页的帐号输入框的html代码如下:
Java代码
<input id="passport_51_user"type="text"
value="" tabindex="1" title="用户名/彩虹号/邮箱"
name="passport_51_user">
通过xpath查找:
Java代码
WebElement element=driver.findElement(By.xpath("//input[@id='
passport_51_user ']"));
在定位link元素的时候,可以使用link和link_text属性;
另外还可以使用tag_name属性定位任意元素;
上面提到findElements()方法可以返回一个符合条件的元素List组。看下面例子。
Java代码
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElementsStudy {
/**
* @author
gongjf
*/
publicstatic void
main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.51.com");
//定位到所有<input>标签的元素,然后输出他们的id
List<WebElement>element =
driver.findElements(By.tagName("input"));
for(WebElement e : element){
System.out.println(e.getAttribute("id"));
}
driver.quit();
}
}
输出结果:
Java代码
passport_cookie_login
gourl
passport_login_from
passport_51_user
passport_51_password
passport_qq_login_2
btn_reg
passport_51_ishidden
passport_auto_login
上面的代码返回页面上所有input对象
TAG:
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 430406
- 日志数: 133
- 建立时间: 2013-07-02
- 更新时间: 2014-09-17