2016-07-07 6 views
0

私は次の問題に直面しています.PhantomJSドライバからserenityフレームワークを使用してjavascriptを実行しています。phantomJSドライバを使用してjavascriptを実行中に問題が発生しました

(PhantomJSDriver) driver).executeScript("document.getElementsById('whereOut')") 

エラー:

{"errorMessage":"'undefined' is not a function (evaluating 'document.getElementsById('whereOut')')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"69","Content-Type":"application/json; charset=utf-8","Host":"localhost:33819","User-Agent":"Apache-HttpClient/4.3 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"document.getElementsById(\u0027whereOut\u0027)\",\"args\":[]}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/b0246730-4435-11e6-b64c-7bf1e13a7577/execute"}} Command duration or timeout: 278 milliseconds

も試み次のコードを下記のようにコード上

((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut')") 

私は戻り値を追加してもNULL値を返す:

((JavascriptExecutor) driver).executeScript("return document.activeElement") 

ヌル値を返します。

誰もがファントムjsを使用して特定の要素のcssを変更しようとしましたか?

((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut').className=''") 

私は何を考え出したことは、同じ要素がFirefoxブラウザで純粋なセレンのコードを使用して検索可能である一方で、コードの線の上実行している間、私はjavascript実行を使用して要素を取得していないということです。

P.S.私は平静と一緒にphantomJS 1.9.7のバージョンを使用しています:1.0.56。

+0

しかし、JavascriptExecutorはPhaによって実装されていませんntomJSDriver、どうすればこれはうまくいくのでしょうか? –

答えて

0

次のようにcallbackexecuteAsyncScriptを使用してみてください: -

String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];var element = document.getElementById('whereOut');element.setAttribute('class', 'your new class name');callback(element.getAttribute('class'));"); 

編集: - あなたが望む要素のclassNameを変更したい場合は、それは願っています

WebElement element = driver.findElement(By.id("whereOut")); 
String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];arguments[0].setAttribute('class', arguments[1]);callback(arguments[0].getAttribute('class'));", element, "your new class name"); 

以下のように .setAttributeを使用してみてください助けてください... :)

+0

このエラーが発生しました[[email protected] - >不明なロケータ] –

+0

@BilboBagginsまず、ブラウザのコンソールで 'document.getElementById( 'whereOut')'が何かを返すかどうかを確認してください –

+0

もし私がセレンを使って同じことをすれば、WebElement element = driver.findElementBy(By.id( "whereOut"));またはdriver.findElementBy(By.cssClassName( "whereOut")); (cssClass名のメソッドの名前が正しくない可能性があります)、私は希望のdivタグを取得しますが、上記のWebElementインスタンスを使用してそのCSSを削除することはできません。奇妙なのは、同じことが純粋なセレンコードでうまく動作するためですが、セレンティBDDフレームワークを使用する必要があります。 –

関連する問題