Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - How to capture network traffic with selenium

I'm starting a new Django project, I'm trying to capture the network traffic with Selenium.

I have already achieved this objective with Selenium-wire (MITM Proxy), but Django doesn't like to work with selenium-wire ( must start the server with "--nothreading --noreload", connection bug... ). I'm looking for achieve this with modern solutions, like parsing the network devtools of firefox directly or with a firefox addons. I'm using Firefox Geckodriver for my test.

    for x in range(0, 10):
                profile = profile_firefox()
                options = options_firefox()
                driver = webdriver.Firefox(firefox_profile=profile, options=options, executable_path='/Users/*****/Desktop/selenium-master/headless_browser/geckodriver')
                try:
                        driver.set_window_position(0, 0)
                        driver.set_window_size(randint(1024, 2060), randint(1024, 4100))
                        time.sleep(randint(3,10))
                        driver.get(url)
                        wait = ui.WebDriverWait(driver, 10)
                        time.sleep(randint(8,10))
                        if driver.find_element_by_xpath("//*[@id="container"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button"):
                                driver.find_element_by_xpath("//*[@id="container"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
                                del driver.requests
                                time.sleep(randint(8,10))
                                driver.find_element_by_xpath("//*[@id="container"]/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
                                time.sleep(randint(10,20))
                                for request in driver.requests:
                                        if request.path == "https://api.*********.**/*******/*******":
                                                request_api = request
                                                raw = str(request_api.body)
                                                request_api = raw.split(('b''))
                                                payload_raw = request_api[1]
                                                payload = payload_raw[:-1]
                                                if payload:
                                                        header = request.headers
                                                        time.sleep(8)
                                                        break

                except:
                        print("Houston on a eu un probleme")
                        firefox_closing(driver)

Edit :


def profile_firefox():
        profile = FirefoxProfile()
        profile.set_preference('permissions.default.image', 2)
        profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
        profile.set_preference("general.useragent.override", firefox_init())
        profile.set_preference('network.proxy.type', 1)
        profile.set_preference('network.proxy.socks', 'localhost')
        profile.set_preference('network.proxy.socks_port', 9050)
        profile.set_preference("network.proxy.socks_remote_dns", False)
        profile.set_preference("driver.privatebrowsing.autostart", True)
        profile.update_preferences()
        return profile


Test 2 with Socks,HTTP,SSL configuration :


server = Server('/Users/*****/Desktop/selenium-master/browsermob-proxy-2.1.4/bin/browsermob-proxy')
server.start()
proxy = server.create_proxy()
proxy.selenium_proxy()#Dont understand what it does ???
port = int(proxy.port)

profile = FirefoxProfile()
profile.set_preference('permissions.default.image', 2)
profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
profile.set_preference('general.useragent.override', firefox_init())
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', 'localhost')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference('network.proxy.ssl', 'localhost')
profile.set_preference('network.proxy.ssl_port', port)
profile.set_preference('network.proxy.http', 'localhost')
profile.set_preference('network.proxy.http_port', port)
profile.set_preference('network.proxy.socks_remote_dns', False)
profile.set_preference('driver.privatebrowsing.autostart', True)
profile.update_preferences()


It seems Http proxy override the socks configuration...

Thanks a lot if you have any clue or advice about my code or solutions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a proxy to catch the network traffic. browsermob-proxy works well with selenium in Python. You need to download browsermob executable before. This is the piece of code with Firefox :

from browsermobproxy import Server
from selenium import webdriver

server = Server('path_to_executable')
server.start()
proxy = server.create_proxy()
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("file_name", options={'captureHeaders': True, 'captureContent': True})
driver.get("your_url")
proxy.wait_for_traffic_to_stop(1, 60)
for ent in proxy.har['log']['entries']:
    print(ent)

server.stop()
driver.quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...