簡體   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