【发布时间】: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