🌐Script with Standalone Pylenium

How to use Pylenium in a regular script instead of in a test

Setup

Pylenium needs two things in order to be instantiated:

  • PyleniumConfig

  • Pylenium

Create a main.py file and add the necessary imports:

main.py
from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

Create an instance of PyleniumConfig

Start by creating an instance of PyleniumConfig. Leaving it blank will create a config with default values. NOTE: This does not use pylenium.json

Default config
from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

config = PyleniumConfig()

To use pylenium.json, you'd have to read the file first:

Use pylenium.json
import json
from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

with open("pylenium.json") as file:
    pylenium_json = json.load(file)

config = PyleniumConfig(**pylenium_json)

You can set config values directly in the script - mixing and matching as needed

import json
from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

with open("pylenium.json") as file:
    pylenium_json = json.load(file)

config = PyleniumConfig(**pylenium_json)
config.browser = "firefox"

Create an instance of Pylenium

Once the config is ready, instantiate Pylenium with it:

from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

config = PyleniumConfig()
py = Pylenium(config)

Write your Script

You now have access to Pylenium's many commands to script what you need:

Google Search
from pylenium.driver import Pylenium
from pylenium.config import PyleniumConfig

config = PyleniumConfig()
py = Pylenium(config)

py.visit("https://google.com")
py.get("[name='q']").type("pylenium.io\n")
py.should().contain_title("pylenium.io")
py.quit()

Run your Script

Use python to execute main.py

python main.py

Last updated