Home > Net >  Geckodriver / FirefoxDriver / Selenium - How to open (empty) start page or clear page?
Geckodriver / FirefoxDriver / Selenium - How to open (empty) start page or clear page?

Time:03-13

I would like to clear a page after it has been loaded. Best way would be if the start page of the Firefox would be loaded, which is an empty page for me.

Background is, I'm getting some pages with the same framework but different data. Like you getting the history of a wikipedia article in a loop. Sometimes it seems to use the last, already gotten, page and not the new one. I don't know why this happens but it happens rarely from time to time. In this case an error would be great which I would get with a cleared page or different site.

A quick and dirty solution would be to load another page (like google or bing) but this causes additional data traffic and isn't really what I want.

In the only comment below is the line where I would like to navigate to the empty default page or clear the loaded page.

public class myGecko 
{

    WebDriver   FireFoxDriver;
    
    public void openFireFoxSingle() 
    {
        try 
        {
            System.setProperty("webdriver.gecko.driver","C:\\Gecko.exe");   
            File pathBinary             = new File("F:\\Firefox.exe");      
            FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
            
            
            firefoxBinary.addCommandLineOptions("--disable-extensions");
            
            DesiredCapabilities desired = new DesiredCapabilities();
            FirefoxOptions options      = new FirefoxOptions();                                                 
            
            desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));            
            FireFoxDriver = new FirefoxDriver(options);                                                         
            FireFoxDriver.manage().window().maximize();                                                         
        }
        catch(Exception e) {}
    }
    
    public void getPage(String myLink) 
    {
        try 
        {
            FireFoxDriver.get(myLink);
            WebElement myDynamicElement = (new WebDriverWait(FireFoxDriver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("col-content")));
            
            Thread.sleep(2000L);
            
            // HERE NOW NAVIGATE BACK TO START PAGE or an EMPTY PAGE or CLEAR THE PAGE
        }
        catch(Exception e) {return; }
        
        
    }
}

How do I clear the page or/and navigate back to the empty default page?

What works for me but causes a page to load three times is:

        FireFoxDriver.get(myLink);
        FireFoxDriver.navigate().refresh();
        FireFoxDriver.get(myLink);

With this I am completely sure, the page has been refreshed.

I use the latest Firefox 98.0, Selenium 4.12 and Geckodriver 0.3.0 But I had this issue also with older versions (Selenium 3.x and older Gecko).

CodePudding user response:

The answer is to create FireFoxDriver.get("about:blank");

  • Related