【问题标题】:Shows exception in java code (Selenium + Jsoup)在 java 代码中显示异常(Selenium + Jsoup)
【发布时间】:2013-04-11 03:32:57
【问题描述】:

我正在做一个项目。在那,我必须获得 HTML 页面源代码。为此,我使用Selenium调用firefox驱动,并将页面源代码存储在String中,然后使用Jsoup解析

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

我的selenium代码如下,

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;
}

//main函数下面的代码(url1和url2是两个url)

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

例外

[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

【问题讨论】:

  • 你在项目中运行了多少个脚本(类似这个)?例外说两件事:1)无法连接到您的Firefox和2。)磁盘上没有足够的空间。因此,请确保在完成项目之前,您的 firefox 浏览器实例不会在脚本中的其他位置关闭,如果磁盘空间不足,请尝试限制项目中的脚本数量。
  • 1) 为什么它没有连接到Firefox,因为互联网正在工作? 2)空间足够,即我的硬盘中有84 GB的格力。是的,浏览器每次都是关闭的,获取到任意一个url的页面源后,就可以看到上面的代码了。我认为上面的createFirefoxProfile 模块有问题,请您帮帮我。
  • 是的,看起来问题出在 createProfile 方法中。看看这个问题。看起来和你的问题类似。 stackoverflow.com/questions/13514338/…
  • 保留 C:\Users\您的计算机名\AppData\Roaming\Mozilla\Firefox\Profiles 中已有的 FF 配置文件的备份并删除它们。在此之后尝试运行您的代码。让我们知道结果。
  • @HemChe 在此处创建或使用配置文件 C:\tmp\firefox-profile-dir @devsda 清理您的目录 C:\tmp\firefox-profile-dir 并检查您的 C:\ 剩余空间。

标签: java exception selenium jsoup


【解决方案1】:

您可能在使用 USB 闪存驱动器或 SD 卡。检查您的硬盘可用空间以及您是否正在使用外围设备。

【讨论】:

  • 不,我正在使用具有 248 GB 硬盘的桌面,可用空间为 85 GB。
  • 我建议您尝试使用空配置文件。喜欢FirefoxProfile profile = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(profile);
  • 其实我做了createFirefoxProfile函数,因为它解决了每次创建新浏览器实例的问题,需要很多时间。你能优化这个模块吗?所以我可以减少时间成本,也解决了这个异常。
  • 您的个人资料在哪里?因为/tmp/firefox-profile-dir 路径是假的,如果你想从像D:\Users\marco\Desktop\dev\tmp\firefox-profile-dir这样的高清中获取它
  • 个人资料是什么意思?我不明白你在上面的评论中所说的,你能用更简单的术语问我吗?我从这里获取了上面的代码,stackoverflow.com/questions/15830334/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 2020-09-02
  • 2017-04-25
  • 1970-01-01
相关资源
最近更新 更多