2017-07-19 35 views
3

我无法解除Firefox位置提醒。当我点击商店定位器时,Firefox将打开位置警报。我想点击“不允许”。我知道如何处理硒web驱动程序中的警报框。但是,我无法点击提醒。如何在selenium webdirver中关闭Firefox的“共享位置”提醒

package toysrus; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.Alert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.NoAlertPresentException; 
import org.openqa.selenium.UnexpectedAlertBehaviour; 
import org.openqa.selenium.UnhandledAlertException; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.testng.annotations.Test; 

public class Toysrus { 
    public WebDriver driver; 

@Test(priority=1) 
public void firefox() throws InterruptedException 
{  
    System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe"); 
     driver=new FirefoxDriver(); 
} 


@Test(priority=2) 
public void urlaccess() 
{ 
    String URL="http://m.toysrus.com/?TRU=1"; 
    driver.get(URL); 
} 

@Test(priority=3) 
public void menucontainner() 
{ 
    driver.findElement(By.id("sk_menu_icon_container")).click();  
} 

@Test(priority=3) 
public void storeloocator() throws InterruptedException 
{ 
    driver.findElement(By.name("storelocator")).click(); 
    Thread.sleep(5000); 
    Alert alert = driver.switchTo().alert(); 
    alert.dismiss(); 
} 
+0

您是否尝试过这个 - 1.点击取消按钮 '''driver.switchTo() .alert()。dismiss();''' 2.点击警报的'OK'按钮。 '''driver.switchTo()。alert()。accept();''' –

+0

否它不工作。我已经试过了 –

回答

0

共享位置的权限不是警报(至少不是webdriver)。这是一种能力。所以最好的办法是关掉这个能力。参考我对这个geolocation post的回答。基本上你需要设置DesiredCapabilities您的驱动程序:

DesiredCapabilities caps = new DesiredCapabilities(); 
caps.setBrowserName("firefox"); 
caps.setCapability("locationContextEnabled", false); 

driver = new RemoteWebDriver(new URL(URL),caps); 

//continue the rest of your test and remove the code on Alert 

这将防止从提示显示连。

附注:我建议把在驱动程序初始化这个代码在@Before方法,而不是@Test.

相关问题