zoukankan      html  css  js  c++  java
  • 在selenium2.0中使用selenium1.0的API

    Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium RC API那么方便。

    Selenium2.0提供了使用Selenium RC API的方法:

        // 我用火狐浏览器作为例子

        WebDriver driver = new FirefoxDriver(); 

        String baseUrl ="http://www.google.com"; 

        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

        // 执行selenium命令

        selenium.open("http://www.google.com");

        selenium.type("name=q", "cheese");

        selenium.click("name=btnG");

        WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

        selenium.stop();

    分别使用WebDriver API和SeleniumRC API写了一个Login的脚本,很明显,后者的操作更加简单明了。

    (1)WebDriver API写的Login脚本:

        public void login() {

            driver.switchTo().defaultContent();

            driver.switchTo().frame("mainFrame");

            WebElement eUsername= waitFindElement(By.id("username"));

            eUsername.sendKeys(manager@ericsson.com);

            WebElement ePassword= waitFindElement(By.id("password"));

            ePassword.sendKeys(manager);

            WebElementeLoginButton = waitFindElement(By.id("loginButton"));

           eLoginButton.click();

        }

        

    (2)SeleniumRC API写的Login脚本:

        public void login() {

            selenium.selectFrame("relative=top");

            selenium.selectFrame("mainFrame");

            selenium.type("username","manager@ericsson.com");

            selenium.type("password","manager");

            selenium.click("loginButton");

    }

  • 相关阅读:
    url 百分号解密
    16.UA池和代理池
    15.scrapy框架之日志等级、请求传参、提高scrapy框架的爬取效率
    14. scrip框架之5大核心组件和post请求
    13.scrapy 框架之递归解析(手动发送请求),
    12. scrapy 框架持续化存储
    11.scrapy框架简介和基础应用
    10. 移动端数据爬取
    09.python之网络爬虫之selenium、phantomJs和谷歌无头浏览器的自动化操作
    08 python之网络爬虫之乱码问题
  • 原文地址:https://www.cnblogs.com/liu-ke/p/4330203.html
Copyright © 2011-2022 走看看