【问题标题】:How can I set proxy to selenium remote web driver?如何将代理设置为 selenium 远程 Web 驱动程序?
【发布时间】:2020-09-26 03:30:47
【问题描述】:

我正在尝试在公司代理后面执行 aws 设备场示例。 起初我在连接 aws 时遇到了代理问题。 我设法通过设置 aws 代理配置来解决这个问题。 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 proxy remotewebdriver aws-device-farm


【解决方案1】:

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

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

Javascript 示例如下。

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

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 2017-04-09
    • 2015-06-16
    • 2015-05-23
    • 2023-03-21
    • 2012-05-01
    相关资源
    最近更新 更多