2016-10-19 77 views
0

我想單擊對話框中的確定按鈕。我如何找到xpath來查找按鈕並單擊關閉窗口。它不在任何Iframe中。 這裏是我的代碼:如何處理對話框窗口點擊硒webdriver中的按鈕

`<div id="YesNoDialogBox" class="dialogbox errorDialog firepath- 
 
matching-node" style="display: block;"> 
 
<div class="errWarnMsg"> 
 
<div class="popupHeaderContainer"> 
 
<div class="imgicon"><img src="/web/images/error.png" class="vMiddle" 
 
tabindex=""></div> 
 
\t <h1>Warning message</h1> 
 
\t <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> 
 
\t <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> 
 
\t </a> 
 
\t <div class="clear"></div> \t 
 
\t <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> 
 
\t \t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t Please Confirm to change warehouse 
 
\t \t \t \t <br><br> 
 
    \t \t \t \t \t Do you want to continue? 
 
\t \t \t \t </span> 
 
\t </div> 
 
\t <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> 
 
\t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t Please Confirm to change Customer 
 
\t \t \t <br><br> 
 
    \t \t \t \t Do you want to continue? 
 
\t \t </span> \t \t 
 
\t </div> 
 
\t <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> 
 
\t \t <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> 
 
\t \t 
 
\t \t <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> 
 
\t </div> 
 
</div> 
 
</div> 
 
</div>`<div id="YesNoDialogBox" class="dialogbox errorDialog firepath- 
 
    matching-node" style="display: block;"> 
 
    <div class="errWarnMsg"> 
 
    <div class="popupHeaderContainer"> 
 
     <div class="imgicon"><img src="/web/images/error.png" class="vMiddle" tabindex=""></div> 
 
\t \t <h1>Warning message</h1> 
 
\t \t <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> 
 
\t \t <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> 
 
\t \t </a> 
 
\t \t <div class="clear"></div> \t 
 
\t \t <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> 
 
\t \t \t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t \t Please Confirm to change warehouse 
 
\t \t \t \t \t <br><br> 
 
    \t \t \t \t \t Do you want to continue? 
 
\t \t \t \t \t </span> 
 
\t \t </div> 
 
\t \t <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> 
 
\t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t Please Confirm to change Customer 
 
\t \t \t \t <br><br> 
 
    \t \t \t \t Do you want to continue? 
 
\t \t \t </span> \t \t 
 
\t \t </div> 
 
\t \t <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> 
 
\t \t \t <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> 
 
    \t \t 
 
    \t \t <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> 
 
\t \t </div> 
 
    </div> 
 
</div> 
 
</div>

+0

在提供的HTML中有多個yes/no按鈕,您要查找哪一個按鈕? –

回答

1

我想你是粘貼相同的對話框窗口HTML兩次。假設只有一個對話窗口。

你應該嘗試使用WebDriverWait等到對話窗口可見,使點擊下面想要的元素: -

WebDriverWait wait = new WebDriverWait(driver, 10); 
  • 如果你想點擊Yes按鈕嘗試如下: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("warehouseOk"))).click(); 
    
  • 如果你想點擊No按鈕試試: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("cancelDialog"))).click(); 
    
  • 如果你想點擊Close按鈕嘗試如下: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("close"))).click(); 
    

注意: - 無需使用xpath定位器,所需的元素可以找到很容易使用By.id()定位器。

1

您應該能夠通過ID匹配:

//button[@id="warehouseOk"] 

This article引用HTML XPath查詢的一些有用的例子。

相關問題