如何处理硒中的弹出窗口或警报?

时间:2019-04-03 11:11:56

标签: java selenium selenium-webdriver

每当我运行我的代码时,我就会弹出。 如何取消该网站的弹出窗口:https://www.goibibo.com/

每当我运行我的代码时,我就会弹出。 如何取消该网站的弹出窗口:https://www.goibibo.com/

package basic;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class goibibo {
    private static WebDriver driver = null;
    public static void main(String[] args) throws InterruptedException  {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.goibibo.com/");
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']"))).sendKeys("A");
        Thread.sleep(1000);
        List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='dib marginL10 pad0 textOverflow width90']/div/span")));
        for (int i = 0; i < myList.size(); i++)
        {
                System.out.println(myList.get(i).getText());
                if (myList.get(i).getText().equals("Ahmedabad"))
                {
                    myList.get(i).click();
                    break;
                }
            }
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"gosuggest_inputDest\"]"))).sendKeys("Mum");
        Thread.sleep(1000);
        List<WebElement> Dept = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='dib marginL10 pad0 textOverflow width90']/div/span")));
        for (int j = 0; j < Dept.size(); j++)
        {
                System.out.println(Dept.get(j).getText());
                if (Dept.get(j).getText().equals("Mumbai"))
                {
                    Dept.get(j).click();
                    break;
                }
        }
        Thread.sleep(3000);
        driver.switchTo().alert().dismiss();
    }

}


no such alert

3 个答案:

答案 0 :(得分:2)

首先不是弹出窗口,而是iframe。

您可以从此链接获取更多信息,了解如何处理硒中的iframe

首先,您需要切换到iframe。

为此,在Thread.sleep(3000);从html源中找到iframe名称后,您需要编写以下代码。

driver.switchTo().frame("notification-frame-~2514428c7");
driver.findElement(By.xpath("//i[@class='wewidgeticon we_close']")).click();

使用此功能,您可以关闭html弹出窗口。

答案 1 :(得分:0)

正如我所看到的,这是您正确的警报

enter image description here

因此您可以做一件事情,可以为警报编写代码,如果可见警报,则按由try catch包围的关闭按钮,以便在警报即将关闭时可以轻松摆脱警报。尽快,您的执行将会受到干扰,但是您需要使用诸如for from和to文本字段之类的主要元素,例如,如果给它们更少的等待时间,并且如果弹出窗口花费大量时间关闭,那么测试用例将失败,并且还有其他事情当我们将源传递给值时,弹出窗口只会在一个会话中出现一次

让我们知道您是否需要更多帮助

答案 2 :(得分:0)

这是<div class="contact-form"> <form id="contact-form" method="post" action="contact-form-handler.php"> <input name="name" type="text" class="form-control" placeholder="Your Name" required> <br> <input name="email" type="email" class="form-control" placeholder="Your Email"> <br> <textarea name="message" class="form-control" placeholder="Message" rows="30" required></textarea><br> <input type="submit" class="form-control" value="SEND MESSAGE"> </form> </div> ,阻止您访问该元素。首先使用iframewebdriverwaitswitch,然后访问该元素。尝试以下代码。

iframe
相关问题