zoukankan      html  css  js  c++  java
  • Selenium环境搭建

    一、开发环境:

    1、JDK1.6
    2、Eclipse
    3、Selenium:selenium-java-2.39.0.zip,

    解压selenium-java包,这个包里面包含四部分,如下图:(也可搜索selenium-java-2.42.2.zip等版本下载)

     

    二、新建一个Java Project:

    1、然后把上面解压出来的文件拷到新建的project目录下,目录结构如下图:

      

    2、添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JAR

      把libs文件夹下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs(快捷键Ctrl+选择的多个文件)

      

    3、添加完之后目录结构如下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包:

      

    4、关联webdriver的源码:

      


    至此,环境工作准备就绪,下面来写一个简单的小例子。

    三、在src下面新建测试类,如下图:



    代码如下,主要是打开百度,然后在搜索框输入过敏,点击搜索按钮,关闭浏览器。

    package com.selenium;

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.chrome.*;

      public class TestHelloWorld {

        public static void main(String[] args) {  

            //如果火狐浏览器没有默认安装在C盘,需要制定其路径

           //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 

         System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe");

         WebDriver driver = new ChromeDriver();

            driver.get("http://www.baidu.com/");

            driver.manage().window().maximize();

            WebElement txtbox = driver.findElement(By.name("wd"));

            txtbox.sendKeys("过敏"); 

           WebElement btn = driver.findElement(By.id("su"));

            btn.click();

        }

     }

    然后直接右键-->Run As-->Java Application就可以看到效果了

  • 相关阅读:
    mysql设置不区分大小写
    java.lang.StackOverflowError: null
    与或非
    mysql自动备份
    Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 不支持“variant”数据类型。
    MySQL主从复制 + Mycat实现读写分离
    Swing做的非阻塞式仿飞秋聊天程序
    Hudson + SVN + Maven 持续集成实现自动化编译、打包、部署(over SSH 和 Deploy war/ear to a container 两种部署方式)
    CMake安装(源码方式)
    多线程使用实例
  • 原文地址:https://www.cnblogs.com/ydnice/p/5784188.html
Copyright © 2011-2022 走看看