-
Notifications
You must be signed in to change notification settings - Fork 258
Kubernetes DNS and Remote Webdriver #220
Comments
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 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? |
@prabhatverma286 did the suggested workaround above work for you? |
@wkeeling apologies for taking so long to reply - got a bit busy. I tried the workaround again today and it works, so thank you so much! FWIW, my code below (I am using forward proxy as I am behind a firewall)
Which produces the output:
Took me a couple of tries though, because I remember trying it a few weeks ago and it didn't work. But most probably I was doing something wrong. One question though: the requests dictionary at the end of the output is empty. Any idea why? |
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
|
@prabhatverma286
what is the port at here?
what is kubernetes-service-name at here? if I'm using docker at here, what should I put at here? my docker-compose.yml is like
and current code is like
Appreciate your help in advance |
What is selenium-service-name here |
This is my working setup with proxy and chrome-node in Docker. from seleniumwire import webdriver
PROXY_IP = 'your proxy ip'
PROXY_PORT = 'your proxy port'
sw_options = {
'addr': "0.0.0.0",
'auto_config': False,
'port': 8087, # You can choose any other, the main thing is that it is free
# proxy settings
'proxy': {
'http': f'http://{PROXY_IP}:{PROXY_PORT}',
'https': f'https://{PROXY_IP}:{PROXY_PORT}'
}
}
chrome_options = webdriver.ChromeOptions()
# here should be port from sw_options['port']
chrome_options.add_argument('--proxy-server=host.docker.internal:8087')
chrome_options.add_argument('--ignore-certificate-errors')
driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub", # docker selenium-hub address
options=chrome_options,
seleniumwire_options=sw_options
) |
Hi, thanks @kaletvintsev for sharing. I am a bit confused about what addr, port, PROXY_IP and PROXY_PORT are supposed to target. Because:
Just to be has clear as possible:
|
Maybe I should open the PROXY_PORT in the docker-compose.yml config, so that the http request can go through? |
what is port: 8087, what is it for? @diyoyo |
I have no clue @danztensai , I just tried to use the code previous posted 😄 |
This is the port for the seleniumwire proxy. You can choose any other, the main thing is that it is free |
do I have to expose the port in the docker? @kaletvintsev |
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 theaddr
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
or0.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 withMessage: 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).The text was updated successfully, but these errors were encountered: