如何解决Selenium搭配BrowserMobProxy设置请求头时的localStorage访问拒绝异常
Selenium结合BrowserMobProxy设置请求头报localStorage访问拒绝错误
我在Selenium测试流程中尝试通过BrowserMobProxy设置请求头时,遇到如下报错:
Exception in thread "main" org.openqa.selenium.WebDriverException: : Failed to read the 'localStorage' property from 'Window': Access is denied for this document. Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z' System info: host: 'G9HQBVT2E', ip: '10.62.6.122', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_271' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.141, chrome: {chromedriverVersion: 87.0.4280.20 (c99e81631faa0..., userDataDir: C:\Users\abc\AppData\...}, goog:chromeOptions: {debuggerAddress: localhost:64062}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(manual, http=G9HQBVT2..., setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true} Session ID: 73d6ecf19b420b0bc368f2bc3d5f78b1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:485) at com.sample.test.DataReader.main(DataReader.java:79)
报错本质是浏览器安全机制拦截了JS对localStorage的读写操作,提示当前文档无访问权限。
测试所用的驱动程序代码如下:
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Try\\drivers\\chromedriver.exe"); // 启动BrowserMob代理服务 BrowserMobProxy proxy = new BrowserMobProxyServer(); proxy.setTrustAllServers(true); proxy.start(0); // 生成适配Selenium的代理配置对象 Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy); String chromeProfilePath = "C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data\\Default"; // 配置Chrome启动参数 ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-web-security"); options.addArguments("--ignore-certificate-errors"); options.addArguments("--user-data-dir=" + chromeProfilePath); // 将代理、Chrome参数写入Selenium能力配置 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, seleniumProxy); capabilities.setCapability(ChromeOptions.CAPABILITY, options); // 启动浏览器实例 WebDriver driver = new ChromeDriver(capabilities); // 开启HAR捕获,记录请求、响应的完整内容 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT); // 跳转到目标Chrome扩展的后台页面 driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html"); String jsScript = "localStorage.setItem('profiles', JSON.stringify([{ title: 'Selenium', hideComment: true, appendMode: '', \n" + " headers: [ \n" + " {enabled: true, name: 'token-1', value: '{\"abc\": 1234, \"type\": \"def\"}', comment: ''}\n" + " ], \n" + " respHeaders: [],\n" + " filters: []\n" + " }]));"; ((JavascriptExecutor)driver).executeScript(jsScript); // 新建以localhost:4200为标识的HAR捕获记录 proxy.newHar("localhost:4200"); // 打开被测业务页面 driver.get("http://localhost:4200/app/"); }
烦请有相关经验的人员帮忙排查解决该问题,非常感谢。
内容的提问来源于stack exchange,提问作者suneel chaitanya krishna
相关产品推荐
相关产品推荐

