使用Selenium WebDriver的Spring Boot Web应用程序

use*_*735 4 java selenium spring maven spring-boot

我试图让我的头围绕春季启动,我有一些问题试图将selenium集成到我的春季启动应用程序中.我正在尝试实现一个简单的网页,它有一个输入框和按钮.输入框将包含一个URL,然后该按钮将启动导航到输入的URL的selenium浏览器.

我目前有一个简单的弹簧启动应用程序,包括以下内容:

的index.html

包含传递给myController的输入表单(用户在此处键入URL).

myController.java

@Controller
public class myController {

    @Autowired
    private WebDriver driver;

    ....
}   
Run Code Online (Sandbox Code Playgroud)

的pom.xml

含有硒..

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>   
Run Code Online (Sandbox Code Playgroud)

我运行项目时的错误:

 APPLICATION FAILED TO START


 Description:

 Field driver in com.project.myController required a bean of type 'org.openqa.selenium.WebDriver' that could not be found.


 Action:

 Consider defining a bean of type 'org.openqa.selenium.WebDriver' in your configuration.
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建Selenium WebDriver的实例,以便我可以在需要时使用它.我只会在这个控制器中需要它,所以我在这里宣布它.我错过了什么?任何帮助将不胜感激.先感谢您.

小智 8

您需要有一个WebDriver实例,例如:

@Bean
public WebDriver webDriver() {
    return new WebDriver();
}
Run Code Online (Sandbox Code Playgroud)

在您的一个配置类中.这将是注释的任何内容@Configuration或包含此内容的注释; 在最基本的Spring Boot应用程序中(如Spring Boot示例中所使用的),这可能是这样的:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    // Your beans go here

}
Run Code Online (Sandbox Code Playgroud)

因为@SpringBootApplication带有@SpringBootConfiguration注释的注释@Configuration.