selenium+java自动化测试入门

1.准备jar包,

2.浏览器对应driver。

1.chromedriver 下载地址

2.Firefox的驱动geckodriver 下载地址

3.IE的驱动IEdriver 下载地址

 

实例代码:

public  static void main(String[] args){
		try {
			// 设置 chrome的路径
			System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\Desktop\\chromedriver.exe");
			ChromeOptions options = new ChromeOptions();
			options.addArguments("disable-infobars");
			options.addArguments("start-maximized");
			//不打开真实浏览器而使用虚拟浏览器执行操作
//			options.addArguments("headless");
			WebDriver driver = new ChromeDriver(options);
			// 让浏览器访问 Baidu
			driver.get("http://www.baidu.com");
			System.out.println("1 Page title is: " + driver.getTitle());
			// 通过 id 找到 input 的 DOM
			WebElement element = driver.findElement(By.id("kw"));
			// 输入关键字
			element.sendKeys("bootstrap");
			driver.findElement(By.id("su")).click();
			Thread.sleep(2000);
			List titles = driver.findElements(By.cssSelector("h3.t"));
			for (WebElement title : titles) {
				WebElement webTitle = title.findElement(By.tagName("a"));
				System.out.println("webTitle:" + webTitle + ":" );
				System.err.println( webTitle.getText());
			}
			
			// 关闭浏览器
			// driver.quit();
			//杀掉进程
			WindowsUtils.killByName("chromedriver.exe");
			WindowsUtils.killByName("chromedriver.exe");
//			chrome.exe
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

下面贴出常见错误信息:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:157)
    at com.as.test.Test.main(Test.java:22)

没有添加system property
解决方案:加上 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\Desktop\\chromedriver.exe");

Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: C:\Users\Administrator\Desktop\chromedrive1r.exe
    at com.google.common.base.Preconditions.checkState(Preconditions.java:585)
    at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:146)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:141)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:157)
    at com.as.test.Test.main(Test.java:22)

驱动位置问题
解决方案:配置正确的路径,System.setProperty第二个参数是你对应某个浏览器driver的存放位置。

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: unable to discover open pages
  (Driver info: chromedriver=2.0,platform=Windows NT 6.2 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20.62 seconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-I18QONF', ip: '10.0.0.41', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_73'
Driver info: driver.version: ChromeDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$errorHandler$0(JsonWireProtocolResponse.java:54)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:157)
    at com.as.test.Test.main(Test.java:22)

driver版本问题,使用正确版本的driver.
   

注意:

firefox56以下不需要安装webdriver驱动,firefox57(firefox47及以上版本)需要安装geckodriver-v0.19.1-win32/64。

IEDriverServer的版本号和Selenium的版本号一定要一致。

demo下载

你可能感兴趣的:(学习记录)