Selenium и BrowserMob Proxy для захвата запросов и ответов - PullRequest
0 голосов
/ 08 апреля 2020

ОБНОВЛЕНИЕ - Решена эта проблема с помощью System.setProperty ("webdriver. chrome .silentOutput", "true");

Я пытаюсь перехватить запрос и ответ браузера используя селен и прокси моб браузера. Устали большинство вариантов и комбинаций из разных блогов, но все же проблема не решена. Может кто-то помочь.

Ниже приведен код:

String sFileName = System.getProperty("user.dir") + "filepath";

        System.setProperty("webdriver.chrome.driver",
                "driverpath");
        proxy = new BrowserMobProxyServer();
        proxy.start(0);

        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

        DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();
        seleniumCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        seleniumCapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

        ChromeOptions options = new ChromeOptions();
        options.merge(seleniumCapabilities);
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();

        proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

        try {
            proxy.newHar("networkData");
            driver.get("https://www.google.com/");
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Har har = proxy.getHar();
            File harFile = new File(sFileName);
            har.writeTo(harFile);
        } catch (IOException ex) {

        }
        driver.quit();
        proxy.stop();
    }

Моя Chrome версия 81.0.4044.92 и chrome драйвер 81.0.4044.69 и селен 4.0.0-alpha-4

Я получаю ошибку ниже,

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '4.0.0-alpha-4', revision: 'c2d955b49e'
System info: host: '', ip: '', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.2'
Driver info: driver.version: RemoteWebDriver
Capabilities {acceptInsecureCerts: true, browserName: chrome, browserVersion: 81.0.4044.92, chrome: {chromedriverVersion: 81.0.4044.69 (6813546031a4b..., userDataDir: \AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:59361}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(manual, http=WKWIN524..., setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 0897543cf930b2ab7564fdd00fa8398a
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:603)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:282)
    at steps.Network.main(Network.java:47)
Caused by: java.lang.RuntimeException: NettyHttpHandler request execution error
    at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:61)
    at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
    at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55)
    at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:45)
    at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
    at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55)
    at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:43)
    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:582)
    ... 2 more
Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:34575 after 60000 ms
    at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
    at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2070)
    at org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:201)
    at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:55)
    ... 11 more
Caused by: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:34575 after 60000 ms
    at org.asynchttpclient.netty.timeout.TimeoutTimerTask.expire(TimeoutTimerTask.java:43)
    at org.asynchttpclient.netty.timeout.RequestTimeoutTimerTask.run(RequestTimeoutTimerTask.java:50)
    at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:680)
    at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:755)
    at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:483)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:830)
...