首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python在Selenium中使用用户名和密码验证代理

如何使用Python在Selenium中使用用户名和密码验证代理
EN

Stack Overflow用户
提问于 2020-09-15 05:11:54
回答 1查看 142关注 0票数 1

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

Proxy示例,

代码语言:javascript
复制
IP = xxx.xx.xx.xx
PORT = xxxxx
USERNAME = USERNAME
PASSWORD = PASSWORD

我使用了以下代码,

代码语言:javascript
复制
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)

我也尝试过一些其他的代码片段。我也尝试将值放入警告框中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-25 13:42:17

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

代码语言:javascript
复制
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方法中第二个参数的更多信息,代码如下:

代码语言:javascript
复制
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))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63891893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档