【问题标题】:How to authenticate proxy with username and password in Selenium using Python如何使用 Python 在 Selenium 中使用用户名和密码对代理进行身份验证
【发布时间】:2020-09-14 21:11:53
【问题描述】:

我正在尝试使用 Python 在 Selenium 中使用用户名和密码对代理进行身份验证,但当前代码不起作用。我尝试了很多解决方案,但都没有奏效。

代理示例,

IP = xxx.xx.xx.xx
PORT = xxxxx
USERNAME = USERNAME
PASSWORD = PASSWORD

我使用了以下代码,

driver.execute_script("""
          Services.prefs.setIntPref('network.proxy.type', 1);
          Services.prefs.setCharPref("network.proxy.http", arguments[0]);
          Services.prefs.setIntPref("network.proxy.http_port", arguments[1]);
          Services.prefs.setCharPref("network.proxy.ssl", arguments[0]);
          Services.prefs.setIntPref("network.proxy.ssl_port", arguments[1]);
          Services.prefs.setCharPref('network.proxy.socks', arguments[4]);
          Services.prefs.setIntPref('network.proxy.socks_port', arguments[5]);
          Services.prefs.setCharPref('network.proxy.socks_username', arguments[6]);
          Services.prefs.setCharPref('network.proxy.socks_password', arguments[7]);
          """, http_addr, http_port, ssl_addr, ssl_port, socks_addr, socks_port, socks_username, socks_password)

我也尝试了其他一些代码 sn-ps。我也尝试将值放入警告框中。

【问题讨论】:

    标签: python selenium proxy


    【解决方案1】:

    您可以使用AutoIt 来实现此目的。它具有 Python 绑定 PyAutoIt。一旦你使用 PIP - pip install PyAutoIt 安装了 PyAutoIt,下面的代码就可以完成你的工作。

    import autoit
    
    autoit.win_wait_active("Authentication Required") # title of the dialog box to wait. so it will wait for the Authentication Required dialog
    autoit.send("username", 1) # second parameter is the mode (changes how "keys" is processed)
    autoit.send("{TAB}") # press tab key to go to the password field
    autoit.send("password", 1)
    autoit.send("{Enter}") # press enter key
    

    关于send方法中第二个参数的更多信息,这里是代码,

    def send(send_text, mode=0):
        """
        Sends simulated keystrokes to the active window.
        :param send_text:
        :param mode: Changes how "keys" is processed:
            flag = 0 (default), Text contains special characters like + and ! to
             indicate SHIFT and ALT key presses.
            flag = 1, keys are sent raw.
        :return:
        """
        AUTO_IT.AU3_Send(LPCWSTR(send_text), INT(mode))
    

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 2016-11-13
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2021-12-14
      相关资源
      最近更新 更多