[英]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.