so I wonder what ports does Selenium uses with ChromeDriver to run? like from what port to what port selenium runs. Example 1500-3000
I'm trying to run selenium while using NordVpn but it says that it couldn't find any free port so I'm looking for a port list that selenium chromedriver uses to whitelist them in nordvpn and be able to run Selenium while using NordVPN
CodePudding user response:
You can use Chrome DevTools Protocol. Try with below steps:
- Add the path of the Chrome executable to the environment variable PATH.
- Launch Chrome with a custom flag, and open a port for remote debugging
Please make sure the path to chrome’s executable is added to the environment variable PATH. You can check it by running the command chrome.exe (on Windows) or Google/ Chrome ( on Mac). It should launch the Chrome browser.
If you get a similar message as below that means Chrome is not added to your system’s path:
'chrome' is not recognized as an internal or external command, operable program or batch file.
If this is the case, please feel free to Google how to add chrome to PATH?
Launch browser with custom flags
To enable Chrome to open a port for remote debugging, we need to launch it with a custom flag –
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\ChromeProfile"
For --remote-debugging-port
value you can specify any port that is open.
For --user-data-dir
flag you need to pass a directory where a new Chrome profile will be created. It is there just to make sure chrome launches in a separate profile and doesn’t pollute your default profile.
You can now play with the browser manually, navigate to as many pages, and perform actions and once you need your automation code to take charge, you may run your automation script. You just need to modify your Selenium script to make Selenium connect to that opened browser.
You can verify if Chrome is launched in the right way:
- Launch a new browser window normally (without any flag), and navigate to http://127.0.0.1:9222
- Confirm if you see the Google homepage reference in the second browser window
Launch browser with options
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
#Change chrome driver path accordingly
chrome_driver = "C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
print driver.title
The URL 127.0.0.1
denotes your localhost. We have supplied the same port, i.e 9222, that we used to launch Chrome with --remote-debugging-port
flag. While you can use any port, you need to make sure it is open and available to use.