3

I need to load a page in Firefox selenium and then rewrite the content of the page, It's essential that I use Firefox so Chrome is not an option.

I tried the below code

FirefoxDriver firefoxDriver = new FirefoxDriver(new FirefoxOptions() { AcceptInsecureCertificates = true });
            
IJavaScriptExecutor javaScriptExecutor = (IJavaScriptExecutor)firefoxDriver;
firefoxDriver.Navigate().GoToUrl("https://www.google.com");
javaScriptExecutor.ExecuteScript("document.write('a');");

But it gives me the error:

OpenQA.Selenium.WebDriverException: 'SecurityError: The operation is insecure.

`

I need to know if there is any option in about:config or any way to make Firefox run insecure operations.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

Document.write()

The Document.write() method writes a string of text to a document stream, calling document.write() on a closed (loaded) document automatically calls document.open(), which will clear the document.

For a long time, and additionally erased all JavaScript variables in addition to removing all nodes. But this is no longer the case. However still continues to do so.

  • Equivalent Python code:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://google.com")
    driver.execute_script("document.write('a');")
    
  • Browser Snapshot:

Chrome


Gecko-specific notes

Starting with Gecko 1.9, this method is subject to the same same-origin policy as other properties, and does not work if doing so would change the document's origin.

Starting with Gecko 1.9.2, document.open() uses the principal of the document whose URI it uses, instead of fetching the principal off the stack. As a result, you can no longer call document.write() into an untrusted document from chrome, even using wrappedJSObject.


tl; dr

See Security check basics for more about principals.


References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352