關於監控網頁白屏時間所遇到的問題

之前嘗試使用Prometheus的webdriver_exporter 來監控網頁白屏時間,但是一直有問題,於是就自己寫了一個腳本來監控。
目前瞭解到的白屏時間 = responseStart - navigationStart
大致代碼如下:

from selenium import webdriver
source = "https://www.baidu.com"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path="/usr/local/bin/chromedriver")
driver.get(source)
navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
responseStart = driver.execute_script("return window.performance.timing.responseStart")
domComplete = driver.execute_script("return window.performance.timing.domComplete")
blanktime = responseStart - navigationStart
print("Back End:%s" % blanktime)
driver.quit()

在使用的過程中,可能還會遇到一個
“selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist” 或者 “The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.”
這兩個問題要看下 /opt/google/chrome/google-chrome 的末尾是不是:
exec -a "$0" "$HERE/chrome" "$@"

如果不是的話,請修改爲這樣的。 that's all

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章