七、浏览器操作

以firefox为例,其他浏览器一样
[b][size=large]1.启动浏览器[/size][/b]
详见:
[url=http://sariyalee.iteye.com/blog/1688254]HtmlUnitDriver[/url]
[url=http://sariyalee.iteye.com/blog/1697540]FirefDriver和InternetExplorerDriver[/url]
[url=http://sariyalee.iteye.com/blog/1697825]ChromeDriver[/url]
[b][size=large]2.访问url[/size][/b]
方法1:使用get

package selenium.test.googleSearch;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
}

}


方法2:

package selenium.test.googleSearch;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.navigate().to("http://www.baidu.com/");
}

}


[b][size=large]3.关闭浏览器[/size][/b]

package selenium.test.googleSearch;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
public class BaiduFirefoxDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
//页面跳转
driver.get("http://www.baidu.com/");
//关闭浏览器
driver.quit();
}

}

[b][size=large]4.获得页面元素[/size][/b]
//得到title
String title = driver.getTitle();

//得到当前页面url
String currentUrl = driver.getCurrentUrl();


getWindowHandle()   // 返回当前的浏览器的窗口句柄
getWindowHandles() // 返回当前的浏览器的所有窗口句柄
getPageSource() // 返回当前页面的源码

详细用法在[url=http://sariyalee.iteye.com/blog/1688254]这里[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值