【问题标题】:How to install Selenium selenium-java-3.141.59 library on Eclipse or Netbeans?如何在 Eclipse 或 Netbeans 上安装 Selenium selenium-java-3.141.59 库?
【发布时间】:2021-01-07 22:38:27
【问题描述】:

我使用的是 JDK 1.15.0.1 和 Java 版本 8。 结合 here 的 Selenium 库,我也一直在使用来自 here 的 Chrome 驱动程序

我已经尝试了所有可以在网上找到的教程,但不知道如何成功安装最新的 Selenium WebDriver 库。 我主要关注this tutorial(以及类似的),但在第一次尝试无济于事后,每次尝试都会有细微的变化。 Eclipse 在将鼠标悬停在测试代码中的相关类用法上时,通过声明它“找不到符号 [来自 selenium 库的类]”不断指示我的程序没有注册库或正确导入包。

我在 Netbeans IDE 中使用相同的文件尝试了类似的方法和类似的教程,结果相同。

这是我一直在使用的测试代码:

package internet.bot;

/**
 *
 * @author Owner
 */
public class InternetBot {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //for the web driver
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Owner\\Documents\\GitHub\\Internet-Bot\\Internet Bot\\src\\chromedriver_win32\\chromedriver_win32\\chromedriver.exe");
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new ChromeDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}

/*Sources
Example Google Search code:                             https://stackoverflow.com/questions/4640972/how-to-have-java-application-interact-with-a-website

*/

编辑:我想我还应该提一下,我下载的 selenium 库没有教程 referenced 中显示的那么多 .jar 文件。

【问题讨论】:

  • 您只需将必要的 jar 文件添加到您的项目中……这些应该会在您添加后显示在您的项目中的“libs”下。然后添加您的导入。在Netbeans中,我认为当光标位于红线文本上时它是alt-enter ...(它应该建议添加一个导入行并自动添加它......)没有什么要安装......你只是在添加依赖项。
  • @pcalkins 我认为这可能是正确的!我不知道为什么它以前不允许我导入包,但似乎当我执行 alt+enter 时它让我毫无困难地导入它们!现在唯一的问题是它似乎没有将 chromedriver.exe 文件识别为可执行文件。我对此有点陌生,可以在这里回答还是我应该为此提出一个新问题?
  • 没关系 @frianH 帮我解决了剩下的问题,不过谢谢你的帮助!

标签: java selenium selenium-webdriver java-8 selenium-chromedriver


【解决方案1】:

*Eclipse

我正在尝试建议使用 Maven 配置您的项目,这样更容易。

创建项目后,右键单击它。

选择配置 > 转换为 Maven 项目 > 完成

它会自动生成pom.xml文件。

注意:请确保您已连接到互联网。

之后,请根据您想要的版本在关闭</build>和关闭</project>之间添加selenium依赖项,如下所示:

pom.xml

  ....
  ....
  </build>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
  </dependencies>
  
</project>

保存,项目会自动构建,稍等片刻让工作区完成配置。

【讨论】:

  • 我刚刚尝试过,它给了我同样的问题,这就是我所做的:1 按照原始问题中链接的教程的步骤 4 进行操作 2 按照您的说明进行操作 3 保存所有文件,然后等待小 4 尝试导入软件包,因为提供的一些快速修复建议弹出窗口无济于事 5 重新启动 eclipse 无济于事 我检查并仔细检查以确保我做的事情正确。
  • 在 Eclipse 中与以前相同,但@pcalkins 的回答似乎已经让它在 Netbeans 中工作,只是它似乎没有将 chromedriver.exe 文件识别为驱动程序可执行文件,返回read "main" java.lang.IllegalStateException: The driver executable does not exist: C:\Users\... 我对此有点陌生;可以在这里回答还是我应该提出一个新问题?
  • @Isaac 你确定chromedriver.exe 在那个路径中吗?或者尝试将路径更改为更简单的路径,如C:\\Downloads\\chromedriver.exe,请将二进制放在此路径中。
【解决方案2】:

我认为如果您可以进行 Mavan 项目,升级/降级版本和插件会更好

【讨论】:

  • 我试过了,可惜没用
猜你喜欢
  • 2021-08-25
  • 2021-12-18
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 2016-09-06
相关资源
最近更新 更多