【python+selenium自動化】設置Chrome啟動參數


起因:直接用selenium的webdriver啟動chrome,會彈出“Chrome正在受到自動軟件的控制”,並且窗口較小,是因為chrome沒有加載任何配置

解決:點進selenium的ChromeOptions源碼,可見其提供了如下方法

添加啟動參數即可,項目中的設置webdrier的代碼展示如下

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from apitest.uitest.UIMethod import getyamlconf


class DriverConfig:

    def driver_config(self):
        """
        瀏覽器驅動
        :return:
        """
        # 實例化ChromeOptions
        options = webdriver.ChromeOptions()
        # 關閉瀏覽器提示信息
        options.add_argument('disable-infobars')
        # 瀏覽器全屏
        options.add_argument('start-fullscreen')
        # 設置默認下載目錄
        download_path = getyamlconf.GetConf().get_joinpath() + r"\Requests\apitest\uitest\DownloadFile"
        prefs = {'download.default_directory': download_path}
        options.add_experimental_option('prefs', prefs)
        # 獲取谷歌瀏覽器所有控制台信息
        des = DesiredCapabilities.CHROME
        des['loggingPrefs'] = {'performance': 'ALL'}
        # 谷歌瀏覽器驅動路徑
        joinpath = getyamlconf.GetConf().get_joinpath()
        driverpath = joinpath + r'\Requests\apitest\uitest\WebDriver\chromedriver.exe'
        # 瀏覽器驅動
        driver = webdriver.Chrome(driverpath, options=options, desired_capabilities=des)
        # driver = webdriver.Remote(command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities=des,
        #                           options=options)
        implicitly_wait = getyamlconf.GetConf().get_implicitly_wait()
        driver.implicitly_wait(implicitly_wait)
        return driver

這里我添加了:關閉瀏覽器提示信息、瀏覽器全屏、設置默認下載目錄(用來處理文件下載后的比對)、控制台信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM