简体   繁体   中英

WebDriver and ChromeDriver cannot be resolved to a type

I know this is a very frequently asked question but i have tried so many fixes to this problem (including: downloading java and eclipse again) and none of the fixes worked. i am asking for very specific and simplified help because i am new to this subject and i don't understand a lot.

i get an error in the imports and in the chromedriver and webdriver.

this is the code:

package firstPackage;

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

public class FirstScript {

    public static void main(String[] args) {
        System.setProperty("Webdriver.Chrome.driver","/C:/Users/shale/Downloads/chromedriver_win321/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");

    }

}

here is the project with all of the selenium jars that i downloaded from their site here is where the chromedrivere.exe file is stored

输出

Can you change

System.setProperty("Webdriver.Chrome.driver","/C:/Users/shale/Downloads/chromedriver_win321/chromedriver");

to

System.setProperty("Webdriver.Chrome.driver","/C:/Users/shale/Downloads/chromedriver_win321/chromedriver.exe"); 

You are getting the error because you have not added the selenium dependencies to your classpath.

I strongly advice you to use a dependency management tool such as Maven or Gradle to do this.

However, if you would still want to add all the dependencies, then the following dependencies would be required (you may try to have only the chrome dependencies and give it a go)

已解决的依赖关系

You should also change

System.setProperty("Webdriver.Chrome.driver","/C:/Users/shale/Downloads/chromedriver_win321/chromedriver");

to

System.setProperty("webdriver.chrome.driver","C:/Users/shale/Downloads/chromedriver_win321/chromedriver.exe");

(The case of the property matters).

It should be System.setProperty("Webdriver.chrome.driver","/C:/Users/shale/Downloads/chromedriver_win321/chromedriver.exe"); in setProperty().

Also please add all the required jar files.

This error message...

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
ChromeDriver cannot be resolved to a type

...implies that WebDriver and ChromeDriver wasn't resolved at compiletime .


Seems your imports are good to go. However, as you are using OS you need to take care of a few things as follows:

  • In the System.setProperty() line, you need to replace Webdriver.Chrome.driver with webdriver.chrome.driver .
  • You need to provide the absolute path of the chromedriver removing the initial back slash ie / before C: .
  • You can also provide the absolute path of the chromedriver escaping the forward slash ie \\\\ .
  • You need to provide the extension of the chromedriver binary, ie exe .
  • So effectively, the line of code will be:

     System.setProperty("webdriver.chrome.driver","C\\\\Users\\\\shale\\\\Downloads\\\\chromedriver_win321\\\\chromedriver.exe");

You can find a relevant discussion in java.lang.Error: Unresolved compilation problems : WebDriver/ChromeDriver cannot be resolved to a type error while executing selenium tests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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