java獲取異常信息,Selenium碰到的異常記錄

 2023-11-22 阅读 31 评论 0

摘要: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)Cau

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目錄下的包。

java獲取異常信息,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源碼的方法

?

shell腳本異常捕獲、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的源碼了。

shell異常處理、轉載于:https://www.cnblogs.com/yyhh/p/5194672.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/184574.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息