首页 > 解决方案 > 当我尝试为 Selenium Web 驱动程序创建 GUI 时出现“应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常”

问题描述

我尝试使用 JavaFX 为 selenium Web 驱动程序项目创建 GUI,但是当我使用 java 8 在 eclipse 上运行此代码时出现此错误。我希望在单击按钮后启动 chrome 并运行测试用例。这是我的代码:

FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
   <center>
      <Button fx:id="primary" mnemonicParsing="false" onAction="#seleniumLaunch" text="Button" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

控制器类:

在这个按钮功能中,我希望在单击它之后启动 chrome 浏览器,然后转到 baseURL

package application;

import java.net.URL;
import java.util.ResourceBundle;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import selenium.TestCase;

public class SampleController implements Initializable {
TestCase testcase = new TestCase();
    @FXML
    private Button primary;

    @FXML
    void seleniumLaunch() {
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\driver\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
        
            String baseUrl = "http://demo.guru99.com/test/newtours/";
            driver.get(baseUrl);
    }

    @Override // and this
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

错误日志:

Exception in Application start method
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 27 more
Exception running application application.Main

标签: javaseleniumselenium-webdriverjavafx

解决方案


推荐阅读