Selenium环境需要的软件:jdk 1.7 ,FF 46.0, selenium-java-2.52
Java和selenium版本不一样会出现兼容问题
Java项目结构以及包引入
参照http://www.cnblogs.com/hanlong/p/5258706.html
注意的一点就是,除了将下面的文件copy到工程之外,还需要另外将jar包引入工程
引入步骤是:添加buildpath,项目目录右键-->BuildPath--> config build path-->Java Build Path-->Libraries-->Add JARs
把libs文件夹下的jar包全部添加上,再添加selenium-java-2.52.0和selenium-java-2.52.0-srcs
完成之后结果如下:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//importorg.openqa.selenium.ie.InternetExplorerDriver;
//importorg.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.*;
public class WebdriverTest {
//public WebDriver driver;
WebDriverdriver = new FirefoxDriver();
//WebDriver driver = new InternetExplorerDriver();
//WebDriver driver = new ChromeDriver();
publicvoid openURL() {
//Firefox
//设置的属性可以直接是webdriver.firefox.bin,然后是firefox的安装详细目录。但是对于Chrome和IE就需要设置成webdriver的目录。("webdriver.chrome.driiver","G:\\Tool\\Selenium\\Selenium
//Driver\\chromedriver.exe"
//firefox不需要setProperty也可以打开浏览器
//如果不想用setProperty的方式,可以将chromedriver.exe
//放在”C:\Windows\System32”路径下或者path可以找到的路径下并重启电脑即可。
//System.setProperty("webdriver.firefox.bin", "C:/ProgramFiles/Mozilla
//firefox/firefox.exe");
//System.setProperty("webdriver.ie.driver",
//"G://webdriver//IEDriverServer.exe");
//System.setProperty("webdriver.chrome.driver",
//"G://webdriver//chromedriver.exe");
driver.get("http://www.baidu.com/");
}
publicvoid input_search() {
driver.manage().window().maximize();
WebElementtxtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("selenium");
WebElementbtn = driver.findElement(By.id("su"));
btn.click();
driver.close();
}
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
WebdriverTestd = new WebdriverTest();
d.openURL();
d.input_search();
}
}
/**
*http://www.cnblogs.com/hanlong/p/5258706.html when the error encountered as
*bellow Exception in thread "main" java.lang.Error: Unresolvedcompilation
*problem: The method sendKeys(CharSequence[]) in the type WebElement is not
*applicable for the arguments (String)
*
* ithas a simple solution. Change your compiler compliance level from 1.4 to
*1.7.
*
*Follow these steps in your eclipse:
*
*1.Right click on your java project and select Build Path -> Click on
*Configure Build Path... 2.In project properties window, Click/select Java
*Compiler at the left panel 3.At the right panel, change the Compiler
*compliance level from 1.4 to 1.7 (Select which is higher version in your
*eclipse) 4.Lastly Click on Apply and OK Now check your code. it will never
*show the same error.
*****************************************************************
*
*
* 出现这个错误时,处理办法:org.openqa.selenium.firefox.NotConnectedException: Unable to
*connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console
*output: 其实原因很简单,就是你的Selenium版本和firefox 不兼容了。去下载火狐的旧版本,或是更新selenium jar包
*Firefox各版本下载地址;使用firefox 46.0+selenium 2.52.0
*http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/
*
*Selenium Jar 包下载地址:
*
*http://selenium-release.storage.googleapis.com/index.html
* 原文:http://www.webdriver.org/article-22-1.html
*/
第一个代码出现问题的可能都在注释中标注了,而且不同的driver也在代码中试过。
注意点就是,IE需要设置安全模式low,google最好也不要有安全限制
其他的注意点就是webdriver的exe文件仅仅是IE和FF使用,而且应该放在单独的文件中,用的时候使用全路径就好。