如何将代理设置为Selenium远程Web驱动程序?

问题描述

我正在尝试在公司代理后面执行AWS设备场示例。 起初,我在连接aws时遇到代理问题。 我设法通过设置aws Proxy Configuration解决了这一问题。 my another question on this matter

现在,我面临另一个代理问题。 创建remotewebdriver时,出现未知的主机名错误。 这是我的源代码

@Before
public void setUp() {
    try {
        ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder();
        proxyConfig.endpoint(new URI("<YOUR PROXY URL>"));
        proxyConfig.username("<YOUR USER ID>");
        proxyConfig.password("YOUR PASSWORD");
        ApacheHttpClient.Builder httpClientBuilder =
                ApacheHttpClient.builder()
                                .proxyConfiguration(proxyConfig.build());

        String myARN = "<YOUR ARN>";
        DeviceFarmClient client  = DeviceFarmClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create())
                .region(Region.US_WEST_2)
                .httpClientBuilder(httpClientBuilder)
                .overrideConfiguration(ClientOverrideConfiguration.builder().build())
                .build();
        CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder()
                .expiresInSeconds(300)        // 5 minutes
                .projectArn(myARN)
                .build();
        URL testGridUrl = null;
        CreateTestGridUrlResponse response = client.createTestGridUrl(request);
        testGridUrl = new URL(response.url());
        driver = new RemoteWebDriver(testGridUrl,DesiredCapabilities.chrome()); //error occurred at this line.
    } catch (Exception e) {
        e.printstacktrace();
    }
}

这是错误日志。

UnkNownHostException: testgrid-devicefarm.us-west-2.amazonaws.com

我尝试了一些措施来解决此问题,但是这些措施都无法正常工作。 以下是我的一些试验。

  1. 使用browsermob库
DesiredCapabilities cap = DesiredCapabilities.chrome();
browserMobProxy bmp = new browserMobProxyServer();
bmp.setChainedProxy(
        new InetSocketAddress("<PROXY HOST>",Integer.valueOf("<PROXY PORT>")));
bmp.chainedProxyAuthorization("<USER>","<PASSWORD>",AuthType.BASIC);
bmp.setTrustAllServers(true);
bmp.start();
Proxy proxy = ClientUtil.createSeleniumProxy(bmp);
cap.setCapability(CapabilityType.PROXY,proxy);
driver = new RemoteWebDriver(testGridUrl,cap);
  1. 使用browserup而不是browsermob

它也无法正常运行,并且发生了相同的错误(UnkNownHostException)。

  1. 使用Authenticator.setDefault

它也不能很好地工作。

Authenticator.setDefault(new Authenticator() {
    public PasswordAuthentication getpasswordAuthentication() {
        return new PasswordAuthentication(proxyUserName,proxyPassword.tochararray());
    }
} );
System.setProperty("http.proxyUser",proxyUserName);
System.setProperty("http.proxyPassword",proxyPassword);

错误消息是..

java.io.IOException: Failed to authenticate with proxy

我该如何解决这个问题?

谢谢。

解决方法

在您的计算机中运行的 Selenium 客户端需要使用公司代理连接到 AWS Device Farm。

需要为您的 webdriver 连接设置代理。

Javascript 中的示例如下。

const driver = await new Builder()
  .usingWebDriverProxy(process.env.HTTPS_PROXY || '')
  .usingServer(urlString)
  .withCapabilities(...