在Java代码中显示异常(Selenium + Jsoup) [英] Shows exception in java code (Selenium + Jsoup)

查看:103
本文介绍了在Java代码中显示异常(Selenium + Jsoup)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目。在那里,我必须得到HTML页面的源代码。为此,我使用 Selenium 调用firefox驱动程序,并将页面源代码存储在String中,然后使用 Jsoup解析 / p>

我的代码适用于单个 url 。但是当我把我的代码放在测试中,它必须逐个获取URLS的数量,那么最后它会抛出一个异常,我的项目失败。请看异常,告诉我为什么会发生这种情况,并给我一些解决方案来克服这个异常。



我的硒代码如下,

  private static FirefoxProfile createFirefoxProfile(){
File profileDir = new File(/ tmp / firefox-profile-dir );
if(profileDir.exists()){
返回新的FirefoxProfile(profileDir);
}
FirefoxProfile firefoxProfile = new FirefoxProfile();
文件dir = firefoxProfile.layoutOnDisk();
try {
profileDir.mkdirs();
FileUtils.copyDirectory(dir,profileDir);
} catch(IOException e){
e.printStackTrace();
}
返回firefoxProfile;
}

//下面的代码主要功能(url1和url2是两个url)

  WebDriver驱动程序=新的FirefoxDriver(createFirefoxProfile()); 
driver.get(url1);
String hml1 = driver.getPageSource();
driver.get(url2);
String hml2 = driver.getPageSource();
driver.close();

异常

  [警告] 
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect。 NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在org.codehaus.mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java:297)
在java.lang.Thread.run(Thread.java:662)
导致: org.openqa.selenium.WebDriverException:端口7056无法连接到二进制FirefoxBinary(C:\Program文件(x86)\Mozilla Firefox\firefox.exe);过程输出如下:
null
构建信息:版本:'2.31.0',修订版本:'1bd294d185a80fa4206dfeab80ba773c04ac33c0',时间:'2013-02-27 13:51:26'
系统信息:os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.6.0_43'
驱动程序信息:driver.version:FirefoxDriver
在org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:122)
在org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:244)
在org.openqa.selenium.remote.RemoteWebDriver。< init>(RemoteWebDriver.java:110)
在org.openqa.selenium.firefox.FirefoxDriver。< init>(FirefoxDriver.java:188)
在org.openqa.selenium.firefox.FirefoxDriver。< init>(FirefoxDriver.java:183)
在org.openqa.selenium.firefox.FirefoxDriver。< init>(FirefoxDriver.java:179)
在org.openqa.selenium.firefox.Fir efoxDriver。< init>(FirefoxDriver.java:96)
at XXX.YYY.ZZZ.Template_Matching.Scroll_down.processing(Scroll_down.java:334)
at XXX.YYY.ZZZ.Template_Matching.Scroll_down .main(Scroll_down.java:472)
... 6更多
导致:org.openqa.selenium.firefox.UnableToCreateProfileException:java.io.IOException:磁盘上没有足够的空间
构建信息:版本:'2.31.0',修订版本:'1bd294d185a80fa4206dfeab80ba773c04ac33c0',时间:'2013-02-27 13:51:26'
系统信息:os.name:'Windows 7' os.arch:'amd64',os.version:'6.1',java.version:'1.6.0_43'
驱动程序信息:driver.version:FirefoxDriver
在org.openqa.selenium.firefox。 FirefoxProfile.layoutOnDisk(FirefoxProfile.java:453)
在org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:77)
... 14更多
导致:java.io.IOException:没有en磁盘上的空格
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:282)
java.io.BufferedOutputStream .flushBuffer(BufferedOutputStream.java:65)
在java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
在org.openqa.selenium.io.Zip.unzipFile(Zip.java:171 )
在org.openqa.selenium.io.Zip.unzip(Zip.java:152)
在org.openqa.selenium.io.FileHandler.unzip(FileHandler.java:54)
在org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory(FileExtension.java:81)
在org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:58)
在org.openqa.selenium.firefox.internal.ClasspathExtension.writeTo(ClasspathExtension.java:63)
在org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:469)
在org.openqa.selenium.firef ox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:447)
... 15更多
[INFO] ---------------------- --------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ---------------------------------- --------------------------------------
[INFO]总时间:20: 49:03.625s
[INFO]完成于:Thu Apr 18 10:47:12 IST 2013
[INFO] Final Memory:12M / 163M
[INFO] ------ -------------------------------------------------- ----------------
[ERROR]无法执行目标org.codehaus.mojo:exec-maven-plugin:1.2.1:java(default-cli)on project Template_Matching:执行Java类时发生异常。 null:InvocationTargetException:无法连接到端口7056上的二进制FirefoxBinary(C:\Program文件(x86)\Mozilla Firefox\firefox.exe);过程输出如下:
[ERROR] null
[ERROR] Build info:version:'2.31.0',revision:'1bd294d185a80fa4206dfeab80ba773c04ac33c0',time:'2013-02-27 13:51:26'
[ERROR]系统信息:os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.6.0_43'
[ERROR ]驱动程序信息:driver.version:FirefoxDriver:java.io.IOException:磁盘空间不足
[ERROR] Build info:version:'2.31.0',revision:'1bd294d185a80fa4206dfeab80ba773c04ac33c0',time: '2013-02-27 13:51:26'
[ERROR]系统信息:os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version :'1.6.0_43'
[ERROR]驱动程序信息:driver.version:FirefoxDriver
[ERROR] - > [帮助1]
[ERROR]
[ERROR]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。
[ERROR]使用-X开关重新运行Maven以启用完整的调试日志记录。
[错误]
[错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:
[错误] [帮助1] http://cwiki.apache.org / confluence / display / MAVEN / MojoExecutionException


解决方案

在您的USB闪存驱动器或SD卡。查看您的硬盘可用空间,如果您不在外设上工作。


I am working on an project. In that, I have to get HTML page source code. For that, I invoke firefox driver using Selenium, and store page source code in String, and then parse using Jsoup

My code worked fine for single url. But when I put my code in testing, where it has to get numbers of URLS one by one, then at the end it throws one exception, and my project fails. Please see the exception and tell me why this occurs, and give me some solution to overcome this Exception.

My selenium code is as follows,

private static FirefoxProfile createFirefoxProfile() {
    File profileDir = new File("/tmp/firefox-profile-dir");
    if (profileDir.exists()) {
        return new FirefoxProfile(profileDir);
    }
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    File dir = firefoxProfile.layoutOnDisk();
    try {
        profileDir.mkdirs();
        FileUtils.copyDirectory(dir, profileDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return firefoxProfile;
}

// below code in main function (url1 and url2 are two urls )

WebDriver driver = new FirefoxDriver(createFirefoxProfile());
driver.get(url1);
String hml1 = driver.getPageSource();
driver.get(url2);
String hml2 = driver.getPageSource();
driver.close();

Exception

[WARNING] 
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:662)
    Caused by: org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7056; process output follows: 
    null
    Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
    System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
    Driver info: driver.version: FirefoxDriver
        at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:122)
        at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:244)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:188)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:183)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:179)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:96)
        at XXX.YYY.ZZZ.Template_Matching.Scroll_down.processing(Scroll_down.java:334)
        at XXX.YYY.ZZZ.Template_Matching.Scroll_down.main(Scroll_down.java:472)
        ... 6 more
    Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.IOException: There is not enough space on the disk
    Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
    System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
    Driver info: driver.version: FirefoxDriver
        at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:453)
        at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:77)
        ... 14 more
    Caused by: java.io.IOException: There is not enough space on the disk
        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(FileOutputStream.java:282)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
        at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
        at org.openqa.selenium.io.Zip.unzipFile(Zip.java:171)
        at org.openqa.selenium.io.Zip.unzip(Zip.java:152)
        at org.openqa.selenium.io.FileHandler.unzip(FileHandler.java:54)
        at org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory(FileExtension.java:81)
        at org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:58)
        at org.openqa.selenium.firefox.internal.ClasspathExtension.writeTo(ClasspathExtension.java:63)
        at org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:469)
        at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:447)
        ... 15 more
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 20:49:03.625s
    [INFO] Finished at: Thu Apr 18 10:47:12 IST 2013
    [INFO] Final Memory: 12M/163M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project Template_Matching: An exception occured while executing the Java class. null: InvocationTargetException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7056; process output follows:
    [ERROR] null
    [ERROR] Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
    [ERROR] System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
    [ERROR] Driver info: driver.version: FirefoxDriver: java.io.IOException: There is not enough space on the disk
    [ERROR] Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
    [ERROR] System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_43'
    [ERROR] Driver info: driver.version: FirefoxDriver
    [ERROR] -> [Help 1]
    [ERROR] 
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

解决方案

You probably work on your usb flashdrive or SD card. Check out your hard disk free space and if you aren't working on a peripheral.

这篇关于在Java代码中显示异常(Selenium + Jsoup)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆