3

我需要在 Firefox selenium 中加载一个页面,然后重写页面的内容,我必须使用 Firefox,所以 Chrome 不是一个选项。

我尝试了下面的代码

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

但这给了我错误:

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

`

我需要知道是否有任何选项about:config或任何方式可以使 Firefox 运行不安全的操作。

4

1 回答 1

1

Document.write()

Document.write()方法将一串文本写入文档流,调用document.write()已关闭(加载)的文档会自动调用document.open(),这将清除文档。

长期以来,除了删除所有节点外,还删除了所有 JavaScript 变量。但情况已不再如此。然而仍然继续这样做。

  • 等效的 Python 代码:

    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');")
    
  • 浏览器快照:

铬合金


壁虎特有的笔记

从 Gecko 1.9 开始,此方法受制于与 其他属性相同的同源策略 ,如果这样做会更改文档的来源,则该方法不起作用。

从 Gecko 1.9.2 开始,document.open() 使用它所使用的 URI 的文档的主体,而不是从堆栈中获取主体。因此,您不能再document.write()从 chrome 调用不受信任的文档,即使使用 WrappedJSObject 也是如此


tl; 博士

有关主体的更多信息,请参阅安全检查基础知识。


参考

您可以在以下位置找到一些相关的详细讨论:

于 2020-11-19T22:09:23.597 回答