1

HtmlUnitDriver を含む基本的なセレン スクリプトを実行できません。コードを実行しようとすると、次のエラーが発生します。

コード:

public class SampleUnitDriver  
{   
    public static void main(String[] args) throws Exception 
    {
        HtmlUnitDriver unitDriver = new HtmlUnitDriver(); 

        unitDriver.get("http://google.com");

        System.out.println("Title of the page is -> " + unitDriver.getTitle());             

        Thread.sleep(3000L);
        WebElement searchBox = unitDriver.findElement(By.className("gsfi"));

        searchBox.sendKeys("Selenium");             

        WebElement button = unitDriver.findElement(By.name("gbqfba"));

        button.click();

        System.out.println("Title of the page is -> " + unitDriver.getTitle());

    } 
}

エラー:

    Title of the page is -> Google
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Returned node was not a DOM element
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'user-PC', ip: '192.168.1.52', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: driver.version: SampleUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByCssSelector(HtmlUnitDriver.java:1060)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByClassName(HtmlUnitDriver.java:1032)
    at org.openqa.selenium.By$ByClassName.findElement(By.java:391)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
    at com.digitalmqc.automation.action.SampleUnitDriver.main(SampleUnitDriver.java:16)
4

1 に答える 1

0

入力ボックスのクラス名gsfiは JavaScript によって生成されるため、2 つのオプションがあります。

簡単なもの: ID を使用して選択するだけですlst-ib

ベター ワン: 要素が完全にインタラクティブになるまで待ちます。この方が優れている理由は、javascript が役割を果たすボックスにテキストを入力しようとしているからです。前者のオプションでは、予期しないエラーが発生する場合があります。

于 2016-07-07T15:06:15.663 に答える