2

Java プログラムで Selenium firefox webdriver を使用しています。テキストボックスに文字列を入力して送信をクリックしようとしています。driver.findElement ただし、テキストボックスは非表示の要素にネストされているため、id、xpath、または cssを使用してアクセスできません。(ただし、Web ページでは表示されます)。私は得るNoSuchElementException

Web ページの html は次のとおりです。

<div id="mainContent" style="overflow:hidden;">
<!-- content left here -->
<div id="contentLeft">
<script type="text/javascript" src="/enterprisesolutions/staticcontent/includes/scripts/QuickTask.js"/>
<script type="text/javascript" src="/enterprisesolutions/Includes/Javascript/date-picker.js"/>
<div id="quickTask">
<h2 class="boxContentHead">
<div id="qtBoxContent" class="boxContent quickTaskBack">
<!-- Accounts and Maintenance -->
<div id="qt_accounts_and_maintenance" class="qtStack" style="display:block">
<!-- Orders -->
<div id="qt_orders" class="qtStack" style="display:block">
<!-- Invoice -->
<div id="qt_invoices" class="qtStack" style="display:block">
<div class="qtIcon invoices"/>
<div class="qtContent">
<div class="qtTitle">Invoices</div>
<div class="qtForm">
<div class="qtLabel qtLabelInvoices">
<div class="qtLeftInvoices">
<div class="firstField">
<div class="qtFormLabel">Account/Phone/Circuit</div>
<div>
<input id="qt_invoices_text1" type="text" value=""/>
</div>
</div>
<div class="secondField">
</div>
<div class="qtRight">
</div>
</div>
</div>
<!-- Repairs -->
<div id="qt_repairs" class="qtStack" style="display:block">
<!-- Lottery Reporting -->
<!-- Reporting -->
<form id="quickTaskForm" name="quickTaskForm" action="/enterprisesolutions/global/quickTaskAction.do" method="post">
<!-- QT No Access -->
<!-- QT Request Access -->
<div id="qt_request_access" class="qtStackAccess" style="display:block">
</div>
<div id="qtBottom" class="quickTaskBack" style="background: none repeat scroll 0% 0% transparent;">
</div>
<!-- start promo and customized pod -->
<div id="promo_msg">
<!-- end promo and customized pod -->
</div>
<!-- end content left-->
<div id="contentRight" style="height: 845px;">
<!-- end content right -->
</div>

id = mainContent の div には「overflow:hidden」のスタイルがあり、Web ドライバーが要素を見つけられないと推測しています。

次のことを試しましたが、フィールドが非表示driver.findElement(By.cssSelector("#qt_invoices_text1")).sendKeys("hi"); driver.findElement(By.id("qt_invoices_text1")).sendKeys("hi"); になっているため (そして正しい xpath を使用しているため)、機能しません。

JavascriptExecutor を使用してこれを回避する方法があると聞きましたが、まだ見つけていません。

編集:スタックトレースは次のとおりです。

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#qt_invoices_text1"}
Command duration or timeout: 20.18 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_21'
Session ID: 334122ae-6eab-4036-96ca-1183c9129b51
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=21.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at DownloadInvoice.read(DownloadInvoice.java:282)
    at DownloadInvoice.main(DownloadInvoice.java:406)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"css selector","selector":"#qt_invoices_text1"}
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_21'
Driver info: driver.version: unknown

そして、これが私のJavaコードです(関係のない種類ですが、人々は尋ね続けました)(より大きなプロジェクトの一部であり、これが唯一の関連コードです):

driver.get(sheet.getCell(j+1,i).getContents());
                driver.findElement(By.id("UserId")).click();
                driver.findElement(By.id("UserId")).clear();
                driver.findElement(By.id("UserId")).sendKeys(sheet.getCell(j+2,i).getContents());
                Thread.sleep(1000);
                driver.findElement(By.id("PASSWORD")).click();
                driver.findElement(By.id("PASSWORD")).clear();                                
                driver.findElement(By.id("PASSWORD")).sendKeys(sheet.getCell(j+3,i).getContents());
                Thread.sleep(5000);
                driver.findElement(By.id("submit")).click();
                Thread.sleep(2000);
            driver.findElement(By.cssSelector("#qt_invoices_text1")).sendKeys("hi");
driver.findElement(By.id("submit")).click();

編集: Selenium IDE で遊んSelectFrame _shellbodyだところ、コマンドを入力する前にフレームが選択されましたが、エクスポートすると、これがコード内のコメントとして表示されます。 // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | _shellbody | ]]

どんな助けでも大歓迎です!

4

3 に答える 3

5

を使用してみてくださいWebDriverWait。可視性について話すのは意味がありません。なぜなら、例外は NoSuchElementExceptionであり、要素がそこにないことを意味するからです。この例外を解決した後、可視性について話し合うことができます。

driver.switchTo().frame(0); // use this if it's in frame and you don't know how to locate it
WebElement input = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("qt_invoices_text1")));

また、注意してください。使用しないでください。WebDriverWaitThread.Sleep()を使用してください。

于 2013-06-26T21:18:40.737 に答える
2

十分に待ってもうまくいかない場合は、JSExecutor を試してください。

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("document.getElementById(div 要素 id).click()");

これはうまくいくはずです.....

于 2013-06-29T03:45:33.067 に答える
0

スクリプトを実行している間、アプリケーションが非表示の要素をロードするのに時間がかかる間、セレンは一歩先に進み、要素を見つけようとしますが、この要素がまだロードされていないため、要素が見つからないため、一度チェックして例外をスローします。Thread.Sleep()を利用してWebDriverWaitを使用します。

于 2013-06-27T10:00:28.277 に答える