#python #selenium #proxy
#python #селен #прокси
Вопрос:
Я пытаюсь аутентифицировать прокси с именем пользователя и паролем в Selenium с помощью Python, но текущий код не работает. Я перепробовал много решений, но ни одно из них не сработало.
Пример прокси,
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)
Я также пробовал некоторые другие фрагменты кода. Я также попытался поместить значения в поля предупреждений.
Ответ №1:
Вы можете добиться этого с помощью AutoIt. И у него есть привязка к Python PyAutoIt. После того, как вы установили PyAutoIt с помощью PIP — 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
Для получения дополнительной информации о втором параметре в методе отправки, вот код,
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))