2016-12-01 6 views
1

JAVAのjavascriptとseleniumを使用してフォームのアクション属性を置き換えようとしましたが、動作させることができません。JAVAでJavascriptとSeleniumを使用してフォームアクションを変更する方法

これは、サイトのスニペットのHTMLです:

</div> 
<div class="contionue-shopping-wrapper"> 
<form class="co-formcontinueshopping" action="https://www.yahoo.com" method="post" id="dwfrm_cart_d0jilurhcxpm"> 
<fieldset> 
<button class="rbk-button-red button-primary bp-black right" type="submit" value="Continue Shopping" name="dwfrm_cart_continueShopping"> 
<span>Continue Shopping</span> 
</button> 
</fieldset> 
</form> 
</div> 

これは私が

String link = "https://www.google.com" 
((JavascriptExecutor)atc).executeScript("document.getElementsByClassName('co-formcontinueshopping')[0].action="+link); 

上記の動作を変更しようとする試みでJAVAでやったものです(注意:ATCは、セレン、クロムでありますwebdriverとサイトはこの段階で読み込まれます)

EDIT:私は次のjavascriptを上に変換すればうまくいくと思いますか?

実際に例えば、このコードは動作しますが、動作するはず
document.getElementById(document.querySelector("[id^='dwfrm_cart']").id).action = url})() 
+0

ログ 'document.getElementsByClassName( '共同formcontinueshopping')[0]かどうかを確認するために、コンソールに.action'セレクタは 'console.log(document.getElementsByClassName( 'co-formcontinueshopping')[0] .action)' –

+0

を使って動作します。ありがとう – hello

+0

問題はあなたのJavaScriptに '' 'がないことです。ですから、String link = "'https://www.google.com'"と書くか、 '' JavascriptExecutor'を介してJavaScriptを '' JavascriptExecutor''を使って実行する必要があります。 –

答えて

1

@Test 
public void test() throws InterruptedException{ 

    driver.get("https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/Cart-Show"); 
    JavascriptExecutor js=(JavascriptExecutor) driver; 

    String link = "'http://www.example.com'"; // notice the extra ' inside the " 

    //log the current value of 'action' 
    js.executeScript("console.log(document.getElementsByClassName('co-formcontinueshopping')[0].action)"); 

    //change the value to the value hold by our link 
    js.executeScript("document.getElementsByClassName('co-formcontinueshopping')[0].action=" + link); 

    //log 'action' again 
    js.executeScript("console.log(document.getElementsByClassName('co-formcontinueshopping')[0].action)"); 

    driver.quit(); 
} 

をし、ブラウザのコンソールの出力は次のとおりです。

https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/Cart-Show/C1360474325 
http://www.example.com/ 

ので、アプローチは唯一、権利でありますあなたの状況で何かが欠けている必要があります:

  • document.getElementsByClassName('co-formcontinueshopping')が何か間違っている場合、これにはいくつかの原因があります。サイトはiframeです。正しい要素が返されているかどうかを確認します。たとえば、コンソールにログを記録します。
  • document.getElementsByClassName('co-formcontinueshopping')[0]が期待どおりに何かを返している場合は、それもチェックしてください。
  • あなたは何か他に行動を設定しています。私は''を使用しました。
  • など別のブラウザとして何か他のもの、(私はMac上でクローム54.0.2840.98を使用)
+0

私はこのjavascriptを代わりに使用しました:console.log(document.getElementById(document.querySelector( "[id^= 'dwfrm_cart']")。id).action)そしてそれはうまくいきました。ありがとうございます – hello

+0

と私は以前に作成した文字列変数にアクションを設定することができます。 – hello

+0

私はコードをここで働いている、私は私の答えを更新します、1 sec –

関連する問題