簡體   English   中英

使用Firebug + NetExport + FireStarter的Selenium Webdriver沒有在Python中創建har文件

[英]Selenium Webdriver with Firebug + NetExport + FireStarter not creating a har file in Python

我目前正在使用Firebug,NetExport和(試用)FireStarter在Python中運行Selenium,試圖獲取URL的網絡流量。 我希望HAR文件出現在列出的目錄中,但不會出現任何內容。 當我在Firefox中測試它並通過UI時,會導出並保存一個HAR文件,因此我知道代碼本身按預期運行。 查看多個示例后,我看不到我錯過的內容。

我使用的是Firefox 29.0.1 Firebug 1.12.8 FireStarter 0.1a6 NetExport 0.9b6

還有其他人遇到過這個問題嗎? 我收到正確填寫的“webFile.txt”文件。

在查找每個版本的附加組件后,它們應該與我正在使用的Firefox版本兼容。 我嘗試使用Firefox 20版,但這沒有幫助。 我目前正在提取源代碼。

此外,我在使用和不使用FireStarter的情況下嘗試了它,並且我嘗試在兩種情況下手動刷新頁面以嘗試生成HAR。

我的代碼看起來像這樣:

import urllib2
import sys
import re
import os
import subprocess
import hashlib
import time
import datetime
from browsermobproxy import Server
from selenium import webdriver
import selenium
a=[];
theURL='';
fireBugPath = '/Users/tai/Documents/workspace/testSelenium/testS/firebug.xpi';
netExportPath = '/Users/tai/Documents/workspace/testSelenium/testS/netExport.xpi';
fireStarterPath = '/Users/tai/Documents/workspace/testSelenium/testS/fireStarter.xpi';

profile = webdriver.firefox.firefox_profile.FirefoxProfile();
profile.add_extension( fireBugPath);
profile.add_extension(netExportPath);
profile.add_extension(fireStarterPath);

#firefox preferences
profile.set_preference("app.update.enabled", False)  
profile.native_events_enabled = True
profile.set_preference("webdriver.log.file", "/Users/tai/Documents/workspace/testSelenium/testS/webFile.txt")
profile.set_preference("extensions.firebug.DBG_STARTER", True);

profile.set_preference("extensions.firebug.currentVersion", "1.12.8");
profile.set_preference("extensions.firebug.addonBarOpened", True);
profile.set_preference("extensions.firebug.addonBarOpened", True);
profile.set_preference('extensions.firebug.consoles.enableSite', True)                          


profile.set_preference("extensions.firebug.console.enableSites", True);
profile.set_preference("extensions.firebug.script.enableSites", True);
profile.set_preference("extensions.firebug.net.enableSites", True);
profile.set_preference("extensions.firebug.previousPlacement", 1);
profile.set_preference("extensions.firebug.allPagesActivation", "on");
profile.set_preference("extensions.firebug.onByDefault", True);
profile.set_preference("extensions.firebug.defaultPanelName", "net");

#set net export preferences
profile.set_preference("extensions.firebug.netexport.alwaysEnableAutoExport", True);
profile.set_preference("extensions.firebug.netexport.autoExportToFile", True);
profile.set_preference("extensions.firebug.netexport.saveFiles", True);

profile.set_preference("extensions.firebug.netexport.autoExportToServer", False);
profile.set_preference("extensions.firebug.netexport.Automation", True);
profile.set_preference("extensions.firebug.netexport.showPreview", False);
profile.set_preference("extensions.firebug.netexport.pageLoadedTimeout", 15000);
profile.set_preference("extensions.firebug.netexport.timeout", 10000);

profile.set_preference("extensions.firebug.netexport.defaultLogDir", "/Users/tai/Documents/workspace/testSelenium/testS/har");
profile.update_preferences();
browser = webdriver.Firefox(firefox_profile=profile);


def openURL(url,s):
    theURL = url;
    time.sleep(6);
    #browser = webdriver.Chrome();
    browser.get(url); #load the url in firefox
    time.sleep(3); #wait for the page to load
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight/5);")
    time.sleep(1); #wait for the page to load
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4);")
    time.sleep(1); #wait for the page to load
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3);")
    time.sleep(1); #wait for the page to load
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")
    time.sleep(1); #wait for the page to load
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    searchText='';
    time.sleep(20); #wait for the page to load
    if(s.__len__() >0):
        for x in range(0, s.__len__()):
            searchText+= (""  + browser.find_element_by_id(x));
    else:
        searchText+= browser.page_source;

        a=getMatches(searchText)
    #print ("\n".join(swfLinks));
    print('\n'.join(removeNonURL(a)));
   # print(browser.page_source);
    browser.quit();
    return a;
def found_window(name):
        try: browser.switch_to_window(name)
        except NoSuchWindowException:
             return False
        else:
             return True # found window
def removeFirstQuote(tex):
    for x in tex:
        b = x[1:];
        if not b in a:
            a.append(b);
    return a;
def getMatches(t):
    return removeFirstQuote(re.findall('([\"|\'][^\"|\']*\.swf)', t));
def removeNonURL(t):
    a=[];
    for b in t:
        if(b.lower()[:4] !="http" ):
            if(b[0] == "//"):
                a.append(theURL+b[2:b.__len__()]);
            else:
                while(b.lower()[:4] !="http" and b.__len__() >5):
                    b=b[1:b.__len__()];
                a.append(b);
        else:
            a.append(b);
    return a;

openURL("http://www.chron.com",a);

我通過在關閉瀏覽器之前設置更長的等待來解決此問題。 我認為你現在正在設置退出后將netexport設置為導出,因此沒有寫入文件。 造成這種情況的一行是:

profile.set_preference("extensions.firebug.netexport.pageLoadedTimeout", 15000);

netexport源代碼我們得到pageLoadedTimeout是“在聲明加載頁面的最后一個頁面請求之后等待的毫秒數”。 所以我懷疑你的所有次要頁面加載都阻止了netexport有足夠的時間來編寫文件。 需要注意的是,您將系統設置為在10秒后自動導出,因此我不確定您為什么不獲取半載json文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM