update
This commit is contained in:
1 parent
b64a617615
commit
2631da9c10
3 files changed
+48
-10
No files matched your search
+11
-3
@@ -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...")
|
||||
|
||||
@@ -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):
|
||||
@@ -142,18 +143,18 @@ class Cookie(dict):
|
||||
@path.deleter
|
||||
def path(self):
|
||||
del self["path"]
|
||||
|
||||
|
||||
@property
|
||||
def same_site(self):
|
||||
"""A cookies.SameSiteStatus value that indicates the SameSite state of the cookie.
|
||||
If omitted, it defaults to 0, 'no_restriction'.
|
||||
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/cookies/SameSiteStatus"""
|
||||
return self.get("sameSite")
|
||||
|
||||
|
||||
@same_site.setter
|
||||
def same_site(self, value):
|
||||
self["sameSite"] = value
|
||||
|
||||
|
||||
@same_site.deleter
|
||||
def same_site(self):
|
||||
del self["sameSite"]
|
||||
@@ -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)
|
||||
self._fill_option(OPTION_PROXY_USERNAME, proxy.username)
|
||||
self._fill_option(OPTION_PROXY_PASSWORD, proxy.password)
|
||||
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 = []
|
||||
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user