Java 剧作家使用无浏览器代理连接

Java Playwright using connect with Proxy for browserless

我想使用 Playwright.connect() 方法使用 Proxy 来使用 Browserless。根据 Browserless 文档

https://docs.browserless.io/docs/playwright.html

The standard connect method uses playwright's built-in browser-server to handle the connection. This, generally, is a faster and more fully-featured method since it supports most of the playwright parameters (such as using a proxy and more). However, since this requires the usage of playwright in our implementation, things like ad-blocking and stealth aren't supported. In order to utilize those, you'll need to see our integration with connectOverCDP.

我认为 connect 会有一个 .setProxy(),喜欢 launch()

browserType.launch(new BrowserType.LaunchOptions().setProxy(proxy));

但是连接方法有两种变体

default Browser connect(String wsEndpoint) {
   return connect(wsEndpoint, null);
}
Browser connect(String wsEndpoint, ConnectOptions options);

我想我会选择 connect + ConnectOptions 它确实有一个 .setProxy 但它没有。

class ConnectOptions {
 public Map<String, String> headers;
 public Double slowMo;
 public Double timeout;

 public ConnectOptions setHeaders(Map<String, String> headers) {
   this.headers = headers;
   return this;
 }

 public ConnectOptions setSlowMo(double slowMo) {
   this.slowMo = slowMo;
   return this;
 }
 public ConnectOptions setTimeout(double timeout) {
   this.timeout = timeout;
   return this;
 }
}

我试过了

final Browser.NewContextOptions browserContextOptions = new Browser.NewContextOptions().setProxy(proxy);
Browser browser = playwright.chromium()
            .connect("wss://&--proxy-server=http://myproxyserver:1111")
            .newContext(browserContextOptions)
            .browser();
browser.newPage("resource");

但是需要代理returns认证。

我现在很困惑 Browserless.connect 可以提供代理,但如何提供?无浏览器有错吗?或者我错过了什么?我是这项技术的新手。

我也试过使用page.setExtraHTTPHeaders

private void applyProxyToPage(final Page page,final String 
userPassCombination){
final String value = "Basic "+Base64.getEncoder().encodeToString(userPassCombination.getBytes(Charset.forName("UTF-8")));
page.setExtraHTTPHeaders(Collections.singletonMap("Authorization",value));
//page.setExtraHTTPHeaders(Collections.singletonMap("Proxy-Authorization",value));// Not working either

}

在我朋友 Alejandro LoyolaBrowserless 的帮助下,我现在可以连接了。我将 post 片段:

private String navigateWithPlaywrightInBrowserlessWithProxy(final String token,final String proxyHost,final String userName,final String userPass,final String url){
    final Browser.NewContextOptions browserContextOptions = new Browser.NewContextOptions()
            .setProxy(new Proxy(proxyHost)
                    .setUsername(userName)
                    .setPassword(userPass));//Raw password not encoded in any way;
    try (final Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().connectOverCDP("wss://chrome.browserless.io?token=" + token);final BrowserContext context = browser.newContext(browserContextOptions);){
        Page page = context.newPage();
        page.route("**/*.svg", Route::abort);
        page.route("**/*.png", Route::abort);
        page.route("**/*.jpg", Route::abort);
        page.route("**/*.jpeg", Route::abort);
        page.route("**/*.css", Route::abort);
        page.route("**/*.scss", Route::abort);
        page.navigate(url, new Page.NavigateOptions()
                .setWaitUntil(WaitUntilState.DOMCONTENTLOADED));
        return page.innerHTML("body");
    }
}

我遇到的问题如下。

我正在使用:

"wss://chrome.browserless.io/playwright?token=

而不是:

"wss://chrome.browserless.io?token="

并使用:

connectOverCDP