python-3.xproxyselenium-chromedriverhar

Export HAR file using chrome QUIC protocol (HTTPS)


I am trying to export HAR file using Chrome and QUIC through python. QUIC protocol using TLS1.3 - so i can only use the protocol with the client certificate

Basically, i want to merge this 2 codes into one. Export a HAR file using chrome profile (to enable TLS1.3, HTTPS and QUIC)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

and

from browsermobproxy import Server
from selenium import webdriver
import os
import json
import urlparse

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
url = urlparse.urlparse (proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()

Solution

  • I think the simplest way to implement this scenario (capture a HAR file with the Chrome default profile) is using chrome-har-capturer command in Linux. Setup from the following link:

    https://github.com/cyrus-and/chrome-har-capturer

    After installing this you can start chrome from the command line like this:

    google-chrome --remote-debugging-port=9222
    

    capturing HAR file using the command (saving the HAR file into the current path):

    chrome-har-capturer -o <filename> <website>
    

    You can write a short program in python using 'os' and 'subprocess' libraries:

    import os
    import subprocess
    import time
    
    CHROME_TYPE = "google-chrome --remote-debugging-port=9222"
    FILE_NAME = "example.har"
    WEBSITE = " https://www.youtube.com"
    
    def main():
        # open new terminal for 'open chrome' command
        browserProcess = subprocess.Popen(CHROME_TYPE, stdout=subprocess.PIPE, shell=True)
        # wait for the chrome window opening
        time.sleep(3)
        # the action include only one line
        os.system("chrome-har-capturer -o "+FILE_NAME+WEBSITE)
        # optional:
        browserProcess.kill()
    
    if __name__ == "__main__":
        main()