selenium webdriver学习 02 – 对浏览器的简单操作

玩技站长 Auto测试评论685字数 3044阅读模式
打开一个测试浏览器
对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。但要注意的是,因为Chrome Driver是Chromium 项目自己支持和维护的,所以你必需另外下载安装Chrome Driver,详细介绍查下他们的wiki
Java代码

import java.io.File; 
 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxBinary; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
 
 
public class OpenBrowsers { 
 
     
    public staticvoid main(String[] args) { 
        //打开默认路径的firefox 
        WebDriver diver = new FirefoxDriver(); 
         
        //打开指定路径的firefox,方法1 
        System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
        WebDriver dr = new FirefoxDriver(); 
         
        //打开指定路径的firefox,方法2 
        File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);   
        WebDriver driver1 = new FirefoxDriver(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" 
                                             +"\\Application Data\\Google\\Chrome\\Application\\chrome.exe");        
         
    } 
 
}
打开指定路经ie和chrome方法和ff一样。
打开1个具体的url
打开一个浏览器后,我们需要跳转到特定的url下,看下面代码:
Java代码

文章源自玩技e族-https://www.playezu.com/10110.html

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class OpenUrl { 
    public staticvoid main(String []args){ 
        String url = "http://www.51.com"; 
        WebDriver driver = new FirefoxDriver(); 
         
        //用get方法 
        driver.get(url); 
         
        //用navigate方法,然后再调用to方法 
        driver.navigate().to(url); 
    } 
}
如何关闭浏览器
测试完成后,需要关闭浏览器
Java代码

文章源自玩技e族-https://www.playezu.com/10110.html

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class CloseBrowser { 
    public staticvoid main(String []args){ 
        String url = "http://www.51.com"; 
        WebDriver driver = new FirefoxDriver(); 
         
        driver.get(url); 
         
        //用quit方法 
        driver.quit(); 
         
        //用close方法   
        driver.close(); 
        } 
}
如何返回当前页面的url和title
有时候我们需要返回当前页面的url或者title做一些验证性的操作等。代码如下:
Java代码

文章源自玩技e族-https://www.playezu.com/10110.html

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class GetUrlAndTitle { 
    public staticvoid main(String []args){ 
        String url = "http://www.51.com"; 
        WebDriver driver = new FirefoxDriver(); 
         
        driver.get(url); 
         
                //得到title 
        String title = driver.getTitle(); 
 
                //得到当前页面url 
        String currentUrl = driver.getCurrentUrl(); 
         
                //输出title和currenturl 
        System.out.println(title+"\n"+currentUrl); 
         
        } 
}
其他方法
  • getWindowHandle()    返回当前的浏览器的窗口句柄
  • getWindowHandles()  返回当前的浏览器的所有窗口句柄
  • getPageSource()         返回当前页面的源码
小结
从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都 是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承 RemoteWebDriver。

文章源自玩技e族-https://www.playezu.com/10110.html 文章源自玩技e族-https://www.playezu.com/10110.html

玩技站长微信
添加好友自动发送入群邀请
weinxin
rainbow-shownow
玩技官方公众号
官方微信公众号
weinxin
PLAYEZU
 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/10110.html
    转载说明: 点我前往阅读>>>
匿名

发表评论

匿名网友
确定