chrome Webdriver 无法解析为类型错误 eclipse 和 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48897286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:55:30  来源:igfitidea点击:

chrome Webdriver can't be resolved to a type error eclipse and java

javaeclipseselenium

提问by Pete

Im trying to do some automation projects with chrome, java and selenium and having problems importing the chrome driver.

我试图用 chrome、java 和 selenium 做一些自动化项目,但在导入 chrome 驱动程序时遇到问题。

package main;

import org.openqa.selenium.*;

public class SitePoster {

    public static void main(String[] args) {
         //System.setProperty("webdriver.chrome.driver", "./pathtodriver");
        WebDriver driver = new ChromeDriver();
        //Getting error saying "ChromeDriver can't be resolved to a type"
    } 

}

回答by Shwetank Vashishtha

ChromeDriver is one extra level nested in org.openqa.selenium package. Try with import statement

ChromeDriver 是嵌套在 org.openqa.selenium 包中的一个额外级别。尝试导入语句

org.openqa.selenium.chrome.*;

org.openqa.selenium.chrome.*;

OR

或者

import org.openqa.selenium.chrome.ChromeDriver;

导入 org.openqa.selenium.chrome.ChromeDriver;

回答by DebanjanB

The error says it all :

错误说明了一切:

"ChromeDriver can't be resolved to a type"

While working with Selenium 3.xyou have to mention the Key-Valuepair through System.setProperty()line mandatory as follows :

在使用Selenium 3.x 时,您必须通过以下强制行提及键值System.setProperty()

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

Additionally, as per best practices instead of import org.openqa.selenium.*;you have to mention the individual packages for import as follows :

此外,根据最佳实践,import org.openqa.selenium.*;您不必提及要导入的各个包,如下所示:

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

回答by Shaqeel Ahamad

You need to attach jar files. I was getting the exactly the same issue. you need to install "Selenium 3.4.0" jar files. Which contains lib folder of .jar files and "client-combined-3.4.0-nodeps" files. After you attach these jar files refresh the code by giving run command.

您需要附加 jar 文件。我遇到了完全相同的问题。您需要安装“Selenium 3.4.0”jar 文件。其中包含 .jar 文件和“client-combined-3.4.0-nodeps”文件的 lib 文件夹。附加这些 jar 文件后,通过给出运行命令刷新代码。

回答by dilRox

Adding Selenium jar files from the build path > add external libraries fix the issue for me.

从构建路径添加 Selenium jar 文件 > 添加外部库为我解决了这个问题。

回答by Anogoya Dagaate

The solution for me was the POM specified an older version of ChromeDriverManager which was how chromeDriver was obtained in my build. If you are specifying a location using setProperty this will of course not apply to you. But it helps to check with the version of the dependencies in your POM

我的解决方案是 POM 指定了旧版本的 ChromeDriverManager,这是在我的构建中获取 chromeDriver 的方式。如果您使用 setProperty 指定位置,这当然不适用于您。但它有助于检查 POM 中依赖项的版本

<dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>3.6.1</version>
        <scope>test</scope>
    </dependency>

That did it!

做到了!

回答by Jeffer thomas

For Linux user go to terminal check google version with below command

对于 Linux 用户,请使用以下命令前往终端检查谷歌版本

google-chrome --version

谷歌浏览器 --version

OP:- Google Chrome 69.0.3497.100

操作:-谷歌浏览器 69.0.3497.100

Now, Upgrade chrome using below command

现在,使用以下命令升级 chrome

sudo apt --only-upgrade install google-chrome-stable

sudo apt --only-upgrade 安装 google-chrome-stable

After this check the google chrome version using above same command

在此之后使用上述相同的命令检查谷歌浏览器版本

google-chrome --version

谷歌浏览器 --version

OP:- Google Chrome 78.0.3904.87

操作:-谷歌浏览器 78.0.3904.87

Now you need to download the same version google driver. you can find it from the below link.

现在您需要下载相同版本的谷歌驱动程序。你可以从下面的链接找到它。

https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/

https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/

Download google driver and extract it and save the path and use it in the code, Now run the code.

下载谷歌驱动程序并解压并保存路径并在代码中使用,现在运行代码。

回答by Gaurav khurana

if you are not able to add this statement

如果您无法添加此语句

 import org.openqa.selenium.chrome.ChromeDriver;

the probable reason is you are using mavenand you have not included the below

可能的原因是您使用的是maven而您没有包含以下内容

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>

Even if we put the below statement, problem still comes

即使我们把下面的语句,问题仍然存在

    System.setProperty("webdriver.chrome.driver", "C:\Software\chromedriver_win32\chromedriver.exe");

So the solution is goto maven repository site and take the chrome dependency from there to solve this problem

所以解决方案是转到 maven 存储库站点并从那里获取 chrome 依赖来解决这个问题

回答by Sindhuja Balasubramanian

Even after installing the required jar files I was facing the same error message and then I updated the compiler compliance level in the Java compiler to 1.8(depends) and then error disappeared. It solved my issue.

即使在安装了所需的 jar 文件后,我也遇到了相同的错误消息,然后我将 Java 编译器中的编译器合规性级别更新为 1.8(依赖),然后错误消失了。它解决了我的问题。

You can follow below steps:

您可以按照以下步骤操作:

  1. Select the Java project(created one)

  2. Build Path

  3. Configure build path

  4. Java compiler

  5. Change the compiler compliance level(I selected 1.8)

  6. Apply and Close.

  1. 选择Java项目(创建一个)

  2. 构建路径

  3. 配置构建路径

  4. Java编译器

  5. 更改编译器合规性级别(我选择了 1.8)

  6. 应用并关闭。

回答by nimmy sukumaran

I was getting the exact same error. It did resolve for me after performing the below steps.

我得到了完全相同的错误。执行以下步骤后,它确实为我解决了。

  1. Right click on Project>Buildpath>configure build path
  2. Libraries>Add external Jars.
  3. Add the external Jars from the 'libs' folder first, then add the 'Client component' ones.

    import org.openqa.selenium.WebDriver;    
    import org.openqa.selenium.chrome.ChromeDriver;        
    
    public class test1 {
    
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\Users\nsukumar\Documents\selenium\chromedriver_win32\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://google.co.in");
    
        } 
    }
    
  1. 右键单击项目>构建路径>配置构建路径
  2. 图书馆>添加外部罐子。
  3. 首先从“libs”文件夹添加外部 Jars,然后添加“客户端组件”。

    import org.openqa.selenium.WebDriver;    
    import org.openqa.selenium.chrome.ChromeDriver;        
    
    public class test1 {
    
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\Users\nsukumar\Documents\selenium\chromedriver_win32\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://google.co.in");
    
        } 
    }
    

回答by Alok Ranjan

chrome Webdriver can't be resolved to a type error eclipse and java:-

chrome Webdriver 无法解析为类型错误 eclipse 和 java:-

I was getting same problem with selenium -java 3.141.59 on macOs.Check below steps to resolve issue:-

我在 macOs 上遇到了与 selenium -java 3.141.59 相同的问题。检查以下步骤来解决问题:-

  1. Create a new Java Project >class >Create Class name
  2. Right click on Project >Property > Build path > Libraries
  3. Add external Jars to Classpath download from selenium client & web driver language binding.
  4. Apply and Close.
  1. 创建一个新的 Java 项目 >class >Create Class name
  2. 右键单击项目>属性>构建路径>库
  3. 从 selenium 客户端和 Web 驱动程序语言绑定将外部 Jars 添加到 Classpath 下载。
  4. 应用并关闭。

Hope you got your Issue fixed. Kindly reply back in case of any issue.

希望你的问题得到解决。如有任何问题,请回复。