簡體   English   中英

我如何處理對話框窗口以單擊Selenium Webdriver中的按鈕

[英]How Can I handle dialog window to click on button in selenium webdriver

我試圖單擊對話框中的“確定”按鈕。 我如何找到xpath來找到按鈕並單擊以關閉窗口。 它不在任何iframe中。 這是我的代碼:

  `<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> <h1>Warning message</h1> <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> </a> <div class="clear"></div> <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> <span class="warningText" id="errorTextWarning"> Please Confirm to change warehouse <br><br> Do you want to continue? </span> </div> <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> <span class="warningText" id="errorTextWarning"> Please Confirm to change Customer <br><br> Do you want to continue? </span> </div> <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> </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> <h1>Warning message</h1> <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> </a> <div class="clear"></div> <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> <span class="warningText" id="errorTextWarning"> Please Confirm to change warehouse <br><br> Do you want to continue? </span> </div> <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> <span class="warningText" id="errorTextWarning"> Please Confirm to change Customer <br><br> Do you want to continue? </span> </div> <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> </div> </div> </div> </div> 

您應該能夠通過ID進行匹配:

//button[@id="warehouseOk"]

本文引用了一些有用的HTML XPath查詢示例。

我認為您要粘貼兩次相同的對話框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()定位器輕松定位所需的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM