Python – How to add multiple extensions to the selenium chrome webdriver

How to add multiple extensions to the selenium chrome webdriver… here is a solution to the problem.

How to add multiple extensions to the selenium chrome webdriver

How do I add multiple extensions to the Selenium Chrome Webdriver?

I tried adding an extension with a comma but got an error

chrome_options.add_extension(r'C:\Users\Administrator\Desktop\chromedriver_win32\extension.crx',r'C:\Users\Administrator\Desktop\chromedriver_win32\ extension1.crx',,r'C:\Users\Administrator\Desktop\chromedriver_win32\extension3.crx')

But there is an error

Mine with a working code with an extension :

import os
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import gmtime, strftime
from os import system
import pyperclip
import datetime
import time
import os
import sys

executable_path = r"C:\Users\Administrator\Desktop\chromedriver_win32\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension(r'C:\Users\Administrator\Desktop\chromedriver_win32\extension.crx')
driver = webdriver. Chrome(executable_path=executable_path, chrome_options=chrome_options)
time.sleep(4)
driver.get("https://www.tradingview.com/chart/gK6Rq0UH/")
time.sleep(4)
driver.find_element_by_xpath("""//*[@id="footer-chart-panel"]/div[2]/span[4]""").click()
time.sleep(4)
while True:
    driver.find_element_by_xpath("""//*[@id="bottom-area"]/div[2]/div[1]/div[2]/ul/li[4]""").click()
    time.sleep(4)
    driver.switch_to_alert().accept()
    contents = pyperclip.paste()
    time.sleep(2)
    filepath = r"C:\Users\Administrator\Desktop\DATA.txt"
    time.sleep(2)
    with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
        f.write(contents)

Solution

For each extension you want to add, you must call chrome_options.add_extension:: like this

chrome_options.add_extension('Path/to/extension/one')
chrome_options.add_extension('Path/to/extension/two')

Related Problems and Solutions