3

Web ページ上の要素を検索するために Selenium Web Driver HtmlUnitDriver を使用しています。ページ ソースに表示されている要素のみを検索できます。ただし、これらの要素の詳細は、Internet Explorer Developer Tools (F12) を使用して確認できます。Selenium を使用してこれらの詳細 (id/name/XPath) を使用すると、org.openqa.selenium.NoSuchElementException. 私が書いたコードは次のとおりです。

public static void main(String[] args) {

        WebDriver driver = new HtmlUnitDriver(){
            @Override
            protected WebClient modifyWebClient(WebClient client) {

                DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
                credentialsProvider.addNTLMCredentials("username", "password", null, -1, "localhost", "domain");
                client.setCredentialsProvider(credentialsProvider);
                return client;
            }
        };

        driver.get("URL");

        System.out.println(driver.getPageSource());
        WebElement myDynamicElement = (new WebDriverWait(driver, 20)).until(ExpectedConditions.presenceOfElementLocated(By.id("ppm_timesheet")));

    }

ページソース

<?xml version="1.0" encoding="UTF-8"?>
    <html>
    <head>
    <meta name="application-name" content="CONTENT"/>
    <meta name="upk-namespace" content="en"/>
    <meta http-equiv="X-UA-Compatible" content="IE=8"/>
    <link rel="SHORTCUT ICON" href="ui/uitk/images/shortcut.ico"/>
    <title>
      Title
    </title>
    <link rel="stylesheet" href="ui/uitk/css/min.css"/>
    <script type="text/javascript" src="ui/ext/ext.min.js">
    </script>
    <script type="text/javascript">
    //<![CDATA[
    require( {baseUrl: "ui"},[ "uif/js/main.min" ] );
    //]]>
    </script>
  </head>
  <body>
    <div id="ppm">
    </div>
  </body>
</html>

「ppm」という要素を検索すると、ページソースに存在するものが成功します。要素「ppm_timesheet」を検索すると、おそらくこの要素がページ ソースに存在しないため、次の例外が発生します。しかし、Internet Explorer で F12 を押して、開発者ツールでこの要素を選択すると、この要素が HTML に表示されます。

<button title="Time" class="class" id="ppm_timesheet" type="submit" jQuery171013988872804261454="13" alt="Time">

完全な例外は次のとおりです。

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1 build 7601 Service Pack 1', java.version: '1.6.0'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:220)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:188)
    at com.auto.Automation.main(Automation.java:32)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: ppm_timesheet
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.8.0', revision: '14056', time: '2011-10-06 12:41:26'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1 build 7601 Service Pack 1', java.version: '1.6.0'
Driver info: driver.version: HtmlUnitDriver
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:685)
    at org.openqa.selenium.By$ById.findElement(By.java:210)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1222)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:969)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1219)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:396)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:289)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:287)
    at org.openqa.selenium.support.ui.ExpectedConditions$3.apply(ExpectedConditions.java:86)
    at org.openqa.selenium.support.ui.ExpectedConditions$3.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:173)

一部の要素がクライアント側で生成され、ビュー ソースに表示されないことを読みました。しかし、これは、View Source にない要素にはまったくアクセスできないことを意味しますか。

要素がロードされるのを 30 秒間待ってみました。しかし、それでも同じエラーが発生します。

私はたくさんグーグルで検索しましたが、これを理解することができませんでした.

4

2 に答える 2

0

探している要素がページ ソースにないという事実は問題になりません。WebDriver API は、要素が JavaScript 経由で追加される場合など、動的ページのサポートを明示的に追加するために導入されました。

ただし、要素が DOM 内にあるだけでは十分ではなく、要素が表示される必要もあります。WebDriver は、ユーザーが実行できる対話のみを許可することを意味します。そのため、要素が表示されていない場合、たとえば、WebDriver を介して要素をクリックすることはできません。

于 2014-06-05T09:47:54.880 に答える