Selenium automatically loads chrome plugins

Install plugin

Visit the google plug-in store: click here to go.
It is best to access google on the Internet, otherwise it may not open!

Insert picture description here

View installation path

1. Enter in the browser: chrome://extensions/ to view the installed plug-ins, as shown below:

Insert picture description here

2. Enter in the browser: chrome://version to view the local installation path of the plug-in, as shown below:

Insert picture description here

3. If you want to download the crx plug-in package, you need to install the download plug-in in advance: CRX Extractor/Downloader can directly search crx in the plug-in store to find the plug-in and install it!

Insert picture description here

4. Search for the plug-in you want, then right-click to find the download CRX, and load the crx plug-in:

The browser must not be set as a headless browser, otherwise the plug-in will not be loaded

options.add_extension('./exampleOfExtensionDownloadedToFolder.crx')

Set up the headless test as follows:

from selenium import webdriver
from selenium.webdriver .chrome.options import Options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless') 
chrome_options.add_extension('插件.crx')
dr = webdriver.Chrome(chrome_options=chrome_options)

The error message is as follows:
Insert picture description here

If you load the installed plug-in, you can set it as a headless browser:

chrome_options.add_argument("load-extension=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default/Extensions/bpgkfekflkgphdifpnehncchdfkhoahi/0.22_0")

Instance

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

import time
import random

def main():
	chrome_options = Options()
	chrome_options.add_argument('--no-sandbox')
	# chrome_options.add_argument('--headless')
	chrome_options.add_argument('--disable-dev-shm-usage')
	chrome_options.add_argument('--start-maximized')
	# 一般加载的路径为插件包下一级的版本号即可
	chrome_options.add_argument("load-extension=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default/Extensions/bpgkfekflkgphdifpnehncchdfkhoahi/0.22_0") # 这是重点
	# chrome_options.add_argument('blink-settings=imagesEnabled=false')
	chrome_options.add_argument('--disable-gpu')
	chrome_options.add_argument('--ignore-certificate-errors')
	driver = webdriver.Chrome(chrome_options=chrome_options)
	allwindows = driver.window_handles # 获取tab标签页
	# driver.switch_to_window(allwindows[0]) # 跳转至第1个
	driver.get('https://www.baidu.com')
	time.sleep(random.randint(1, 3))
	driver.quit()

if __name__ == '__main__':
	main()

Guess you like

Origin blog.csdn.net/Lin_Hv/article/details/112554880