我正在尝试使用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)
我也尝试了其他一些代码片段。试图将值也放入警报框。那么有人可以帮助我解决这个问题吗?提前致谢。
您可以使用AutoIt来实现。并且它具有Python绑定PyAutoIt。使用PIP-安装PyAutoIt之后pip install 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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句