java - SecurityError : CSSStyleSheet. cssRules getter:使用 Selenium Firefox 读取 CSS 样式表时不允许访问跨源样式表错误

标签 java selenium firefox cross-domain selenium-firefoxdriver

我正在将 Selenium 与 Java 和 Chrome/Firefox 驱动程序一起使用。

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", true);
profile.setPreference("security.fileuri.strict_origin_policy", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability("acceptInsecureCerts",true);
FirefoxOptions options = new FirefoxOptions();
options.addCapabilities(capabilities);
WebDriver webDriver = new FirefoxDriver(options);

尝试读取 CSS 样式表

JavascriptExecutor js = ((JavascriptExecutor) driver);
Object properties = js.executeScript("return document.styleSheets");

我收到以下错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: SecurityError: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet

使用 Chrome 驱动程序时,我收到错误读取属性时出错

我正在尝试获取页面的 document.styleSheets,但由于 CORS 规则而无法获取。如何禁用它们?

最佳答案

好像是一个错字。您可能指的是文档,而不是文档。实际上,您的代码行将是:

Object properties = js.executeScript("return document.styleSheets");
<小时/>

但是,这个错误消息...

Exception in thread "main" org.openqa.selenium.WebDriverException: SecurityError: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet

...暗示 cssRules getter() 方法被阻止访问跨源样式表

<小时/>

详细信息

根据Unable to access cssRules property DOMException: "CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet"中的讨论即使您的应用程序可能没有任何跨源样式表,但如果您正在使用 ,这些跨源样式表Ghostery 注入(inject)浏览器扩展。

因此,一个快速的解决方案是禁用 Ghostery extension来解决问题。

关于java - SecurityError : CSSStyleSheet. cssRules getter:使用 Selenium Firefox 读取 CSS 样式表时不允许访问跨源样式表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475212/

相关文章:

javascript - 我如何等待元素在 WebDriverJS 中可点击?

python - selenium python 绑定(bind)是否需要 firefox

jquery - Mootools (joomla) + jQuery + Firefox 冲突 : selector bug

java - Spring MVC : @Value annotation with final variable

java - Servlet - 将文件保存到磁盘,但不确定使用什么路径?

java - 安卓时间排行

java - Maven,本地目录的相对路径

testing - 使用 webdriver 上传文件

selenium - 实习生 Leadfoot WaitForAddedById 功能?

html - 在Conkeror的youtube上启用全屏