selenium学习小记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium学习小记相关的知识,希望对你有一定的参考价值。

selenium用于页面自动化测试,其简介云云这里不再粘来粘去的赘述,浪费时间。下面说说自己遇到的一点小问题,及其处理方法。

一:selenium安装

  安装方法,百度一下,相比大家都会见到很多前辈们的详细解释。但是也并不是每个都好使的。

  本人使用的火狐版本是53.0.3(32),直接在插件中搜索selenium,安装了其所有插件,重启浏览器,点击selenium IDE图标,反应半天后经给我弹出窗口selenium IDE为安装!!!

  selenium官网也因为墙的问题不能访问,后来参照了易百教程(http://www.yiibai.com/selenium/selenium_download_ide.html),成功安装。

  打开浏览器,录制操作,重放,一切都很顺利。

二:MyEclipse中使用selenium

  第一步中已经录制了简单的demo。执行导出java/JUnit4/WebDriver,得到Demo.java文件。

  在MyEclipse中创建maven项目,根据需要,引入依赖junit,selenium-chrome-driver|selenium-firefox-driver(注意自己浏览器的版本),selenium-java-client-driver。

  把生成的Demo.java文件放入到项目中。处理包路径等问题。

  运行。。。

  哈哈,问题来了!  

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
...

  根据提示,设置path:  

File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

  再次执行,哇!好感动,浏览器成功启动了!然而。。。后续的操作却没有,我们记录的访问等等都没有执行!

回到MyEclipse,发现JUnit报错:

org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: ‘dfb1306b85be4934d23c123122e06e602a15e446‘, revision: ‘unknown‘, time: ‘2013-01-17 15:05:54‘
System info: os.name: ‘Windows 7‘, os.arch: ‘amd64‘, os.version: ‘6.1‘, java.version: ‘1.7.0‘
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:115)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:161)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at com.zoneland.ysj.webTest.ChromeTest.setUp(ChromeTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
at java.lang.reflect.Method.invoke(Method.java:620)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: ‘dfb1306b85be4934d23c123122e06e602a15e446‘, revision: ‘unknown‘, time: ‘2013-01-17 15:05:54‘
System info: os.name: ‘Windows 7‘, os.arch: ‘amd64‘, os.version: ‘6.1‘, java.version: ‘1.7.0‘
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:506)
... 29 more
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:47688/status] to be available after 20004 ms
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104)
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163)
... 31 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)
... 32 more
Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:213)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
... 33 more

   上述问题几经百度未果,无奈只好硬着头皮看报错,原来是ChromeDriver无法调用。经过查阅得知ChromeDriver是selenium操作chrome的驱动器,他不存在与我们的java代码中,我们需要根据自己chrome的版本下载对应的ChromeDriver.exe,我将下载到的ChromeDriver.exe放在了chrome同级目录下。然后修改我们的代码:

  File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\ChromeDriver.exe");
  System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

  运行,记录的访问完美执行。

以上是关于selenium学习小记的主要内容,如果未能解决你的问题,请参考以下文章

python学习小记

SPSS学习小记

logstash 学习小记

Python学习小记

python学习小记1

神经网络学习小记录70——Pytorch 使用Google Colab进行深度学习