JavaFX apllication不与Selenium WebDriver一起运行

编程入门 行业动态 更新时间:2024-10-28 02:32:57
本文介绍了JavaFX apllication不与Selenium WebDriver一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在JavaFX应用程序中使用Selenium WebDriver,我想获取网页的屏幕截图并在JavaFX应用程序的ImageView中显示它。

I want to use Selenium WebDriver in JavaFX application that I want to get a screenshot of a webpage and show it in ImageView in JavaFX app.

此代码完全有效罚款截取网页并保存:

This code works completely fine to take screenshot of webpage and save it:

package application; import java.io.File; import org.apachemons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; public class Main{ public static void main(String[] args) throws Exception { File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe"); System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe"); System.setProperty("phantomjs.binary.path", file.getAbsolutePath()); WebDriver driver = new PhantomJSDriver(); driver.get("google"); // taking screenshot File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("screenshot.png")); driver.close(); System.out.println("Done!"); } }

为了在JavaFX app中使用保存的屏幕截图我试图对这个类做一些简单的修改,使它成为一个JavaFX应用程序。

In order to use the saved screenshot in JavaFX app I tried to make some simple modifications to this class to make it a JavaFX app.

这是修改后的代码:

import org.apachemons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import javafx.application.Application; import javafx.stage.Stage; public class Main extends Application{ public static void main(String[] args) throws Exception { File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe"); System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe"); System.setProperty("phantomjs.binary.path", file.getAbsolutePath()); WebDriver driver = new PhantomJSDriver(); driver.get("google"); // taking screenshot File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("screenshot.png")); driver.close(); System.out.println("Done!"); launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.show(); } }

这是带有一些代码的最简单的JavaFX应用程序之一在main方法中,当我运行它时,它应该保存屏幕截图并打开一个空白的JavaFX应用程序。但是当我运行它时,它什么也没做,它甚至都没有执行main方法。它在1-2秒后终止而没有做任何事情。

This is one of the simplest JavaFX applications with some code in main method and when I run it, it should save the screenshot and open a blank JavaFX app. But when I run it, it does not do anything, it does not even execute the main method. It gets terminated after 1-2 seconds without doing anything.

然后我意识到当Selenium Webdriver库在构建路径中时,JavaFX应用程序不会运行。即使我从类中删除了所有Selenium的东西,它也会遇到同样的问题。

Then I realized that JavaFX application does not run when Selenium Webdriver library is in the build path. That even if I delete all Selenium stuff from the class, it ends up with the same problem.

package application; import javafx.application.Application; import javafx.stage.Stage; public class Main extends Application{ public static void main(String[] args) throws Exception { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.show(); } }

上述代码仅在我删除Selenium WebDriver库时运行来自项目的构建路径。

The code above runs only when I delete Selenium WebDriver library from the project's build path.

我在两个IDE中尝试了这些:在Eclipse中,程序在1-2秒后终止,没有做任何事情,没有任何错误。 在NetBeans中,程序在1-2秒后终止而没有做任何事情,但它给了我一个错误:

I have tried these in two IDEs: In Eclipse, the program gets terminated after 1-2 seconds without doing anything and without any error. In NetBeans, the program gets terminated after 1-2 seconds without doing anything but it gives me an error:

C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: -1073740791 BUILD FAILED (total time: 2 seconds)

我用Java编写代码差不多2年了,但它是我第一次遇到这样的问题。我google了很多,没有找到类似的东西。

It is almost 2 years that I am writing code in Java, but it is the first time that I come across such a problem. I googled a lot and didn't find anything similar to this.

有没有人知道这里发生了什么,问题是什么?任何想法都将不胜感激。

Does anyone have an idea what is going on here and what is the problem? Any idea would be appreciated.

编辑:当我在IDE之外运行生成的jar文件时,它正常工作。

it works normally when I run the generated jar-file outside of IDE.

感谢您的考虑。

推荐答案

我刚刚找到了这个问题的原因。问题与NVIDIA驱动程序有关。我使用的是版本378.49,我回滚到版本376.33。现在,问题解决了。您可以在 intellij-support.jetbrains/hc/en-us/community/posts/115000060510-Process-finished-with-exit-code-1073740791-0xC0000409-JavaFX-应用程序

I just found the reason of this issue. The problem is related to the NVIDIA Driver. I was using version 378.49 and I rolled back to version 376.33. Now, the problem is solved. You can see more details on intellij-support.jetbrains/hc/en-us/community/posts/115000060510-Process-finished-with-exit-code-1073740791-0xC0000409-JavaFX-Application

感谢@KrishnanMahadevan提醒我使用IntelliJ IDE。

Thanks to @KrishnanMahadevan for reminding me to use IntelliJ IDE.

更多推荐

JavaFX apllication不与Selenium WebDriver一起运行

本文发布于:2023-10-28 02:38:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不与   apllication   JavaFX   Selenium   WebDriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!