def get_chrome_options():
    options = GoogleOptions()
    experimentalFlags = ['same-site-by-default-cookies@2', 'cookies-without-same-site-must-be-secure@2']
    LocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
    options.add_experimental_option('localState', LocalStatePrefs)
    #options.add_argument("--start-fullscreen")
    #options.add_argument("--kiosk")
    #options.add_argument("--window-size=1640,860")
    #options.add_argument('headless')         # 静默
    options.add_argument('disable-infobars')  # 用selenium启动浏览器出现‘Chrome正在受到自动软件的控制’
    options.add_argument('blink-settings=imagesEnabled=false')  # 禁止图片加载
    #设置图片不加载
    prefs = {
        'profile.default_content_setting_values': {
            'images': 2
        }
    }
    #options.add_experimental_option('prefs', prefs)
    #options.add_argument("user-data-dir=selenium") #携带Cookie
    
    return options

def get_headlesschrome_options():
    options = GoogleOptions()
    
    experimentalFlags = ['same-site-by-default-cookies@2', 'cookies-without-same-site-must-be-secure@2']
    LocalStatePrefs = {'browser.enabled_labs_experiments': experimentalFlags}
    options.add_experimental_option('localState', LocalStatePrefs)
    
    #options.add_argument("--start-maximized")
    #options.add_argument("--start-fullscreen")
    #options.add_argument("--kiosk")
    options.add_argument('--headless')         # 静默
    options.add_argument("--window-size=1640,860")
    #options.add_argument('disable-infobars')  # 用selenium启动浏览器出现‘Chrome正在受到自动软件的控制’
    options.add_argument('blink-settings=imagesEnabled=false')  # 禁止图片加载
    #设置图片不加载
    prefs = {
        'profile.default_content_setting_values': {
            'images': 2
        }
    }
    options.add_experimental_option('prefs', prefs)
    #options.add_argument("user-data-dir=selenium") #携带Cookie
    return options
    
def get_headlessFirefox_Options():
    options = FirefoxOptions()
    options.add_argument('--headless')         # 静默
    options.add_argument("--window-size=1640,860")
    return options

测试代码:

${options}=	get_headlesschrome_options			
${dirver}=	Create Webdriver	Chrome	alias=VHL	chrome_options=${options}
GO TO	${url}			
${options}=	get_headlessFirefox_Options		
${dirver}=	Create Webdriver	Firefox	firefox_options=${options}
posted on 2021-09-16 10:07  Old-Kang  阅读(298)  评论(0编辑  收藏  举报