如何在Selenium中使用条件语句,在弹出窗口出现时点击特定按钮。

huangapple go评论71阅读模式
英文:

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:

&lt;div class=&quot;ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; aria-describedby=&quot;DisplayBCWarningPopUpId&quot; aria-labelledby=&quot;ui-id-6&quot; style=&quot;position: absolute; height: auto; width: 300px; top: 81px; left: 837px; display: block;&quot; xpath=&quot;1&quot;&gt;
    &lt;div class=&quot;ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix&quot;&gt;&lt;/div&gt;
    &lt;div id=&quot;DisplayBCWarningPopUpId&quot; class=&quot;ui-dialog-content ui-widget-content&quot; style=&quot;width: auto; min-height: 92px; max-height: none; height: auto;&quot;&gt;
    	&lt;div id=&quot;ApplyFiltersModal&quot;&gt;
    	&lt;br&gt;
    	This will override the following applied filters. Hover over pills to see content changes. Do you wish to proceed?
    	&lt;table&gt;&lt;/table&gt;
    	&lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
    	&lt;div class=&quot;checkbox&quot;&gt;&lt;/div&gt;
    	&lt;div class=&quot;btn-delete-custom-dashboard&quot; style=&quot;padding-top:15px;&quot;&gt;
    		&lt;input type=&quot;button&quot; value=&quot;Cancel&quot; id=&quot;btnCancelOverrideBC&quot; name=&quot;WidgetGenericButtonContainer&quot; buttonenabled=&quot;true&quot; class=&quot;button-layout WidgetGenericButtonEnabled&quot;&gt;
    		&lt;script&gt;&lt;/script&gt;
   		&lt;input type=&quot;button&quot; value=&quot;Proceed&quot; id=&quot;btnOkOverrideBC&quot; name=&quot;WidgetGenericButtonContainer&quot; buttonenabled=&quot;true&quot; class=&quot;button-layout WidgetGenericButtonEnabled&quot;&gt;
    		&lt;script&gt;&lt;/script&gt;
    	&lt;/div&gt;
    &lt;/div&gt;
    	&lt;script&gt;&lt;/script&gt;
    &lt;/div&gt; 

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(&quot;//input[@id=&#39;btnOkOverrideBC&#39;]&quot;)).isDisplayed());
{
driver.findElement(By.xpath(&quot;//input[@id=&#39;btnOkOverrideBC&#39;]&quot;)).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 &gt; 0 it means the element exists.

if(driver.findElements(By.xpath(&quot;//input[@id=&#39;btnOkOverrideBC&#39;]&quot;)).size()&gt;0) {
	driver.findElement(By.xpath(&quot;//input[@id=&#39;btnOkOverrideBC&#39;]&quot;)).click();
}

huangapple
  • 本文由 发表于 2020年9月11日 23:08:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63849733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定