MCPcopy Create free account
hub / github.com/cryzed/Selenium-Requests / get_webdriver_request_headers

Function get_webdriver_request_headers

seleniumrequests/request.py:53–88  ·  view source on GitHub ↗
(webdriver, proxy_host="127.0.0.1")

Source from the content-addressed store, hash-verified

51
52
53def get_webdriver_request_headers(webdriver, proxy_host="127.0.0.1"):
54 # There's a small chance that the port was taken since the call of get_unused_port(), so make sure we try as often
55 # as needed
56 while True:
57 port = get_unused_port()
58 try:
59 server = http.server.HTTPServer(("", port), HTTPRequestHandler)
60 break
61 except socket.error:
62 pass
63
64 threading.Thread(target=server.serve_forever, daemon=True).start()
65 original_window_handle = webdriver.current_window_handle
66 webdriver.execute_script("window.open('http://%s:%d/', '_blank');" % (proxy_host, port))
67
68 UPDATER_HEADERS_MUTEX.acquire()
69 # XXX: .shutdown() seems to block indefinitely and not shut down the server
70 # server.shutdown()
71
72 # Not optional: Make sure that the webdriver didn't switch the window handle to the newly opened window. Behaviors
73 # of different webdrivers seem to differ here. Workaround for Firefox: If a new window is opened via JavaScript as a
74 # new tab, requesting self.current_url never returns. Explicitly switching to the current window handle again seems
75 # to fix this issue.
76
77 # Workaround for buggy Chrome/Chromedriver combination: https://github.com/cryzed/Selenium-Requests/issues/53
78 needs_chrome_workaround = webdriver.name == "chrome" and int(webdriver.capabilities["browserVersion"].split(".", maxsplit=1)[0]) >= 106
79 if not needs_chrome_workaround:
80 webdriver.switch_to.window(original_window_handle)
81
82 global HEADERS
83 headers = HEADERS
84 HEADERS = None
85
86 # Remove the host header, which will simply contain the localhost address of the HTTPRequestHandler instance
87 del headers["host"]
88 return headers
89
90
91def prepare_requests_cookies(webdriver_cookies):

Callers 1

requestMethod · 0.85

Calls 1

get_unused_portFunction · 0.85

Tested by

no test coverage detected