[英]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 彈出窗口。
這是iframe
它阻止你訪問element.Use webdriverwait
和switch
到iframe
,然后再訪問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.