51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1371|回复: 1
打印 上一主题 下一主题

selenium 入门学习 (一)

[复制链接]
  • TA的每日心情
    无聊
    2024-9-27 10:07
  • 签到天数: 62 天

    连续签到: 1 天

    [LV.6]测试旅长

    跳转到指定楼层
    1#
    发表于 2018-2-7 14:28:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
         一、创建maven工程
         在pom.xml依赖中添加selenium的jar包:
    1. <dependency>
    2.     <groupId>org.seleniumhq.selenium</groupId>
    3.     <artifactId>selenium-java</artifactId>
    4.     <version>2.53.1</version>
    5. </dependency>
    复制代码
        二、创建第一个seleniumTest类:
    1. import org.openqa.selenium.WebDriver;
    2. import org.openqa.selenium.chrome.ChromeDriver;
    3. public class SeleniumTest {
    4.     public static void main(String[] args) {
    5.         WebDriver webdriver = new ChromeDriver();
    6.         webdriver.get("https://www.baidu.com/");
    7.     }
    8. }
    复制代码
         启动测试报错:
    1. 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
    2.     at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    3.     at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    4.     at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
    5.     at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
    6.     at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    7.     at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    8.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    9.     at com.pingan.csj.SeleniumTest.main(SeleniumTest.java:8)
    复制代码
        报错原因:没有找到chromeWebDriver,下载webDriver放到工程文件下(下载地址:
    https://sites.google.com/a/chromium.org/chromedriver/downloads)  

         代码中增加设置WebDriver路径
    1. import org.openqa.selenium.WebDriver;
    2. import org.openqa.selenium.chrome.ChromeDriver;
    3. public class SeleniumTest {
    4.     public static void main(String[] args) {
    5.                 //配置webDriver路径
    6.         System.setProperty("webdriver.chrome.driver", "D:\\workspace-ta\\csj\\src\\main\\resources\\driver\\chromedriver.exe");
    7.         WebDriver webdriver = new ChromeDriver();
    8.         webdriver.get("https://www.baidu.com/");
    9.     }
    10. }
    复制代码
          重新执行;
           启动后报错如下:
    1. Starting ChromeDriver 2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b) on port 39480
    2. Only local connections are allowed.
    3. Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created exception
    4. from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"103372.1","isDefault":true},"id":1,"name":"","origin":"://"}
    5.   (Session info: chrome=59.0.3071.115)
    6.   (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    7. Command duration or timeout: 3.83 seconds
    8. Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
    9. System info: host: 'PAFSH-D0564', ip: '10.28.81.85', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'
    10. Driver info: org.openqa.selenium.chrome.ChromeDriver
    11.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    12.     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    13.     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    14.     at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    15.     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    16.     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    17.     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    18.     at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
    19.     at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    20.     at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
    21.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:170)
    22.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:159)
    23.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    24.     at com.pingan.csj.SeleniumTest.main(SeleniumTest.java:12)
    复制代码
         报错原因:webDriver和chrome版本不匹配;报错session不能创建一般都是这个原因;找到匹配版
    本替换webdriver;重新执行;执行成功
          三、对代码进行部分优化
    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.util.concurrent.TimeUnit;

    4. import org.openqa.selenium.WebDriver;
    5. import org.openqa.selenium.chrome.ChromeDriver;

    6. public class SeleniumTest {
    7.     public static void main(String[] args) throws IOException {
    8.         // 通过查找当前路径返回一个规范化路径,这样可以把工程放到不同地方执行了
    9.         String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
    10.         System.setProperty("webdriver.chrome.driver", driverPath);
    11.         WebDriver webDriver = new ChromeDriver();
    12.         webDriver.get("https://www.baidu.com/");
    13.         webDriver.close();
    14.     }
    15. }
    复制代码


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-23 15:22 , Processed in 0.064539 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表