如何处理对话窗口以点击selenium webdriver中的按钮

时间:2016-10-19 14:44:51

标签: java selenium-webdriver junit4

我试图点击对话框中的确定按钮。如何找到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>

2 个答案:

答案 0 :(得分:1)

您应该可以通过ID匹配:

//button[@id="warehouseOk"]

This article引用了一些HTML XPath查询的有用示例。

答案 1 :(得分: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()定位器轻松找到所需的元素。