Java SecurityError:CSSStyleSheet.cssRules getter:使用Selenium Firefox读取CSS样式表时,不允许访问跨源样式表错误
我正在将Selenium与Java和Chrome/Firefox驱动程序一起使用Java SecurityError:CSSStyleSheet.cssRules getter:使用Selenium Firefox读取CSS样式表时,不允许访问跨源样式表错误,java,selenium,firefox,cross-domain,selenium-firefoxdriver,Java,Selenium,Firefox,Cross Domain,Selenium Firefoxdriver,我正在将Selenium与Java和Chrome/Firefox驱动程序一起使用 FirefoxProfile profile=新的FirefoxProfile(); setPreference(“javascript.enabled”,true); setPreference(“security.fileuri.strict\u origin\u policy”,false); DesiredCapabilities=DesiredCapabilities.firefox(); 能力。设置能力
FirefoxProfile profile=新的FirefoxProfile();
setPreference(“javascript.enabled”,true);
setPreference(“security.fileuri.strict\u origin\u policy”,false);
DesiredCapabilities=DesiredCapabilities.firefox();
能力。设置能力(FirefoxDriver.PROFILE,PROFILE);
Capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
setCapability(“AcceptUnsecureCerts”,true);
FirefoxOptions=新的FirefoxOptions();
选项。添加功能(功能);
WebDriver WebDriver=新的FirefoxDriver(选项);
正在尝试读取CSS样式表
JavascriptExecutor js=((JavascriptExecutor)驱动程序);
objectproperties=js.executeScript(“returndocument.styleSheets”);
我得到以下错误:
Exception in thread "main" org.openqa.selenium.WebDriverException: SecurityError: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet
使用chrome驱动程序,我得到了errorerror reading属性
我正在尝试获取页面的
文档.样式表
,但由于CORS规则,我无法获取。如何禁用它们?看起来像是打字错误。您的意思可能不是document
,而是document
。因此,您的代码行实际上是:
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
…意味着cssRulesgetter()
方法被阻止访问跨源样式表
细节 根据中的讨论,即使在您使用的情况下,您的应用程序可能仍然没有任何跨源样式表,但这些跨源样式表是由浏览器扩展注入的
因此,一个快速的解决方案是禁用以解决问题。您好,谢谢您提供的信息,我更正了我评论中的打字错误。不幸的是,在我的例子中,打字错误并不是问题所在。你有没有尝试过禁用你的扩展/插件——特别是假象?