<bdo id='tOLp4'></bdo><ul id='tOLp4'></ul>

      1. <small id='tOLp4'></small><noframes id='tOLp4'>

        <i id='tOLp4'><tr id='tOLp4'><dt id='tOLp4'><q id='tOLp4'><span id='tOLp4'><b id='tOLp4'><form id='tOLp4'><ins id='tOLp4'></ins><ul id='tOLp4'></ul><sub id='tOLp4'></sub></form><legend id='tOLp4'></legend><bdo id='tOLp4'><pre id='tOLp4'><center id='tOLp4'></center></pre></bdo></b><th id='tOLp4'></th></span></q></dt></tr></i><div id='tOLp4'><tfoot id='tOLp4'></tfoot><dl id='tOLp4'><fieldset id='tOLp4'></fieldset></dl></div>
        <tfoot id='tOLp4'></tfoot>

        <legend id='tOLp4'><style id='tOLp4'><dir id='tOLp4'><q id='tOLp4'></q></dir></style></legend>

        通过 Selenium chromedriver 进行 Python 代理身份验证

        Python proxy authentication through Selenium chromedriver(通过 Selenium chromedriver 进行 Python 代理身份验证)

            <bdo id='pm8vh'></bdo><ul id='pm8vh'></ul>
              <tbody id='pm8vh'></tbody>

            <small id='pm8vh'></small><noframes id='pm8vh'>

              • <legend id='pm8vh'><style id='pm8vh'><dir id='pm8vh'><q id='pm8vh'></q></dir></style></legend>
                1. <tfoot id='pm8vh'></tfoot>

                2. <i id='pm8vh'><tr id='pm8vh'><dt id='pm8vh'><q id='pm8vh'><span id='pm8vh'><b id='pm8vh'><form id='pm8vh'><ins id='pm8vh'></ins><ul id='pm8vh'></ul><sub id='pm8vh'></sub></form><legend id='pm8vh'></legend><bdo id='pm8vh'><pre id='pm8vh'><center id='pm8vh'></center></pre></bdo></b><th id='pm8vh'></th></span></q></dt></tr></i><div id='pm8vh'><tfoot id='pm8vh'></tfoot><dl id='pm8vh'><fieldset id='pm8vh'></fieldset></dl></div>
                  本文介绍了通过 Selenium chromedriver 进行 Python 代理身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  We tried for a few days to setup Proxy Authentication with selenium chromedriver in Python. We couldn't set ip up because Chrome throws a pop-up for authentication. Problem is that selenium can't switch to that window and so, can't type. The only solution that worked for us was using pyautogui which is a bad solution for us because we want to use the headless function.

                  Here are all the methods we tried:

                  driver.switch_to_window()
                  

                  driver.switch_to_active_element()
                  

                  driver.switch_to_alert()
                  

                  ActionChains(driver).send_keys("test").perform()
                  

                  driver.switch_to_alert()
                  

                  options = webdriver.ChromeOptions()
                  options.add_argument('--proxy-server=http://user:pass@3.223.68.195:31112')
                  

                  driver.get("https://user:pass@google.com")
                  

                  proxy = {'address': '3.209.253.119:31112',
                           'username': 'user',
                           'password': 'pass'}
                  
                  
                  capabilities = dict(DesiredCapabilities.CHROME)
                  capabilities['proxy'] = {'proxyType': 'MANUAL',
                                           'httpProxy': proxy['address'],
                                           'ftpProxy': proxy['address'],
                                           'sslProxy': proxy['address'],
                                           'noProxy': '',
                                           'class': "org.openqa.selenium.Proxy",
                                           'autodetect': False}
                  
                  capabilities['proxy']['socksUsername'] = proxy['username']
                  capabilities['proxy']['socksPassword'] = proxy['password']
                  
                  driver = webdriver.Chrome(executable_path="chromedriver.exe", desired_capabilities=capabilities)
                  driver.get("http://google.com")
                  

                  Any help would be really appreciated :)

                  解决方案

                  If you need to use a proxy with no authentication (no username or password) with python and Selenium library with chromedriver you usually use the following code:

                  chrome_options = webdriver.ChromeOptions()
                  chrome_options.add_argument('--proxy-server=%s' % hostname + ":" + port)
                  driver = webdriver.Chrome(chrome_options=chrome_options)
                  

                  It works fine unless proxy requires authentication. if the proxy requires you to log in with a username and password it will not work. In this case, you have to use more tricky solution that is explained below.

                  To set up proxy authentication we will generate a file and upload it to chromedriver dynamically using the following code below. This effectively create a chrome extension. This code configures selenium with chromedriver to use HTTP proxy that requires authentication with user/password pair.

                  import os
                  import zipfile
                  
                  from selenium import webdriver
                  
                  
                  def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT):
                      manifest_json = """
                      {
                      "version": "1.0.0",
                      "manifest_version": 2,
                      "name": "Chrome Proxy",
                      "permissions": [
                          "proxy",
                          "tabs",
                          "unlimitedStorage",
                          "storage",
                          "<all_urls>",
                          "webRequest",
                          "webRequestBlocking"
                      ],
                      "background": {
                          "scripts": ["background.js"]
                      },
                      "minimum_chrome_version":"22.0.0"
                      }
                      """
                  
                      background_js = """
                      var config = {
                          mode: "fixed_servers",
                          rules: {
                          singleProxy: {
                              scheme: "http",
                              host: "%s",
                              port: parseInt(%s)
                          },
                          bypassList: ["localhost"]
                          }
                      };
                  
                      chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
                  
                      function callbackFn(details) {
                          return {
                              authCredentials: {
                              username: "%s",
                              password: "%s"
                              }
                          };
                      }
                  
                      chrome.webRequest.onAuthRequired.addListener(
                              callbackFn,
                              {urls: ["<all_urls>"]},
                              ['blocking']
                      );
                      """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
                  
                  
                      def get_chromedriver(use_proxy=True, user_agent=USER_AGENT):
                          path = os.path.dirname(os.path.abspath(__file__))
                          chrome_options = webdriver.ChromeOptions()
                          if use_proxy:
                              pluginfile = 'proxy_auth_plugin.zip'
                              with zipfile.ZipFile(pluginfile, 'w') as zp:
                                  zp.writestr("manifest.json", manifest_json)
                                  zp.writestr("background.js", background_js)
                              chrome_options.add_extension(pluginfile)
                          if user_agent:
                              chrome_options.add_argument('--user-agent=%s' % USER_AGENT)
                          driver = webdriver.Chrome(
                              os.path.join(path, 'chromedriver'),
                              chrome_options=chrome_options)
                          return driver
                  
                      driver = get_chromedriver(use_proxy=True)
                      # driver.get('https://www.google.com/search?q=my+ip+address')
                      driver.get('https://httpbin.org/ip')
                  
                  
                  user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
                  create_chromedriver('192.168.3.2', 8080, 'user', 'pass', user_agent)
                  

                  Function get_chromedriver returns configured selenium web driver that you can use in your application. This code is tested and works just fine.

                  To implement this into your own code just call the function create_chromedriver with args of host, port, user, pass and user-agent. If you do not want to use proxy or user-agent just edit get_chromedriver accordingly so the args are false.

                  这篇关于通过 Selenium chromedriver 进行 Python 代理身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)

                      • <tfoot id='dILW7'></tfoot>
                          <tbody id='dILW7'></tbody>
                      • <small id='dILW7'></small><noframes id='dILW7'>

                        • <bdo id='dILW7'></bdo><ul id='dILW7'></ul>

                          <legend id='dILW7'><style id='dILW7'><dir id='dILW7'><q id='dILW7'></q></dir></style></legend>
                            <i id='dILW7'><tr id='dILW7'><dt id='dILW7'><q id='dILW7'><span id='dILW7'><b id='dILW7'><form id='dILW7'><ins id='dILW7'></ins><ul id='dILW7'></ul><sub id='dILW7'></sub></form><legend id='dILW7'></legend><bdo id='dILW7'><pre id='dILW7'><center id='dILW7'></center></pre></bdo></b><th id='dILW7'></th></span></q></dt></tr></i><div id='dILW7'><tfoot id='dILW7'></tfoot><dl id='dILW7'><fieldset id='dILW7'></fieldset></dl></div>