我无法让HtmlUnitDriver在我尝试访问的页面上查找元素。另一方面,WebDriver工作正常。两者都使用相同的方法。 这是我的代码:
import java.util.logging.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;
public class Main {
public static void main(String[]args)
{
FirefoxDriver driver2=new FirefoxDriver();
HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_10);
driver.setJavascriptEnabled(true);
Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
runTest(driver2); //works
runTest(driver); //does not work
}
public static void runTest(WebDriver driver)
{
driver.get("http://10.3.1.164");
WebElement elem=driver.findElement(By.xpath("//td[2]/input"));
assert elem.isDisplayed();
System.out.println(driver.getTitle());
}
}
这是stacktrace:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //td[2]/input
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_10'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:805)
at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1247)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:987)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1244)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:393)
at Main.runTest(Main.java:28)
at Main.main(Main.java:22)
答案 0 :(得分:5)
我明白了。这只是我等待元素加载的问题。这是代码。
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
public class Main {
public static void main(String[]args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
{
FirefoxDriver driver2=new FirefoxDriver();
HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.setJavascriptEnabled(true);
Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
runTest(driver2); //works
runTest(driver); //does not work
}
public static void runTest(WebDriver driver)
{
driver.get("http://10.3.1.164");
WebElement elem=(new WebDriverWait(driver, 10)) //added this line
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[2]/input")));
System.out.println(elem.getAttribute("id"));
assert elem.isDisplayed();
System.out.println(driver.getTitle());
}
}
答案 1 :(得分:0)
您只需要照顾timeOut
。我刚刚为今天的lalenium-server-standalone.jar运行了一个简单的HTMLUnitDriver测试,它是3.8.1
这是我的java代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HD_HTMLUnitTest {
public static void main(String[] args) throws InterruptedException {
// Creating a new instance of the HTML unit driver
//System.getProperties().put("http.proxyHost", "www-proxy.us.oracle.com");
//System.getProperties().put("http.proxyPort", "80");
HtmlUnitDriver unitDriver = new HtmlUnitDriver();
//unitDriver.setJavascriptEnabled(true);
unitDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
unitDriver.get("https://www.google.co.in");
System.out.println("Title of the page is -> " + unitDriver.getTitle());
unitDriver.findElement(By.name("q")).sendKeys("HTMLUnit through Selenium");
//searchBox.sendKeys("Selenium");
WebElement button = unitDriver.findElement(By.name("btnK"));
button.click();
System.out.println("Title of the page is -> " + unitDriver.getTitle());
unitDriver.quit();
}
}