【发布时间】:2018-11-10 13:00:54
【问题描述】:
我知道这个问题已经被问了一千次了,但我仍然无法完全理解问题的重点,尤其是在我的情况下。所以,我有一个依赖 TestNG 和 Selenium Java 库的简单项目,并且我在全局范围内安装了这些库,所以我的项目只是从“全局”范围导入它们。
所以要解决这个问题,我应该将该全局文件夹添加到我的类路径中吗?或者这从一开始就不是正确的,我不应该在项目中全局使用库?
C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java>javac GoogleSearchTest.java
GoogleSearchTest.java:1: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
^
GoogleSearchTest.java:2: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
GoogleSearchTest.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
^
GoogleSearchTest.java:4: error: package org.openqa.selenium.chrome does not exist
import org.openqa.selenium.chrome.ChromeDriver;
^
GoogleSearchTest.java:5: error: package org.testng.annotations does not exist
import org.testng.annotations.BeforeClass;
^
GoogleSearchTest.java:6: error: package org.testng.annotations does not exist
import org.testng.annotations.Parameters;
^
GoogleSearchTest.java:7: error: package org.testng.annotations does not exist
import org.testng.annotations.Test;
^
GoogleSearchTest.java:12: error: cannot find symbol
private static WebDriver driver;
^
symbol: class WebDriver
location: class GoogleSearchTest
GoogleSearchTest.java:14: error: cannot find symbol
@BeforeClass
^
symbol: class BeforeClass
location: class GoogleSearchTest
GoogleSearchTest.java:23: error: cannot find symbol
@Test
^
symbol: class Test
location: class GoogleSearchTest
GoogleSearchTest.java:24: error: cannot find symbol
@Parameters("queryText")
^
symbol: class Parameters
location: class GoogleSearchTest
GoogleSearchTest.java:17: error: cannot find symbol
driver = new ChromeDriver();
^
symbol: class ChromeDriver
location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
^
symbol: class WebElement
location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
^
symbol: variable By
location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
WebElement searchButton = driver.findElement(By.name("btnK"));
^
symbol: class WebElement
location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
WebElement searchButton = driver.findElement(By.name("btnK"));
^
symbol: variable By
location: class GoogleSearchTest
16 errors
GoogleSearchTest.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class GoogleSearchTest {
private static WebDriver driver;
@BeforeClass
public void setup () {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
}
@Test
@Parameters("queryText")
public void doSearch(String queryText) {
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
searchField.sendKeys(queryText);
WebElement searchButton = driver.findElement(By.name("btnK"));
searchButton.click();
}
}
【问题讨论】:
-
“全球”是什么意思?
-
在“项目结构”窗口中,“平台设置”中有“全局库”。我在开始项目之前从那里安装了库,因此所有导入都可用,并且这些库出现在“项目设置”的“库”中。并且还在“模块”->“依赖项”中。我的意思是库不在我的项目结构中,它们在一些单独的 IntelliJ IDEA 文件夹中。
-
你能显示代码和你在构建时遇到的异常吗?另外,老实说,我建议您使用 Gradle 或 Maven 之类的东西来管理您的依赖项和构建过程,而不是严重依赖 IDE 工具。 javac 是用于编译 JDK 本身(不是 IDE)提供的代码的命令,而 javac 不知道类路径(IDE 知道,这就是为什么您可以从 IDE 运行按钮运行项目的原因)。
-
我用我试图编译的异常和类更新了问题。我还在项目结构的 lib 文件夹中移动了我的依赖库。