3

アクセスしようとしているページの要素を見つけるために 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)
4

2 に答える 2

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());
}
}
于 2013-06-06T20:18:42.033 に答える