• Selenium碰到的异常记录


    Java版本的Selenium异常记录

    1. 没有找到类的异常(NoClassDefFoundError)

    异常如下:

    Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
        at com.yy.baidu.testBaidu.main(testBaidu.java:12)
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

    解决办法:

    此问题通常的包引入的少了,少Selenium包,如下图,除了要引入selenium-java-2.52.0.jar包以外,还需要引入libs目录下的包。

    package

    2. 没有找到Firefox的路径

    异常如下:

    Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN8_1
    Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
    System info: host: 'DESKTOP-VFHU33A', ip: '192.168.3.115', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
    Driver info: driver.version: FirefoxDriver
        at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:74)
        at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
        at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
        at com.yy.baidu.testBaidu.main(testBaidu.java:12)

    解决办法:

    方法1. 重装Firefox,这个问题最简单的办法就是重新按照Firefox,稳稳的

    方法2. 用System.setPropert方法设置webdriver.firefox.bin的值,如:

    public class testBaidu {
        public static void main(String[] args){
     
            System.setProperty("webdriver.firefox.bin","D:\Mozilla Firefox\firefox.exe");
     
     
            System.out.println("srart selenium ");
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.baidu.com");
            WebElement txtbox = driver.findElement(By.id("kw"));
            txtbox.sendKeys("selenium java");
            WebElement btn = driver.findElement(By.id("su"));
            btn.click();
            System.out.println("end selenium");
        }
    }

    方法3. 用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法,直接上代码:

    public class testBaidu {
        public static void main(String[] args){
     
            File pathToFirefoxBinary = new File("D:\Mozilla Firefox\firefox.exe");
            FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
            WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。 
     
            System.out.println("srart selenium ");
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.baidu.com");
            WebElement txtbox = driver.findElement(By.id("kw"));
            txtbox.sendKeys("selenium java");
            WebElement btn = driver.findElement(By.id("su"));
            btn.click();
            System.out.println("end selenium");
        }
    }

    注:有人可能会不知道webdriver.firefox.bin,可以看一下源码,其中

    org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromSystemProperty()

    方法第一句

        String binaryName = System.getProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY);

    说明默认的时候取的就是这个值,重新设置一下。

     

    最后附上查看Selenium源码的方法

    1. 右键selenium引入的jar包“selenium-java-2.52.0.jar”选择Properties

    sele1

    2. 点击External location -> External File... -> 找到“selenium-java-2.52.0-srcs.jar”

    sele2

    设置好了之后,就可以查看Selenium的源码了。

  • 相关阅读:
    吉特日化MES-电子批记录普通样本
    吉特日化配料系统-关于25针串口转接
    吉特仓储管理系统--元宵送开发者一份礼物-开源2018年源码
    吉特日化MES-日化生产称料基本步骤
    吉特日化MES-日化行业原料仓库所见问题汇总
    网易数据中台建设实践
    【杭研大咖说】温正湖:6年,从不会写SQL到数据库专家
    从自动化到智能化,网易杭研的AIOps探索与实践
    特征选择之经典三刀
    【杭研大咖说】王新勇:死磕云原生的“懒人”
  • 原文地址:https://www.cnblogs.com/yyhh/p/5194672.html
Copyright © 2020-2023  润新知