WebDriver拾级而上·之二 浏览器操作

本文深入探讨了如何使用WebDriver启动Firefox、IE和Chrome浏览器,包括指定路径打开浏览器的方法及页面跳转、关闭浏览器、获取页面信息等核心操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.启动浏览器
A.firefox
//打开默认路径的firefox(路径指的是firefox<wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">的安装路径)</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">WebDriver diver = new FirefoxDriver();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//打开指定路径的firefox,方法1</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">WebDriver dr = new FirefoxDriver();</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//打开指定路径的firefox,方法2</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe"); </span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary); </span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">WebDriver driver1 = new FirefoxDriver(firefoxbin,null);</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">B.ie</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">//打开ie</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; line-height:21px">WebDriver ie_driver = new InternetExplorerDriver();</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

C.chrome
因为Chrome Driver是Chromium 项目自己支持和维护的,所以你必需另外下载chromedriver.exe,放在目录下C:\WINDOWS\system32
下载地址: http://vdisk.weibo.com/s/5Bf3L
//打开chrome
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>WebDriver driver = new ChromeDriver();

另一种启动chrome 的方法
wiki介绍:http://code.google.com/p/selenium/wiki/ChromeDriver
//打开chrome<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.setProperty("webdriver.chrome.bin",<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>C:\\Documents and Settings\\fy\\Local Settings"<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>+"\\Application Data\\Google\\Chrome\\Application\\chrome.exe");<wbr><br><br> Chromium介绍:http://code.google.com/p/chromium/<br><br><br><br> 2.页面跳转url<br> String url = "http://www.baidu.com";<br> WebDriver driver = new FirefoxDriver();<br><br> A//用get方法<br><wbr><wbr><wbr><wbr><wbr><wbr>driver.get(url);<br><br> B//用navigate方法,然后再调用to方法,chrome不支持这种方法<br><wbr><wbr><wbr><wbr><wbr><wbr>driver.navigate().to(url);<br><br><br> 3.关闭浏览器</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//quit<wbr>关闭所有页面 <wbr>close<wbr>关闭本次执行打开的页面<wbr><br> A.//用quit方法<br><wbr><wbr><wbr><wbr><wbr><wbr>driver.quit();<br> B.//用close方法<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>driver.close();<br><br> 4.获取页面信息<br> //得到title<br> String title = driver.getTitle();<br> //得到当前页面url<br> String currentUrl = driver.getCurrentUrl();<br><br> getWindowHandle()<wbr><wbr><wbr>返回当前的浏览器的窗口句柄<br> getWindowHandles()<wbr>返回当前的浏览器的所有窗口句柄<br> getPageSource()<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>返回当前页面的源码<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
//String s=driver.getPageSource();s=s.substring(s.indexOf("{"), s.indexOf("}"));
//System.out.println("当前页面的源码:"+s);


5.总结
操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。
源代码这些方法都是在
org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承RemoteWebDriver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值