转:Selenium Webdriver使用技巧(一)
2016-03-29 00:03
190 查看
转自:http://blog.csdn.net/bwgang/article/details/7902526
1.打开一个测试浏览器
对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。
Java代码
import java.io.File;
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");
}
}
2.打开1个具体的url
打开一个浏览器后,我们需要跳转到特定的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);
}
}
3.如何关闭浏览器
测试完成后,需要关闭浏览器
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();
}
}
4.如何返回当前页面的url和title
有时候我们需要返回当前页面的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);
4000
//得到title
Stringtitle = driver.getTitle();
//得到当前页面url
StringcurrentUrl = driver.getCurrentUrl();
//输出title和currenturl
System.out.println(title+"\n"+currentUrl);
}
}
5.其他方法
getWindowHandle() 返回当前的浏览器的窗口句柄
getWindowHandles() 返回当前的浏览器的所有窗口句柄
getPageSource() 返回当前页面的源码
从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承RemoteWebDriver。
1.打开一个测试浏览器
对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。
Java代码
import java.io.File;
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");
}
}
2.打开1个具体的url
打开一个浏览器后,我们需要跳转到特定的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);
}
}
3.如何关闭浏览器
测试完成后,需要关闭浏览器
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();
}
}
4.如何返回当前页面的url和title
有时候我们需要返回当前页面的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);
4000
//得到title
Stringtitle = driver.getTitle();
//得到当前页面url
StringcurrentUrl = driver.getCurrentUrl();
//输出title和currenturl
System.out.println(title+"\n"+currentUrl);
}
}
5.其他方法
getWindowHandle() 返回当前的浏览器的窗口句柄
getWindowHandles() 返回当前的浏览器的所有窗口句柄
getPageSource() 返回当前页面的源码
从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承RemoteWebDriver。
相关文章推荐
- 针对Ruby的Selenium WebDriver安装指南
- Python中使用 Selenium 实现网页截图实例
- selenium grid2 初探
- UI自动化测试框架之Selenium关键字驱动
- selenium使用chrome时,报错ignore certificate errors
- 利用 Selenium 自动化 web 测试
- Xampp安装PHPUnit
- selenium学习3 selenium for IE8+WIN7
- 用C#.NET 与Webdriver写的抓取网页信息的小工具
- [selenium + eclipse + testng]Build environment
- selenium-ide安装与使用
- selenium学习笔记(1)--webdriver中时间控件的使用
- 用Selenium RC插件测试远程应用
- Selenium-Grid 实现案列
- [转]TestNG结合Selenium Grid
- 【转】Selenium Grid 个人理解
- webdriver selenium xpath
- selenium 学习笔记
- Selenium WebDriver入门——API 常用部分(适用于IE浏览器)