简体   繁体   English

无法实例化类型 WebDriver

[英]Cannot instantiate the type WebDriver

I have just started with Selenium and have already hit a problem.我刚刚开始使用 Selenium 并且已经遇到了问题。

Simple enough, but I'm totally missing it, just trying to create an instance of WebDriver but getting the error cannot instantiate...很简单,但我完全错过了它,只是尝试创建 WebDriver 的实例但无法实例化错误...

在此处输入图片说明

I have all the jar files, where am I going wrong?我有所有的 jar 文件,我哪里出错了?

It is giving error because WebDriver is a interface not a class.它给出了错误,因为 WebDriver 是一个接口而不是一个类。 so create object of webdriver as below:-所以创建 webdriver 的对象如下:-

WebDriver driver = new FirefoxDriver();

In the above statement, WebDriver is an interface.在上面的语句中,WebDriver 是一个接口。 An interface contains empty methods that have been defined but not implemented.接口包含已定义但未实现的空方法。 These methods can be implemented by anyone as long as the method type and signatures are not violated.只要不违反方法类型和签名,任何人都可以实现这些方法。 Therefore, an interface is also known as contract, because you can use an interface as you like but you cannot change the way it has been defined.因此,接口也称为契约,因为您可以随意使用接口,但不能更改它的定义方式。 And, since it has empty methods you won't actually need to instantiate it and so you cannot instantiate it.而且,由于它有空方法,您实际上不需要实例化它,因此您无法实例化它。

FirefoxDriver is a class that has been written specifically for the Firefox browser. FirefoxDriver 是专门为 Firefox 浏览器编写的类。 It has methods that are implemented and it can be instantiated.它具有已实现的方法并且可以实例化。 It can perform all functions (or methods) on the Firefox browser as defined in the interface WebDriver.它可以在 Firefox 浏览器上执行接口 WebDriver 中定义的所有功能(或方法)。

So in the above statement, we are actually telling FirefoxDriver class that "hey you can automate the various methods that you want on the Firefox browser but you need to stick to the contract defined in WebDriver".所以在上面的声明中,我们实际上是在告诉 FirefoxDriver 类“嘿,你可以在 Firefox 浏览器上自动化你想要的各种方法,但你需要坚持 WebDriver 中定义的契约”。 So we declare a reference variable of type WebDriver and then use it to instantiate FirefoxDriver, which means that the object (driver) is of type WebDriver but points to the memory allocation to all data and methods in FirefoxDriver (and, as mentioned above, the FirefoxDriver class already has the implemented version of methods in WebDriver).所以我们声明了一个 WebDriver 类型的引用变量,然后用它来实例化 FirefoxDriver,这意味着对象(驱动程序)是 WebDriver 类型但指向 FirefoxDriver 中所有数据和方法的内存分配(以及,如上所述, FirefoxDriver 类已经具有 WebDriver 中方法的实现版本)。 So all good :)所以一切都很好:)

By using this technique, we have made it easy for the tester to use any browser of his or her liking.通过使用这种技术,我们使测试人员可以轻松使用他或她喜欢的任何浏览器。 For example, to automate on IE driver, one will have to simply write a statement like例如,要自动化 IE 驱动程序,您必须简单地编写如下语句

WebDriver driver = new IEDriver();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM