2015-09-30 21 views
5

モデルダイアログボックスのコンテンツにアクセスしてボタンにアクセスしたい(はい、いいえ)。ここでSelenium Webdriverでブートストラップモーダルダイアログにアクセスできない

は、HTMLコードで次のようになります

<div class="modal-dialog"> 
<div class="modal-content"> 
    <div class="modal-header"><div class="bootstrap-dialog-header"> 
     <div class="bootstrap-dialog-close-button" style="display: none;"> 
      <button class="close">×</button> 
     </div> 
     <div class="bootstrap-dialog-title" id="e6adf6aa-dcbf-4fb8-9935-c083762f2812_title"> 
     Inactivate user 
     </div> 
    </div> 
</div> 
<div class="modal-body"> 
    <div class="bootstrap-dialog-body"> 
     <div class="bootstrap-dialog-message"> 
     Are you sure you want Inactivate user? 
     </div> 
    </div> 
</div> 
<div class="modal-footer"> 
    <div class="bootstrap-dialog-footer"> 
     <div class="bootstrap-dialog-footer-buttons"> 
      <button class="btn btn-default" id="868d2d8a-67f6-4308-a3c8-0246a5d4618c">Yes</button> 
      <button class="btn btn-default" id="23e4d027-ef32-4b58-b8b6-6f95bead2db4">No</button> 
     </div> 
    </div> 
</div> 

もう一つは、ボタンのIDが動的です。私はそれが役に立てば幸い

By.XPath("//div[@class='bootstrap-dialog-footer-buttons']//button[text()='Yes']")

:よう

答えて

5

私はそんなにグーグル後、このための解決策を見つけた...ここでは、この

//Switch to active element here in our case its model dialogue box. 
driver.switchTo().activeElement(); 

Thread.sleep(3000); 

// find the button which contains text "Yes" as we have dynamic id 
driver.findElement(By.xpath("//button[contains(text(),'Yes')]")).click(); 

のための簡単な解決策は、このコードは私の問題を解決しています。

1

代わりに、IDは、XPath を使用してみてください。

0

要素のIDがあらかじめ定義されていない場合は、CSSクラスを有効なセレクタとして使用できます。 もちろん、ページにそのクラスの要素が1つしかない場合は、それが当てはまります。

<button class="yes btn btn-default" id="868d2d8a-67f6-4308-a3c8-0246a5d4618c">Yes</button> 
<button class="no btn btn-default" id="23e4d027-ef32-4b58-b8b6-6f95bead2db4">No</button> 

、その後、あなたが使用して目的の要素に到達するために

button.btn-default.yes 

のようなシンプルなCSSセレクタを使用することができます。私は、あなたが別のCSSクラスでそれらの一つ一つをマークすることを示唆しているようになり、複数の要素については セレン。

関連する問題