Skip to content

Instantly share code, notes, and snippets.

@Hwiet
Last active May 24, 2022 10:12
Show Gist options
  • Select an option

  • Save Hwiet/bf8d7a78cb5eacf44533a1e5cba2f61d to your computer and use it in GitHub Desktop.

Select an option

Save Hwiet/bf8d7a78cb5eacf44533a1e5cba2f61d to your computer and use it in GitHub Desktop.
Connect Selenium to existing browser instance (Firefox)

How to connect Selenium to an existing browser instance, Firefox edition. See the example Python script.

(Disclaimer: The browser has to be reopened using the -marionette flag.)

This could be helpful for, for example, logging in to a web application yourself before running your script, so that your script doesn't have to deal with logging in (also helps with avoiding repeated logins and being subject to associated security measures).

Requirements

Instructions

  1. Create a new Firefox instance with the -marionette flag,1 for example on Windows:
C:\Program` Files\Mozilla` Firefox\firefox.exe -marionette
  1. Selenium uses geckodriver to connect to Marionette; in your script, pass the options --connect-existing --marionette-port 2828 to geckodriver. (2828 is the default port for Marionette, you can change this in Firefox configs by typing in about:config in your browser, searching for the marionette.port preference and then changing the default value.)
  2. Your script is now primed to access the browser instance created in step 1. Write the rest of your script.
  3. Run the script—if you have set the preference marionette.debugging.clicktostart to true, the browser will prompt Click to start execution of marionette tests.




Footnotes

Footnotes

  1. https://firefox-source-docs.mozilla.org/testing/geckodriver/Flags.html#code-connect-existing-code

# Selenium script to screenshot pages linked from chosen webpage
# for converting any string to slug-case-strings to use as filenames
from slugify import slugify
# Selenium modules
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
# Connect to a Firefox instance executed with the -marionette flag
service = Service(executable_path='geckodriver', service_args='--marionette-port 2828 --connect-existing'.split())
driver = WebDriver(service=service)
# Page with links to other pages you want to screenshot
driver.get('http://example.com/')
# Get links. This selects all links (!!) in the page specified above.
# Change this CSS selector to be more specific, or replace with other strategy
# See https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html
links = [e.get_attribute('href') for e in driver.find_elements(By.CSS_SELECTOR, 'a')]
for link in links:
driver.get(link)
ActionChains(driver).pause(5).perform()
# Couldn't driver.get get_full_page_screenshot_as_file to work for some reason on Windows,
# even with absolute paths, so I used the typical Python way to save a file.
with open(f'{slugify(driver.title)}.png', 'wb+') as fp:
# PNG data passed as bytes, which is saved to a file
# (Since I'm writing bytes instead of strings, I passed the 'b' flag in the `open()` call the line before)
fp.write(driver.get_full_page_screenshot_as_png())
ActionChains(driver).pause(5).perform()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment