简体   繁体   English

如何在 Selenium Python 中处理模态框或弹出框

[英]How to handle modal or Pop-up Boxes in Selenium Python

I'm using selenium python to test a resturant pos system.我正在使用 selenium python 来测试餐厅 pos 系统。

After click different category menus,there will be about 3 different kinds of pop-up(modal) windows pop out to allow custom to chose items.Different category will pop out different pop-up(modal) windows.点击不同的类别菜单后,会弹出大约3种不同的弹出(模态)窗口,允许自定义选择项目。不同的类别会弹出不同的弹出(模态)窗口。

在此处输入图片说明

The category menus code are:类别菜单代码是:

<div id="iopopsz" style="display: none">
 <div style="display:flex">
     ...code..
 </div>
</div>


<div id="comboitemsz" class="copt" style="display: none;">
    <div style="display: flex">
        ...code..
</div>
</div>

<div id="mcoption8sz" class="copt" style="display: none;">
        <div style="display: flex">
        ...code..
</div>
</div>

As you can see,there is a style="display: none;"如您所见,有一个style="display: none;" in each category,if the category is not selected the value of style will keep display: none ,在每个类别中,如果未选择该类别,则样式值将保持显示:none

once the category is selected,the value of style will change to display:block .选择类别后,样式的值将更改为display:block

Now I need to first check which pop-up(alert) window pop out and then switch to the window to click a item.So what I should do?Any friends can help?现在我需要先检查弹出的是哪个弹出(警告)窗口,然后切换到该窗口单击某个项目。那我该怎么办?有朋友可以帮忙吗?

I think you can use get_attribute("style") to accomplish what you are trying to do here.我认为您可以使用get_attribute("style")来完成您在此处尝试执行的操作。 From what I can tell, you are not actually working with a true alert -- an alert is a Javascript popup that has no HTML, and can be only accepted or dismissed through the alerts class.据我所知,您实际上并没有使用真正的警报——警报是一个没有 HTML 的 Javascript 弹出窗口,只能通过警报类接受或解除。

Here's how you can check the display: none and display: block strings in each element:以下是检查display: nonedisplay: block每个元素中的display: block字符串的方法:

first_item = driver.find_element_by_id("iopopsz")
style_attr_first_item = first_item.get_attribute("style") # "display: none"

if "display: none;" in style_attr_first_item:
    print("First item is not visible.")

Hopefully this will get you started and shows you how to effectively check the display: none and display: block properties of each element.希望这能帮助您入门并向您展示如何有效地检查每个元素的display: nonedisplay: block属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM