简体   繁体   English

如何处理硒Webdriver中的简单HTML弹出窗口?

[英]How to handle a pop up which is a simple HTML pop up in selenium webdriver?

How to handle a pop up which is a simple HTML pop up in selenium webdriver . 如何处理弹出窗口,这是Selenium Webdriver中的简单HTML弹出窗口。 Its a pop up written in HTML . 它是用HTML编写的弹出窗口。

Have you tried using 您是否尝试过使用

driver.switchTo().alert().accept();

?

In case the pop up is modal you have to do what the previous answer is proposing, but in case the pop up is just HTML you should just find a WebElement inside the popup code and work as usual, look the example: 如果弹出式窗口是模态的,则必须执行上一个答案所建议的操作,但是如果弹出式窗口只是HTML,则应该在弹出式代码内找到一个WebElement并照常工作,请看示例:

driver.findElement(By.id(popupid))

Pay attention and manage the implicit timeout in order to make sure the find will wait until the popup starts. 请注意并管理隐式超时,以确保查找将一直等到弹出窗口开始。

Use below given sample code and it will work. 使用下面给定的示例代码,它将起作用。

public String parentWindowHandler = null,subWindowHandler=null;
    Set<String> handles = driver.getWindowHandles(); // get all window handles
    Iterator<String> iterator = handles.iterator();
    while(iterator.hasNext()){
    subWindowHandler = iterator.next();
    driver.switchTo().window(subWindowHandler);//select new popup
    }


    /*
    your code here for script

    */

    driver.switchTo().window(parentWindowHandler);//return to main window

First, you should find unique DOM properties because of sometimes same element properties like id and class same for HTML pop-up and parent Page. 首先,您应该找到唯一的DOM属性,因为有时对于HTML弹出窗口和父页面来说,相同的元素属性(例如id和class)相同。

So If it's unique then u can perform simply drive.findelements(By).click() , or sendkeys(), etc else u should be made unique XPath or by indexing, u have to click or perform other action. 因此,如果它是唯一的,那么您可以简单地执行drive.findelements(By).click()或sendkeys()等,否则您应被设为唯一的XPath或通过建立索引,必须单击或执行其他操作。

SwitchTo uses if it is in iframe else it will not use. SwitchTo使用是否在iframe中,否则将不使用。

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

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