`
dannyhz
  • 浏览: 371714 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

selenium 2 环境的搭建

阅读更多
今天公司用到selenium 2, 所以去下载了下, 配起环境如下
1. 从http://www.seleniumhq.org/download/
  a. 下载The Internet Explorer Driver Server , 我的机器是64的win7 ,所以下载了64 bit windows IE,
  b. 下载 SeleniumClient& WebDriverLanguageBindings, version Java 2.45.0 的版本

2. 把下载的 IE Driver Server.exe 放到Win 7 的环境目录下,比如Windows 文件夹,或者自己在环境变量里加入一个环境变量,然后把这个exe文件放进去。

3. 新建一个Java 工程, 放入下载的包的所有Jar包,如里面的libs包里的所有第三方依赖包, 还有它自己的包 selenium-java-2.45.0.jar

环境配制好了,然后写一个测试类

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface,
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
       
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
       
        //Close the browser
        driver.quit();
    }
}

能看到访问了 google首页面,并且输入 cheer ,并且按了 submit按钮 , 说明环境配制成功。

这其中还遇到个问题,就是IE的设置, IE如果设置了 protected mode 就会报出异常, save all setting as protected mode (yes or no) , 在IE里面 internet options --> security --> Enable Protected Mode 这个 要所有的 Zone (Internet Local Intract Trusted sited restricted sites) 都 设成相同的 Y or N, 这边全部都 不勾上这个选项 。


这些都准备好,应该就没问题了!


  • 大小: 173.7 KB
  • 大小: 108.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics