Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理selenium中的弹出窗口或警报?_Java_Selenium_Selenium Webdriver - Fatal编程技术网

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

Java 如何处理selenium中的弹出窗口或警报?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,每当我运行代码时,我都会弹出窗口。 如何取消该网站的弹出窗口: 每当我运行代码时,我都会弹出窗口。 如何取消该网站的弹出窗口: package-basic; 导入java.util.List; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.chrome.ChromeDriver; 导入org.openqa.sele

每当我运行代码时,我都会弹出窗口。 如何取消该网站的弹出窗口:

每当我运行代码时,我都会弹出窗口。 如何取消该网站的弹出窗口:

package-basic;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类戈比波{
私有静态WebDriver=null;
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
WebDriver驱动程序=新的ChromeDriver();
驱动程序。获取(“https://www.goibibo.com/");
新的WebDriverWait(driver,20).until(ExpectedConditions.elementToBickable(By.xpath(//input[@id='gosuggest_inputSrc'])).sendKeys(“A”);
睡眠(1000);
List myList=new WebDriverWait(driver,20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(//div[@class='dib marginL10 pad0 textfoverflow width90']/div/span));
对于(int i=0;i
正如我所见,这是您得到的正确警告

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


如果您需要更多帮助,请首先通知我们,这不是弹出窗口,而是Iframe

您可以从这个链接获得更多关于如何在selenium中处理的信息

首先,您需要切换到iframe

为此,您需要在
线程之后编写以下代码。sleep(3000)从html源代码中查找iframe名称

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

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

这是阻止您访问元素的
iframe
。请使用
webdriverwait
首先切换到
iframe
,然后访问元素。请尝试以下代码

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();

我在网页上没有看到任何警告。你能发布警报屏幕吗?症状是什么?有什么经验吗?@sharful那不是警报那只是htmlframe@Dhru“soni在这种情况下,我们如何在相同的代码中取消它。@sharful首先切换到iframe,您将获得关闭图标的xpath,然后单击它。它将关闭弹出窗口。那不是弹出窗口,那是iframe。用户需要切换到iframe。但是它是随机出现的。我们如何在两者之间切换呢?在等待网站3或4秒钟后,弹出窗口不会随机出现。我没有收到chrome浏览器的弹出窗口,因为该网站经常更新,我在firefox浏览器中收到弹出窗口,因为我没有从firfox中删除一些本地网站缓存。这很有效,谢谢。driver.switchTo().defaultContent();那么我必须使用这个吗?@sharful driver.switchTo().defaultContent();在这种情况下,焦点将移动到所有嵌套帧的父帧。例如,我有i1、i2、i3帧,它们是嵌套帧。在本例中,如果在焦点位于i3时使用default content(),它将移动到i1帧。但从我的角度来看,使用driver.switchTo().frame(“通知帧-~2514428c7”)的最佳方法;您能告诉我如何点击“出发”和“返回”,并用相同的代码选择所需的日期吗?
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();