蚂蚁+;javac+;WebDriver:“;找不到符号:变量为;

蚂蚁+;javac+;WebDriver:“;找不到符号:变量为;,java,ant,webdriver,selenium-webdriver,javac,Java,Ant,Webdriver,Selenium Webdriver,Javac,我有以下TestBase.java: package com.example.tests; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.*; import org.openqa.selenium.firefox.*; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import

我有以下TestBase.java:

package com.example.tests;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

public class TestBase {

  protected WebDriver driver;

  @BeforeMethod
  @Parameters("hubAddress")
  public void startDriver(String hubAddress) throws MalformedURLException {
    driver = new RemoteWebDriver(new URL(hubAddress), DesiredCapabilities.firefox());
  }

  @AfterMethod
  public void stopDriver() {
    driver.quit();
    driver = null;
  }

}
以及以下扩展TestBase的Test5.java:

package com.example.tests;
import org.testng.annotations.Test;
import java.lang.Thread;

@Test
public class Test5 extends TestBase {

  public void test5() {

    driver.get("https://10.20.44.16/main/");


    try {
        Thread.sleep(20000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    driver.findElement(By.id("loginUserName")).sendKeys("User5");


  }

}
我的Ant配置(build.xml):

所以,我不明白这个错误的原因。上面的命令

driver.get("https://10.20.44.16/main/");
。。。工作正常,所以我想类路径一切正常

有什么想法吗

谢谢,
Racoon

您需要导入org.openqa.selenium.By。

您需要导入org.openqa.selenium.By在Test5.Java中。

不过,您需要在Test5.Java中导入它。谢谢,它可以工作!尽管如此,我还是不明白为什么我必须完全为selenium.By而不是为selenium.WebDriver做这件事。在Test5中,您不使用名称
WebDriver
,只使用
driver
(这是继承的)。间接使用类型是好的;导入与名称有关。顺便说一句,一个好的IDE(IntelliJ、Eclipse等)将为您处理所有的导入-这非常有用,因为否则它们会非常乏味。不过,您需要在Test5中导入它。谢谢,它可以工作!尽管如此,我还是不明白为什么我必须完全为selenium.By而不是为selenium.WebDriver做这件事。在Test5中,您不使用名称
WebDriver
,只使用
driver
(这是继承的)。间接使用类型是好的;导入与名称有关。顺便说一句,一个好的IDE(IntelliJ、Eclipse等)将为您处理所有导入-这非常有用,因为否则它们会非常乏味。
[javac] C:\LoadTest\src\com\example\tests\Test5.java:21: cannot find symbol
[javac] symbol  : variable By
driver.get("https://10.20.44.16/main/");