繁体   English   中英

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

[英]How to handle pop-up or alert in selenium?

每当我运行我的代码时,我都会弹出。 如何取消该网站的弹出窗口: 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

首先,这不是一个 Iframe 的弹出窗口。

您可以从此链接获取更多信息 how to handle iframe in selenium

首先你需要切换到 iframe。

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

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

通过使用它,您可以关闭 html 弹出窗口。

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

在此处输入图片说明

所以你可以做一件事,你可以为警报编写代码,如果警报可见,然后按下由 try catch 包围的关闭按钮,这样你就可以轻松摆脱警报,一旦警报出现,它就会尽快关闭,你执行会受到干扰,但您需要等待主要元素,例如 for from 和 to 文本字段,例如如果您给它们更少的等待,并且如果弹出窗口需要很长时间才能关闭,则测试用例将失败,并且弹出窗口还会出现另一件事当我们将源和值传递给一个会话时,只有一次

如果您需要更多帮助,请告诉我们

这是iframe它阻止你访问element.Use webdriverwaitswitchiframe ,然后再访问element.Try下面的代码。

driver.get("https://www.goibibo.com/");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name ,'notification-frame-')]")));
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[contains(@class ,'wewidgeticon')]")));
element.click();

暂无
暂无

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

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