使用Selenium WebDriver的Spring Boot Web应用程序 [英] Spring Boot Web Application using Selenium WebDriver

查看:1384
本文介绍了使用Selenium WebDriver的Spring Boot Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试引导Spring Boot,但是在尝试将硒集成到Spring Boot应用程序时遇到了一些问题.我正在尝试实现一个简单的网页,其中包含一个输入框和一个按钮.输入框将包含一个URL,然后该按钮将启动一个硒浏览器,导航到输入的URL.

I am trying to get my head around spring boot and I am having some issues trying to integrate selenium into my spring boot application. I am trying to achieve a simple web page, which has a input box and button. The input box will contain a URL and the button will then launch a selenium browser navigating to that URL entered.

我目前有一个简单的spring boot应用程序,其中包括以下内容:

I currently have a simple spring boot application consisting of the following:

包含一个传递给myController的输入表单(此处为用户类型URL).

Contains an input form (user types URL here) which is passed to myController.

@Controller
public class myController {

    @Autowired
    private WebDriver driver;

    ....
}   

pom.xml

包含硒.

pom.xml

Contains selenium..

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>   

我在运行项目时遇到的错误:

 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.

我正在尝试创建Selenium WebDriver的实例,以便可以在需要时使用它.我只会在此控制器中需要它,所以我在这里声明了它.我想念什么?任何帮助将不胜感激.预先谢谢你.

I am trying to create an instance of Selenium WebDriver so that I can use it whenever I need to. I will only ever need it in this controller, so I declared it here. What am I missing? Any help will be appreciated. Thank you in advance.

推荐答案

您需要具有WebDriver的实例,例如:

You need to have an instance of WebDriver, e.g.:

@Bean
public WebDriver webDriver() {
    return new WebDriver();
}

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

in one of your configuration classes. That would be anything annotated with @Configuration or an annotation which includes this; in the most basic Spring Boot application (as used in Spring Boot examples), this would probably be something like this:

@SpringBootApplication
public class Application {

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

    // Your beans go here

}

因为@SpringBootApplication@SpringBootConfiguration注释,而@SpringBootConfiguration@Configuration注释.

because @SpringBootApplication is annotated with @SpringBootConfiguration which is annotated with @Configuration.

这篇关于使用Selenium WebDriver的Spring Boot Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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