This commit is contained in:
Alexandr committed 2021-02-10 22:35:35 +03:00
1 parent b64a617615
commit 2631da9c10
3 files changed
+43 -5

No files matched your search

+11 -3
View File
@@ -1,5 +1,6 @@
import logging
import os.path
import os
import selenium.webdriver
@@ -8,6 +9,11 @@ import selenium_helper
def main():
logger = logging.getLogger(__name__)
if "FIREFOX_PROXY" not in os.environ:
logger.error("Setup FIREFOX_PROXY in environment and try again!")
raise SystemExit(1)
profile = selenium.webdriver.FirefoxProfile()
# отключаем предупреждение при открытии about:config
profile.set_preference("browser.aboutConfig.showWarning", False)
@@ -21,12 +27,14 @@ def main():
try:
selenium_helper_extention = selenium_helper.SeleniumHelperExtension(driver)
selenium_helper_extention.install(os.path.abspath("releases/selenium_helper-0.0.4-fx.xpi"))
proxy = "http://username:password@host:port"
selenium_helper_extention.install(os.path.abspath("releases/selenium_helper-0.0.6-fx.xpi"))
# http://username:password@host:port
proxy = os.environ["FIREFOX_PROXY"]
cookies = [
selenium_helper.Cookie(name="cookie", value="test", url="https://whoer.net")
]
selenium_helper_extention.begin_options().set_proxy(proxy).set_cookies(cookies).apply_options()
selenium_helper_extention.begin_options().push(proxy, cookies, {})
driver.get("https://whoer.net/")
input("Enter to exit...")
+32 -2
View File
@@ -9,7 +9,7 @@ import copy
import json
import urllib.parse
from functools import lru_cache
from typing import List
from typing import List, Dict
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Firefox
@@ -21,6 +21,7 @@ OPTION_PROXY_PORT = "proxy_port"
OPTION_PROXY_USERNAME = "proxy_username"
OPTION_PROXY_PASSWORD = "proxy_password"
OPTION_PROXY_COOKIES = "proxy_cookies"
OPTION_HEADERS = "headers"
class NoAddonException(Exception):
@@ -226,7 +227,8 @@ class SeleniumHelperExtension:
OPTION_PROXY_PORT: "port",
OPTION_PROXY_USERNAME: "username",
OPTION_PROXY_PASSWORD: "password",
OPTION_PROXY_COOKIES: "cookies"
OPTION_PROXY_COOKIES: "cookies",
OPTION_HEADERS: "headers"
}
def __init__(self, driver: Firefox, raise_for_about_config=True):
@@ -277,8 +279,11 @@ class SeleniumHelperExtension:
self._fill_option(OPTION_PROXY_TYPE, proxy.scheme)
self._fill_option(OPTION_PROXY_HOST, proxy.hostname)
self._fill_option(OPTION_PROXY_PORT, proxy.port)
try:
self._fill_option(OPTION_PROXY_USERNAME, proxy.username)
self._fill_option(OPTION_PROXY_PASSWORD, proxy.password)
except TypeError:
pass
return self
def set_cookies(self, cookies: List[Cookie], default_url=None, ignore_errors=False):
@@ -292,10 +297,35 @@ class SeleniumHelperExtension:
self._fill_option(OPTION_PROXY_COOKIES, value)
return self
def set_headers(self, headers: List[Dict[str, str]]):
"""устанавливаем заголовки браузера, для всех запросов."""
value = json.dumps(headers)
self._fill_option(OPTION_HEADERS, value)
return self
def push(self, proxy, cookies, headers):
if proxy:
proxy = urllib.parse.urlparse(proxy)
proxy = {
"type": proxy.scheme,
"host": proxy.hostname,
"port": proxy.port,
"username": proxy.username,
"password": proxy.password
}
js = f"""saveOptionsEx({json.dumps(proxy)}, {json.dumps(cookies)}, {json.dumps(headers)});"""
self._driver.execute_script(js)
return self
def apply_options(self):
"""сохраняем изменения сделанные на странице настроек"""
self._driver.find_element_by_id("submit").click()
def clear_cookies(self, cookies: List[Cookie], default_url=None, ignore_errors=False):
return self._clear_cookies(cookies, default_url, ignore_errors)
def _clear_cookies(self, cookies: List[Cookie], default_url=None, ignore_errors=False):
clear_value = []