使用firefoxProfile.setPreference打开一个新的私有窗口(“browser.private.browsing.autostart”,true)不起作用

时间:2015-12-31 04:14:08

标签: java selenium

我正在尝试使用selenium Web Driver启动一个新的私有窗口。我做了很多搜索以找到出路。以下是我喜欢的一些感兴趣的链接。

Link 1

Link 2

根据这些链接,我发现以下代码对我有用。

// Open a new Private Window
FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);

但是,这段代码不起作用。我甚至没有收到错误消息。但是,此代码不会打开新的私有窗口。

如果有人知道原因请帮忙。提前谢谢。

更新代码

public class bCanGoAfterThreeMin {

    public void bCanGo() {

        WebDriver driver = new FirefoxDriver();

        //Puts an Implicit wait, Will wait for 25 seconds before throwing exception
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);


        //Launch site
        driver.navigate().to("my url");
        System.out.println("Map Page Launched");


        //Maximize the browser
        driver.manage().window().maximize();
        System.out.println("Browser Maximized");

        //Login
        driver.findElement(By.xpath(".//*[@id='login_btn']")).click();
        driver.findElement(By.xpath(".//*[@id='loginform']/div[5]/input")).sendKeys("manuli@et.lk");
        driver.findElement(By.xpath(".//*[@id='loginform']/div[6]/input")).sendKeys("1qaz2wsx");
        driver.findElement(By.xpath(".//*[@id='btn-login']")).click();


        // Click on a Hotel
        driver.findElement(By.xpath(".//*[@id='image_row']/div/div[1]/div/span/a/img")).click();

        if ((driver.getCurrentUrl())!= ("correct url")){
                System.out.println("Successfully redirect to the Aliya hotel Page");
            }
        else{
                System.out.println("Failed to redirect to the Aliya hotel Page");
            }


        // Click Book Now Button
        driver.findElement(By.cssSelector(".btn.btn-primary.btn-lg.book-btn")).click();             
        System.out.println("Successfully redirect to the cart page(With Aliya)");


        if ((driver.getCurrentUrl())!= ("correct url")){

        driver.navigate().to("correct url");

        // Click Proceed to checkout
        driver.findElement(By.cssSelector(".btn.btn-success.pro-btn")).click(); 

        System.out.println("Clicked Proceed to checkout");

        }

        // Open a new Private Window
        FirefoxProfile firefoxProfile = new FirefoxProfile();    
        firefoxProfile.setPreference("browser.private.browsing.autostart",true);

        driver = new FirefoxDriver();
        //driver.get("url");

        System.out.println("New Private Window");


    }
}

2 个答案:

答案 0 :(得分:0)

嘿,如果您想使用任何Firefox Profile,您需要使用该firefox配置文件启动驱动程序,只要实例化驱动程序在构造函数中传递配置文件。

FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);
WebDriver driver = new FirefoxDriver(firefoxProfile);

答案 1 :(得分:0)

您似乎没有初始化驱动程序,请按照以下更改代码,以便根据您的需要进行操作:

public void bCanGo() { 

WebDriver driver; 

driver = new FirefoxDriver(); 
System.out.println("New Private Window"); 

//Puts an Implicit wait, Will wait for 25 seconds before throwing exception 
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS); 


//Launch site 
driver.navigate().to("my url"); 
System.out.println("Map Page Launched"); 


//Maximize the browser 
driver.manage().window().maximize(); 
System.out.println("Browser Maximized"); 

//Login 
driver.findElement(By.xpath(".//*[@id='login_btn']")).click(); 
driver.findElement(By.xpath(".//*[@id='loginform']/div[5]/input")).sendKeys("manuli@et.lk"); 
driver.findElement(By.xpath(".//*[@id='loginform']/div[6]/input")).sendKeys("1qaz2wsx"); 
driver.findElement(By.xpath(".//*[@id='btn-login']")).click(); 


// Click on a Hotel 
driver.findElement(By.xpath(".//*[@id='image_row']/div/div[1]/div/span/a/img")).click(); 

if ((driver.getCurrentUrl())!= ("correct url")){ 
System.out.println("Successfully redirect to the Aliya hotel Page"); 
} 
else{ 
System.out.println("Failed to redirect to the Aliya hotel Page"); 
} 


// Click Book Now Button 
driver.findElement(By.cssSelector(".btn.btn-primary.btn-lg.book-btn")).click(); 
System.out.println("Successfully redirect to the cart page(With Aliya)"); 


if ((driver.getCurrentUrl())!= ("correct url")){ 

driver.navigate().to("correct url"); 

// Click Proceed to checkout 
driver.findElement(By.cssSelector(".btn.btn-success.pro-btn")).click(); 

System.out.println("Clicked Proceed to checkout"); 

} 


// Open a new Private Window 
FirefoxProfile firefoxProfile = new FirefoxProfile(); 
firefoxProfile.setPreference("browser.private.browsing.autostart",true); 

driver = new FirefoxDriver(); 
driver.get("url"); 

System.out.println("New Private Window"); 

}

注意:我已声明驱动程序并使用firefox进行分配。