2016-11-14 87 views
-2

我正在尝试关闭使用java的selenium web驱动程序弹出。我尝试了不同的方式,但无法成功。请帮帮我。如何关闭在硒webdriver弹出?

package Demo; 
import java.util.Set; 

import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class YahooTest { 
    public static void main(String[] args) throws InterruptedException { 
     FirefoxDriver obj = new FirefoxDriver(); 
     String url = "https://www.planyourjourney.com/"; 
     obj.get(url); 
     obj.manage().window().maximize(); 
     obj.findElement(By.xpath("html/body/div[12]/div/div/div[9]")).click(); 
     obj.findElementByClassName("dyna-link-new-registration").click(); 
     obj.findElement(By.linkText("B2B Login")).click(); 
     Thread.sleep(4000); 
     obj.findElement(By.id("userid")).clear(); 
     obj.findElement(By.id("userid")).sendKeys("[email protected]"); 
     obj.findElement(By.id("ulPassword")).clear(); 
     obj.findElement(By.id("ulPassword")).sendKeys("spyj01"); 
     obj.findElement(By.name("Next")).click(); 
     Thread.sleep(2000); 
     obj.findElement(By.id("affilitetrainadvpage")); 
     Alert alert = obj.switchTo().alert(); 
     alert.dismiss(); 
    } 
} 
+1

你究竟试过了什么?提供您的Java代码以及弹出的HTML代码 – Andersson

+2

http://sqa.stackexchange.com/questions/5310/how-to-close-pop-up-window-in-selenium-webdriver – fabersky

+0

请格式化您的代码。这是不可读的。 – Tom

回答

0

我已经看到网页的源代码,并且在其中找不到任何提醒。

弹出窗口实际上写在div标签内。

因此,删除与警报相关的代码并在下面的代码中使用xpath。

obj.findElement(By.xpath("//*[@id='affilitetrainadvpage']/span/a/img")).click(); 
+0

我已经尝试过,但直到不关闭弹出。 –

+0

它正在为我工​​作。我正在使用chrome。你能检查吗?你是否有任何错误。 –