英文:
How do i use if condition to click on particular button if popup appears in Selenium
问题
以下是翻译好的部分:
我有一个情景,弹出窗口可能会出现,也可能不会,这取决于所选的筛选选项。如果它出现了,我必须点击“继续”按钮。请查看附带的屏幕截图。
这是代码:
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="DisplayBCWarningPopUpId" aria-labelledby="ui-id-6" style="position: absolute; height: auto; width: 300px; top: 81px; left: 837px; display: block;" xpath="1">
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"></div>
<div id="DisplayBCWarningPopUpId" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 92px; max-height: none; height: auto;">
<div id="ApplyFiltersModal">
<br>
这将覆盖以下应用的筛选器。将鼠标悬停在药丸上以查看内容更改。您是否要继续?
<table></table>
<div style="clear:both;"></div>
<div class="checkbox"></div>
<div class="btn-delete-custom-dashboard" style="padding-top:15px;">
<input type="button" value="取消" id="btnCancelOverrideBC" name="WidgetGenericButtonContainer" buttonenabled="true" class="button-layout WidgetGenericButtonEnabled">
<script></script>
<input type="button" value="继续" id="btnOkOverrideBC" name="WidgetGenericButtonContainer" buttonenabled="true" class="button-layout WidgetGenericButtonEnabled">
<script></script>
</div>
</div>
<script></script>
</div>
</div>
当所有筛选器都被选择后,点击应用按钮,然后如果弹出窗口显示,尝试点击“继续”按钮。我尝试的是:
if(driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).isDisplayed()) {
driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).click();
}
英文:
I have a scenario where popup may appear or may not appear depend on filter options selected. If its appear i do have to click on 'Proceed' button. Please find attached screenshot.[https://i.stack.imgur.com/B0tN9.png]
Here is the code:
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="DisplayBCWarningPopUpId" aria-labelledby="ui-id-6" style="position: absolute; height: auto; width: 300px; top: 81px; left: 837px; display: block;" xpath="1">
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"></div>
<div id="DisplayBCWarningPopUpId" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 92px; max-height: none; height: auto;">
<div id="ApplyFiltersModal">
<br>
This will override the following applied filters. Hover over pills to see content changes. Do you wish to proceed?
<table></table>
<div style="clear:both;"></div>
<div class="checkbox"></div>
<div class="btn-delete-custom-dashboard" style="padding-top:15px;">
<input type="button" value="Cancel" id="btnCancelOverrideBC" name="WidgetGenericButtonContainer" buttonenabled="true" class="button-layout WidgetGenericButtonEnabled">
<script></script>
<input type="button" value="Proceed" id="btnOkOverrideBC" name="WidgetGenericButtonContainer" buttonenabled="true" class="button-layout WidgetGenericButtonEnabled">
<script></script>
</div>
</div>
<script></script>
</div>
Once all filters get selected clicking on apply button and then if Popup get displays trying to click on button 'Proceed'.
What i tried is
if(driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).isDisplayed());
{
driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).click();
}
答案1
得分: 1
你可以使用 .findElements
(带有 s
)方法,它会返回一个列表。
检查列表的大小,如果 大于 0
,表示元素存在。
if (driver.findElements(By.xpath("//input[@id='btnOkOverrideBC']")).size() > 0) {
driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).click();
}
英文:
You can utilize .findElements
(with s
), this is return a list.
Check the size, if > 0
it means the element exists.
if(driver.findElements(By.xpath("//input[@id='btnOkOverrideBC']")).size()>0) {
driver.findElement(By.xpath("//input[@id='btnOkOverrideBC']")).click();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论