简体   繁体   English

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

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

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"
    } 

}

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创建一个新的 Java 项目 >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.从 selenium 客户端和 Web 驱动程序语言绑定将外部 Jars 添加到 Classpath 下载。
  4. Apply and Close.应用并关闭。

Hope you got your Issue fixed.希望你的问题得到解决。 Kindly reply back in case of any issue.如有任何问题,请回复。

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.即使在安装了所需的 jar 文件后,我也遇到了相同的错误消息,然后我将 Java 编译器中的编译器合规性级别更新为 1.8(依赖),然后错误消失了。 It solved my issue.它解决了我的问题。

You can follow below steps:您可以按照以下步骤操作:

  1. Select the Java project(created one)选择Java项目(创建一个)

  2. Build Path构建路径

  3. Configure build path配置构建路径

  4. Java compiler Java编译器

  5. Change the compiler compliance level(I selected 1.8)更改编译器合规性级别(我选择了 1.8)

  6. Apply and Close.应用并关闭。

The solution for me was the POM specified an older version of ChromeDriverManager which was how chromeDriver was obtained in my build.我的解决方案是 POM 指定了旧版本的 ChromeDriverManager,这是在我的构建中获取 chromeDriver 的方式。 If you are specifying a location using setProperty this will of course not apply to you.如果您使用 setProperty 指定位置,这当然不适用于您。 But it helps to check with the version of the dependencies in your POM但它有助于检查 POM 中依赖项的版本

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

That did it!做到了!

Add Selenium Jar Files to ClassPath instead of ModulePath将 Selenium Jar 文件添加到 ClassPath 而不是 ModulePath在此处输入图片说明

ChromeDriver is one extra level nested in org.openqa.selenium package. ChromeDriver 是嵌套在 org.openqa.selenium 包中的一个额外级别。 Try with import statement尝试导入语句

org.openqa.selenium.chrome.*; org.openqa.selenium.chrome.*;

OR

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

The error says it all :错误说明了一切:

"ChromeDriver can't be resolved to a type"

While working with Selenium 3.x you have to mention the Key-Value pair 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.*;此外,根据最佳实践而不是import org.openqa.selenium.*; you have to mention the individual packages for import as follows :您必须提及要导入的各个包,如下所示:

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

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.首先从“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"); } }

Try to edit external jars in the ModulePath & Classpath .尝试在ModulePathClasspath编辑外部 jar。

See on the picture link -->见图片链接-->在此处输入图片说明

You need to attach jar files.您需要附加 jar 文件。 I was getting the exactly the same issue.我遇到了完全相同的问题。 you need to install "Selenium 3.4.0" jar files.您需要安装“Selenium 3.4.0”jar 文件。 Which contains lib folder of .jar files and "client-combined-3.4.0-nodeps" files.其中包含 .jar 文件和“client-combined-3.4.0-nodeps”文件的 lib 文件夹。 After you attach these jar files refresh the code by giving run command.附加这些 jar 文件后,通过给出运行命令刷新代码。

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

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.下载谷歌驱动程序并解压并保存路径并在代码中使用,现在运行代码。

if you are not able to add this statement如果您无法添加此语句

 import org.openqa.selenium.chrome.ChromeDriver;

the probable reason is you are using maven and 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 依赖来解决这个问题

This type of error occurs when you have added the external jars in the ModulePath.当您在 ModulePath 中添加外部 jars 时会发生这种类型的错误。 To resolve this issue, you can remove the external jars from the node "Modulepath".要解决此问题,您可以从节点“Modulepath”中删除外部 jar。 Select the node "Classpath" then add the external jars.选择节点“类路径”,然后添加外部 jar。 Review that all the jars are under the node "Classpath"查看所有 jars 都在节点“Classpath”下

When adding external jars, make sure you add the jars in the lib folder first before adding the ones in the main selenium-java folder.添加外部 jar 时,请确保先将 jar 添加到 lib 文件夹中,然后再将其添加到主 selenium-java 文件夹中。 This made the difference for me.这让我与众不同。

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

public class testdemo {

    public static void main(String[] args) {
        
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\Users\\victo\\OneDrive\\Documents\\driverchrome\\ChromeDriver.exe");
        
        WebDriver driver=new ChromeDriver();
        
        driver.get(" https:google.com");

I faced this error when using Latest Eclipse 2021-03 and Selenium 3我在使用最新的 Eclipse 2021-03 和 Selenium 3 时遇到了这个错误

To resolve this, add your Selenium standalone jar file in Classpath instead of Module path.要解决此问题,请在类路径而不是模块路径中添加您的 Selenium 独立 jar 文件。 also do not forgot to delete the existing Jar files in modulepath, apply and then apply&close.也不要忘记删除模块路径中现有的 Jar 文件,应用然后应用并关闭。

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

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