簡體   English   中英

通過 Selenium WebDriver 設置 RequestHeader

[英]Set RequestHeader through Selenium WebDriver

我在嘗試使用 browsermobproxy 為 selenium 測試設置請求標頭時遇到以下錯誤。

線程“main”org.openqa.selenium.WebDriverException 中的異常::無法從“Window”讀取“localStorage”屬性:拒絕訪問此文檔。

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)

驅動程序

public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Try\\drivers\\chromedriver.exe"); //PropertyReader.getProperty("chromedriverpath")
        
         // start the proxy
        BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.setTrustAllServers(true);
        proxy.start(0);

        // get the Selenium proxy object
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        
        String chromeProfilePath = "C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data\\Default";
        
      //capabilities
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-web-security");
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--user-data-dir="+chromeProfilePath);
                                                                
        
        // configure it as a desired capability
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        capabilities.setCapability(ChromeOptions.CAPABILITY, options); 
        //capabilities.setCapability("disable-web-security", true);

        // start the browser up
        WebDriver driver = new ChromeDriver(capabilities);

        // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
        proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

        //
        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);

          
    // create a new HAR with the label "yahoo.com"
    proxy.newHar("localhost:4200");

    // open app
    driver.get("http://localhost:4200/app/");

如果有人能在這方面幫助我,那就太好了。 謝謝你。

這是因為您的瀏覽器隱私設置不允許在頁面上設置 javascript 來設置 cookies(第三方 cookie)。 您需要使用屬性更改它們。

您可以在此處找到相關屬性的列表: https://stackoverflow.com/a/48670137/8343843

如何使用 web 驅動程序設置屬性,您可以在此處找到: https://stackoverflow.com/a/25090103/8343843

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM