question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kubernetes DNS and Remote Webdriver

See original GitHub issue

Thank you for such an amazing tool you have built!

If I understand the functionality correctly, selenium-wire opens up a proxy socket on the addr FQDN (and random port), and any requests from selenium are routed through this proxy. This allows the proxy to capture and process all the requests.

It took me a while to understand that the addr FQDN is used for both - the creation of the proxy socket and from selenium to connect to the proxy.

I have set up a remote selenium grid on my kubernetes cluster, and I am trying to connect to it from another pod within my kubernetes cluster. The services for these pods are of type ClusterIP - meaning that the IP is randomly generated with each deployment. Kubernetes has intelligent DNS resolution where you can specify http://service-name:port, and it will resolve it to the IP address. So I should be able to open a port with service-name in the addr option, however when I try to do that, I get the following error: seleniumwire.thirdparty.mitmproxy.exceptions.ServerException: Error starting mitmproxy server: gaierror(-5, 'No address associated with hostname')

I tried using 127.0.0.1 or 0.0.0.0, and although I am able to create the proxy server, selenium is of course unable to connect to it and it fails with Message: unknown error: net::ERR_PROXY_CONNECTION_FAILED

It would be beneficial if I could define, for example, an addr to start the proxy server (where I could use 127.0.0.1) and another option to be able to reach that proxy server from selenium (where I can leverage the kubernetes DNS resolution).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
wkeelingcommented, Nov 2, 2021

Thanks for raising this. You’re right, Selenium Wire sends all browser traffic through an internal proxy it spins up in the background, and it uses the same address for both the proxy server and for Selenium itself when it configures the browser. There isn’t the ability to separate these currently in Selenium Wire itself, but I think there may be a workaround.

If you’re using the latest version of Selenium Wire, then there’s an option called auto_config. This tells Selenium Wire to configure the browser - via Selenium - with the IP/port of it’s internal proxy. The option is set to True by default but if you set this to False then Selenium Wire won’t configure the browser and will assume you will do it manually. You can configure the browser by passing a browser specific option (I’m assuming Chrome here) and specify the Kubernetes service-name at this point.

Here’s some code that demonstrates how to do it:

from seleniumwire import webdriver

sw_options = {
    'auto_config': False,  # Ensure this is set to False
    'addr': '0.0.0.0',  # The address the proxy will listen on
    'port': 8087,
}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=service-name:8087')  # Specify your Kubernetes service-name here
chrome_options.add_argument('--ignore-certificate-errors')

driver = webdriver.Remote(
    desired_capabilities=chrome_options.to_capabilities(),
    seleniumwire_options=sw_options,
)

driver.get(...)

Would that allow you to take advantage of the Kubernetes DNS resolution?

1reaction
wkeelingcommented, Mar 9, 2021

Great news that the workaround works! I may now look at adding the above example to the readme, as it may provide useful for other people running a container setup.

Regarding printing the requests, you just need to make sure that you print them before calling browser.quit(). Quitting the browser will shutdown Selenium Wire and clear out all captured requests. So switching the statements around should fix:

finally:
    print(browser.requests)
    browser.quit()  # Clears out request storage
Read more comments on GitHub >

github_iconTop Results From Across the Web

DNS for Services and Pods - Kubernetes
Kubernetes creates DNS records for Services and Pods. You can contact Services with consistent DNS names instead of IP addresses.
Read more >
Creating Kubernetes Services to access Selenium Hub Pods
In this video, we will discuss how we can create a service to expose the Selenium Hub Pods that we created in our...
Read more >
How does DNS work in kubernetes | Edureka Community
DNS is a built-in service in Kubernetes. It gets launched automatically when Kubernetes is setup for the first time.
Read more >
Fix DNS on a docker-compose selenium grid ... - Stack Overflow
I have a selenium grid running under docker-compose on a Jenkins machine. My docker-compose includes a simple web server that serves up a ......
Read more >
Moon - A cross browser Selenium, Cypress, Playwright and ...
A Selenium, Cypress, Playwright and Puppeteer testing platform running in Kubernetes or Openshift clusters. Fully compatible with Selenium Webdriver ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found