每天早上醒来,看见你和阳光都在,这便是我要的未来。

Selenium3+java环境搭建

上一篇 / 下一篇  2017-03-31 09:16:29 / 个人分类:Selenium

尝试搭建了个Selenium3的环境
需要准备工具安装包如下:
eclipse、
JDK、
chromedriver.exe(我用的chroome浏览器)、
selenium-server-standalone-3.3.1.jar
先不说Selenium3相较之前版本有什么不同,
Selenium3需要1.8的JDK,而JDK1.8需要高版本的eclipse,如果能下载请用最新版的。
因为对版本的不明确,走了不少弯路,最开始捡便宜copy了个myeclipse10,它支持的最高版本JDK是1.6,编译各种报错,各种换JDK1.7的1.8的都尝试过,还是报错,最后才知道是myeclipse版本太低。
还需要关注chrome浏览器的版本,我用的57的,反正往高了用。

以下流程也都是从网上借鉴来实践操作过的,流程很简单,准备好需要的安装包和相关软件就可以开始了。
step1.安装JDK,配置环境变量
step2.安装eclipse
step3.建javaproject,建class
step4.导包 
工程名右键--Properties---Java Build Path---Libraries---Add ExternalJARs---选择下载的selenium-server-standalone-3.3.1.jar
step5.测试配置是否正确
代码如下:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTestChrome {

    public static void main(String [] args){
   
   //通过以下方式加载ChromeDriver
     System.setProperty("webdriver.chrome.driver","C:/Users/liuyu/AppData/Local/Google/Chrome/Application/chromedriver.exe");
        //通过driver打开chrome主页并访问百度
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        //等待10s让网页完全加载
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        if(driver.findElement(By.id("kw")).isEnabled()){
       driver.findElement(By.id("kw")).sendKeys("百度地图");
            driver.findElement(By.id("su")).click();
       System.out.println("百度搜索框可输入搜索");
        }else{
       System.out.println("百度搜索输入框不可编辑");
        }
        driver.quit();
    }
}



最后贴上因为版本问题报的异常,如果出现类型异常可以考虑是eclipse,JDK,selenium之间的版本不融合
The method sendKeys(CharSequence[]) from the type WebElement refers to the missing type CharSequence
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
Exception in thread “main" java.lang.UnsupportedClassVersionError
java.lang.CharSequence cannot be resolved
The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)






TAG: java环境

 

评分:0

我来说两句

Open Toolbar