Selenium

Tue 25 January 2022


Setting the experimental flags is really unintuitive. They are local preferences, but you have to suffix them with @1 for enable or @2 for disable.

Master Preference Keys https://chromium.googlesource.com/chromium/src/+/refs/heads/main/chrome/common/pref_names.cc

import sys

# don't create __pycache__ as chrome extension doesn't like it
sys.dont_write_bytecode = True

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service


def main(argv):
  extension_path = '/Users/joetoth/projects/sideler'
  driver_path = '/Users/joetoth/.local/share/virtualenvs/joetoth.com-s5in2Toy/bin/chromedriver_mac64'
  chrome_path = "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta"
  service_object = Service(driver_path)
  options = webdriver.ChromeOptions()
  options.add_argument(
      "whitelisted-extension-id=pisdfjhhwihiwihfaj")
  options.add_argument(f"load-extension={extension_path}")
  options.add_argument("enable-logging")
  options.add_argument("v=1")
  options.add_argument("auto-open-devtools-for-tabs")
  options.binary_location = chrome_path

  # Set localState preferences.
  experimentalFlags = ['side-panel@1']
  chromeLocalStatePrefs = {
      'browser.enabled_labs_experiments': experimentalFlags,
  }
  options.add_experimental_option('localState', chromeLocalStatePrefs)

  @ Set master preferences.
  chromeMasterPrefs = {
      "extensions.ui.developer_mode": True
  }
  options.add_experimental_option('prefs', chromeMasterPrefs)

  driver = webdriver.Chrome(
      service=service_object,
      options=options,
      service_args=["--verbose"])  #, "--log-path=/tmp/chrome-serv.log"])

  driver.get('file:///Users/joetoth/projects/sideler/data/test.html')
  time.sleep(6000)

  driver.quit()