gpt4 book ai didi

xpath - HtmlUnitDriver 无法定位节点

转载 作者:行者123 更新时间:2023-12-03 15:48:13 24 4
gpt4 key购买 nike

我无法让 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());
}
}

这是堆栈跟踪:

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)

最佳答案

我想通了。这只是我等待元素加载的问题。这是代码。

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());
}
}

关于xpath - HtmlUnitDriver 无法定位节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16968868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com