selenium設(shè)置瀏覽器參數(shù)(分辨率/不加載圖片和css/不提供可視化界面/不等待頁面一直加載)

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

 # get直接返回,不再等待界面加載完成(避免網(wǎng)頁一直加載出現(xiàn)超時(shí),后面可配合WebDriverWait等待某個(gè)元素出現(xiàn)使用)可參考2020.01.06 DY項(xiàng)目
 desired_capabilities = DesiredCapabilities.CHROME
 desired_capabilities["pageLoadStrategy"] = "none"

# 創(chuàng)建chrome參數(shù)對(duì)象
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')  # 解決DevToolsActivePort文件不存在的報(bào)錯(cuò)
options.add_argument('window-size=1600x900')  # 指定瀏覽器分辨率
options.add_argument('--disable-gpu')  # 谷歌文檔提到需要加上這個(gè)屬性來規(guī)避bug
# options.add_argument('--hide-scrollbars')  # 隱藏滾動(dòng)條, 應(yīng)對(duì)一些特殊頁面
options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 開啟管理者模式
# options.add_argument('blink-settings=imagesEnabled=false')  # 不加載圖片, 提升速度
# 禁止圖片和css加載
prefs = {"profile.managed_default_content_settings.images": 2, 'permissions.default.stylesheet': 2}
options.add_experimental_option("prefs", prefs)
# options.add_argument('--headless')  # 瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會(huì)啟動(dòng)失敗
browser.set_page_load_timeout(timeout)
browser.set_script_timeout(timeout)  # 這兩種設(shè)置都進(jìn)行才有效

browser = webdriver.Chrome(executable_path='/root/桌面/youlv/driver/chromedriver',chrome_options=options, desired_capabilities=desired_capabilities)
# browser = webdriver.Firefox(executable_path='/usr/bin/geckodriver')
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容