By default, Chrome's--proxy-server= "Http://ip:port" parameter does not support setting user name and password authentication. This makes "Selenium + Chrome Driver" Unable to use HTTP Basic authentication HTTP proxy. A flexible way is to use IP address authentication, but in the domestic network environment, most users are using ADSL form of network access, IP is changing, also can not use IP address binding authentication. So there is an urgent need to find a way for Chrome to automatically implement the HTTP proxy username password authentication scheme.
stackoverflow shared a program that uses chrome plugins to automate proxy user password authentication. Very good, detailed address http://stackoverflow.com/questions/ 9888323/how-to-override-basic-authentication-in-selenium2-with-java-using-chrome-driver.
Kun's technical staff on the basis of the idea of using Python to achieve the automated chrome plugin creation process, that is, according to the designated agent "Username:[email protected":p ort "automatically create a chrome proxy plug-in, and then you can in the" Selenium + Chrome Driver "in the installation of the plug-in implementation of the agent configuration function, the following code:
#Coding:utf-8#test.py#test "Selenium + Chrome" using a proxy with username and password AuthenticationImportOSImportReImport TimeImportZipFile fromSeleniumImportWebdriver#Chrome Proxy Template plug-in (https://github.com/RobinDev/Selenium-Chrome-HTTP-Private-Proxy) directoryChrome_proxy_helper_dir ='Chrome-proxy-helper'#directory where custom chrome proxy extension files are storedCustom_chrome_proxy_extensions_dir ='chrome-proxy-extensions'defget_chrome_proxy_extension (proxy):"""get a chrome proxy extension with the specified proxy (password authentication with username) proxy-specified proxy, format: Username:[email protected]:p ort"""m= Re.compile ('([^:]+):([^\@]+] \@ ([\d\.] +):(\d+)'). Search (proxy)ifm:#extract the parameters of the agentUsername =m.groups () [0] Password= M.groups () [1] IP= M.groups () [2] Port= M.groups () [3] #Create a custom chrome proxy extension (zip file) if notos.path.exists (Custom_chrome_proxy_extensions_dir): Os.mkdir (Custom_chrome_proxy_extensions_dir) ex Tension_file_path= Os.path.join (Custom_chrome_proxy_extensions_dir,'{}.zip'. Format (Proxy.replace (':','_'))) if notos.path.exists (extension_file_path):#extension file does not exist, creatingZF = ZipFile. ZipFile (Extension_file_path, mode='W') Zf.write (Os.path.join (Chrome_proxy_helper_dir,'Manifest.json'),'Manifest.json') #Replace agent parameters in a templatebackground_content = open (Os.path.join (Chrome_proxy_helper_dir,'Background.js') . Read () background_content= Background_content.replace ('%proxy_host', IP) background_content= Background_content.replace ('%proxy_port', port) background_content= Background_content.replace ('%username', username) background_content= Background_content.replace ('%password', password) zf.writestr ('Background.js', Background_content) zf.close ()returnExtension_file_pathElse: RaiseException ('Invalid proxy format. should be username:[email protected]:p ort')if __name__=='__main__': #TestOptions =Webdriver. Chromeoptions ()#Add a custom proxy plug-in (Configure a specific agent with user name password Authentication)Options.add_extension (Get_chrome_proxy_extension (proxy='Username:[email protected]:p ort')) Driver= Webdriver. Chrome (chrome_options=options)#visit an IP echo Web site to see if the proxy configuration is activeDriver.get ('HTTP://HTTPBIN.ORG/IP') PrintDriver.page_source Time.sleep (60) Driver.quit ()
The tests are as follows:
Selenium + Chrome diver using HTTP proxy with user name password authentication method