我如何处理对话框 window 以单击 selenium webdriver 中的按钮
How Can I handle dialog window to click on button in selenium webdriver
我正在尝试单击对话框中的“确定”按钮。我怎样才能找到xpath找到按钮并点击关闭window。它不在任何 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"]
This article 引用了一些有用的 HTML XPath 查询示例。
我认为您将同一个对话框 window HTML 粘贴了两次。假设只有一个对话框 window.
您应该尝试使用 WebDriverWait
等到对话框 window 可见并允许单击所需的元素,如下所示:-
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()
定位器轻松定位所需元素。
我正在尝试单击对话框中的“确定”按钮。我怎样才能找到xpath找到按钮并点击关闭window。它不在任何 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"]
This article 引用了一些有用的 HTML XPath 查询示例。
我认为您将同一个对话框 window HTML 粘贴了两次。假设只有一个对话框 window.
您应该尝试使用 WebDriverWait
等到对话框 window 可见并允许单击所需的元素,如下所示:-
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()
定位器轻松定位所需元素。