问题
I'm using selenium python to test a resturant pos system.
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.
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;" in each category,if the category is not selected the value of style will keep display: none,
once the category is selected,the value of style will change to 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?
回答1:
I think you can use get_attribute("style") to accomplish what you are trying to do here. 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.
Here's how you can check the display: none and display: block strings in each element:
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.
来源:https://stackoverflow.com/questions/59537965/how-to-handle-modal-or-pop-up-boxes-in-selenium-python