本文整理汇总了Python中selenium.webdriver.ChromeOptions方法的典型用法代码示例。如果您正苦于以下问题:Python webdriver.ChromeOptions方法的具体用法?Python webdriver.ChromeOptions怎么用?Python webdriver.ChromeOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver
的用法示例。
在下文中一共展示了webdriver.ChromeOptions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_browser
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def get_browser():
if "browser" not in g:
options = webdriver.ChromeOptions()
options.add_argument("no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=800,600")
options.add_argument("--disable-dev-shm-usage")
options.set_headless()
host = "chrome" if current_app.config["DEBUG"] else "127.0.0.1"
g.browser = webdriver.Remote(
command_executor=f"http://{host}:4444/wd/hub",
desired_capabilities=DesiredCapabilities.CHROME,
options=options,
)
return g.browser
示例2: driver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def driver():
options = webdriver.ChromeOptions()
options.add_argument("disable-gpu")
options.add_argument("headless")
options.add_argument("no-default-browser-check")
options.add_argument("no-first-run")
options.add_argument("no-sandbox")
d = DesiredCapabilities.CHROME
d["loggingPrefs"] = {"browser": "ALL"}
driver = webdriver.Chrome(options=options, desired_capabilities=d)
driver.implicitly_wait(30)
yield driver
driver.quit()
示例3: __init__
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def __init__(self):
super(HeadlessChromeLocust, self).__init__()
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size={}x{}'.format(
self.screen_width, self.screen_height
))
options.add_argument('disable-gpu')
if self.proxy_server:
_LOGGER.info('Using proxy: ' + self.proxy_server)
options.add_argument('proxy-server={}'.format(self.proxy_server))
driver = webdriver.Chrome(chrome_options=options)
_LOGGER.info('Actually trying to run headless Chrome')
self.client = RealBrowserClient(
driver,
self.timeout,
self.screen_width,
self.screen_height,
set_window=False
)
示例4: test_operadriver_manager_with_selenium
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def test_operadriver_manager_with_selenium():
driver_path = OperaDriverManager().install()
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
if get_os_type() in ["win64", "win32"]:
paths = [f for f in glob.glob(f"C:/Users/{os.getlogin()}/AppData/" \
"Local/Programs/Opera/**",
recursive=True)]
for path in paths:
if os.path.isfile(path) and path.endswith("opera.exe"):
options.binary_location = path
elif ((get_os_type() in ["linux64", "linux32"]) and not
os.path.exists('/usr/bin/opera')):
options.binary_location = "/usr/bin/opera"
elif get_os_type() in "mac64":
options.binary_location = "/Applications/Opera.app/Contents/MacOS/Opera"
ff = webdriver.Opera(executable_path=driver_path, options=options)
ff.get("http://automation-remarks.com")
ff.quit()
示例5: _open_webdriver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def _open_webdriver(self): # 该函数同时作为重启 webdriver 功能使用
try: self.spider_closed()
except: pass
from selenium import webdriver
option = webdriver.ChromeOptions()
extset = ['enable-automation', 'ignore-certificate-errors']
ignimg = "profile.managed_default_content_settings.images"
mobile = {'deviceName':'Galaxy S5'}
option.add_argument("--disable-infobars") # 旧版本关闭“chrome正受到自动测试软件的控制”信息
option.add_experimental_option("excludeSwitches", extset) # 新版本关闭“chrome正受到自动测试软件的控制”信息
option.add_experimental_option("useAutomationExtension", False) # 新版本关闭“请停用以开发者模式运行的扩展程序”信息
# option.add_experimental_option('mobileEmulation', mobile) # 是否使用手机模式打开浏览器
# option.add_experimental_option("prefs", {ignore_image: 2}) # 开启浏览器时不加载图片(headless模式该配置无效)
# option.add_argument('--start-maximized') # 开启浏览器时是否最大化(headless模式该配置无效)
# option.add_argument('--headless') # 无界面打开浏览器
# option.add_argument('--window-size=1920,1080') # 无界面打开浏览器时候只能用这种方式实现最大化
# option.add_argument('--disable-gpu') # 禁用 gpu 硬件加速
# option.add_argument("--auto-open-devtools-for-tabs") # 开启浏览器时候是否打开开发者工具(F12)
# option.add_argument("--user-agent=Mozilla/5.0 HELL") # 修改 UA 信息
# option.add_argument('--proxy-server=http://127.0.0.1:8888') # 增加代理
self.webdriver = webdriver.Chrome(chrome_options=option)
示例6: get_chromedriver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def get_chromedriver(use_proxy=False, user_agent=None):
# path = os.path.dirname(os.path.abspath(__file__)) # 如果没有把chromedriver放到python\script\下,则需要指定路径
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome(
# os.path.join(path, 'chromedriver'),
chrome_options=chrome_options)
return driver
示例7: __init__
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def __init__(self, cookies_list):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
}
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 不加载图片,加快访问速度
option.add_argument('--headless')
self.driver = webdriver.Chrome(options=option)
self.driver.get('https://i.taobao.com/my_taobao.htm')
for i in cookies_list:
self.driver.add_cookie(cookie_dict=i)
self.driver.get('https://i.taobao.com/my_taobao.htm')
self.wait = WebDriverWait(self.driver, 10) # 超时时长为10s
# 模拟向下滑动浏览
示例8: chrome
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def chrome(headless=False):
# support to get response status and headers
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = {'performance': 'ALL'}
opt = webdriver.ChromeOptions()
if headless:
opt.add_argument("--headless")
opt.add_argument("--disable-xss-auditor")
opt.add_argument("--disable-web-security")
opt.add_argument("--allow-running-insecure-content")
opt.add_argument("--no-sandbox")
opt.add_argument("--disable-setuid-sandbox")
opt.add_argument("--disable-webgl")
opt.add_argument("--disable-popup-blocking")
# prefs = {"profile.managed_default_content_settings.images": 2,
# 'notifications': 2,
# }
# opt.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(options=opt,desired_capabilities=d)
browser.implicitly_wait(10)
browser.set_page_load_timeout(20)
return browser
示例9: headless
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def headless(self, path: str, proxy: str = "") -> None:
ua = UserAgent()
userAgent = ua.random
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument("window-size=1500,1200")
options.add_argument("no-sandbox")
options.add_argument("disable-dev-shm-usage")
options.add_argument("disable-gpu")
options.add_argument("log-level=3")
options.add_argument(f"user-agent={userAgent}")
if proxy != "":
self.proxy = True
options.add_argument("proxy-server={}".format(proxy))
self.driver = webdriver.Chrome(path, chrome_options=options)
self.set_config()
self._headless = True
示例10: _make
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def _make(self, debug):
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.binary_location = settings.SELENIUM_CUSTOM_CHROME_PATH
browser = webdriver.Chrome(settings.SELENIUM_CHROMEDRIVER_PATH, options=options)
browser.get('file://' + os.path.abspath(os.path.join(self.dir, 'input.html')))
self.log = self.get_log(browser)
try:
WebDriverWait(browser, 15).until(EC.presence_of_element_located((By.CLASS_NAME, 'math-loaded')))
except TimeoutException:
logger.error('PDF math rendering timed out')
self.log = self.get_log(browser) + '\nPDF math rendering timed out'
return
response = browser.execute_cdp_cmd('Page.printToPDF', self.template)
self.log = self.get_log(browser)
if not response:
return
with open(os.path.abspath(os.path.join(self.dir, 'output.pdf')), 'wb') as f:
f.write(base64.b64decode(response['data']))
self.success = True
示例11: init_driver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def init_driver(offline_mode=False):
"""
Creates headless Google Chrome Web Driver
"""
demisto.debug(f'Creating chrome driver. Mode: {"OFFLINE" if offline_mode else "ONLINE"}')
try:
chrome_options = webdriver.ChromeOptions()
for opt in merge_options(DEFAULT_CHROME_OPTIONS, USER_CHROME_OPTIONS):
chrome_options.add_argument(opt)
driver = webdriver.Chrome(options=chrome_options, service_args=[
f'--log-path={DRIVER_LOG}',
])
if offline_mode:
driver.set_network_conditions(offline=True, latency=5, throughput=500 * 1024)
except Exception as ex:
return_error(f'Unexpected exception: {ex}\nTrace:{traceback.format_exc()}')
demisto.debug('Creating chrome driver - COMPLETED')
return driver
示例12: load_driver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def load_driver(settings):
"""
Load the Selenium driver depending on the browser
(Edge and Safari are not running yet)
"""
driver = None
if settings['browser'] == 'firefox':
firefox_profile = webdriver.FirefoxProfile(settings['browser_path'])
driver = webdriver.Firefox(firefox_profile)
elif settings['browser'] == 'chrome':
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('user-data-dir=' +
settings['browser_path'])
driver = webdriver.Chrome(options=chrome_options)
elif settings['browser'] == 'safari':
pass
elif settings['browser'] == 'edge':
pass
return driver
示例13: get_page
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def get_page(url):
chrome_options = webdriver.ChromeOptions()
ua_argument = 'User-Agent="'+GetUserAgent()+'"'
chrome_options.add_argument(ua_argument)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--incognito')
chrome_options.add_argument('log-level=3')
try:
driver = webdriver.Chrome(chrome_options=chrome_options)
#driver.set_page_load_timeout(6)
# driver.set_script_timeout(6)
driver.get(url)
# time.sleep(0.5)
driver.quit()
except:
driver.quit()
print("timeout")
示例14: driver
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def driver(self, browser):
if browser == 'firefox':
return webdriver.Firefox()
if browser == 'chrome':
return webdriver.Chrome('chromedriver.exe')
if browser == 'opera':
# TODO: Opera implementation is quite buggy annoyingly. It won't close at the moment
# Need to investigate.
options = webdriver.ChromeOptions()
options.binary_location = "C:\\Program Files\\Opera\\launcher.exe"
return webdriver.Opera(executable_path='operadriver.exe', opera_options=options)
if browser == 'ie':
return webdriver.Ie()
if browser == 'edge':
# TODO: check for Windows < 8.1?
return webdriver.Edge()
if browser == 'phantom':
return webdriver.PhantomJS()
raise XVEx("{} is not supported on {}".format(browser, self._device.os_name()))
示例15: __init__
# 需要导入模块: from selenium import webdriver [as 别名]
# 或者: from selenium.webdriver import ChromeOptions [as 别名]
def __init__(self, executable_path="chromedriver", dpi=96.0, **kwargs):
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument("disable-gpu")
options.add_argument("disable-infobars")
options.add_argument("no-sandbox")
options.add_argument("disable-dev-shm-usage")
options.add_argument("enable-experimental-web-platform-features")
options.add_argument("default-background-color FFFFFF00")
self.driver = webdriver.Chrome(
executable_path=executable_path,
options=options)
self.dpi = dpi
self.driver.execute_cdp_cmd(
"Emulation.setDefaultBackgroundColorOverride",
{'color': {'r': 255, 'g': 255, 'b': 255, 'a': 0}}
)