Clicky

Selenium automation example

Updated 2 days ago by Yelena

Related articles

This simple automation script in Python uses the Selenium library to manipulate a profile in Multilogin X.

How it works

To manipulate the driver, the script performs the actions below.

  1. Sign in using Multilogin X API
  2. Start the profile defining Selenium as the selected automation type
  3. Retrieve the port used by the running profile
  4. Start a Selenium driver on localhost using the retrieved port
  5. Use the driver to manipulate browser actions
  6. Stop the profile in 5 seconds

Running the script

Complete the steps below to be able to run the provided script example.

Make sure the agent is connected, as it makes profile launches possible.
  1. Install the following Python libraries:
    1. requests
    2. selenium
  2. Insert your values into the below variables in the script:
    1. USERNAME: your Multilogin X account email
    2. PASSWORD: your Multilogin X account password (MD5 encryption is not required)
    3. FOLDER_ID, PROFILE_ID: find these values using our guides on DevTools or Postman
import requests
import hashlib
import time

from selenium import webdriver
from selenium.webdriver.chromium.options import ChromiumOptions

MLX_BASE = "https://api.multilogin.com"
MLX_LAUNCHER = "https://launcher.mlx.yt:45001/api/v1"
LOCALHOST = "http://127.0.0.1"
HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

#TODO: Insert your account information in both variables below.
USERNAME = ""
PASSWORD = ""

#TODO: Insert the Folder ID and the Profile ID below
FOLDER_ID = ""
PROFILE_ID = ""

def signin() -> str:

payload = {
'email': USERNAME,
'password': hashlib.md5(PASSWORD.encode()).hexdigest()
}

r = requests.post(f'{MLX_BASE}/user/signin', json=payload)

if(r.status_code != 200):
print(f'\nError during login: {r.text}\n')

response = r.json()['data']

token = response['token']

return token

def start_profile() -> webdriver:

r = requests.get(f'{MLX_LAUNCHER}/profile/f/{FOLDER_ID}/p/{PROFILE_ID}/start?automation_type=selenium', headers=HEADERS)

response = r.json()

if(r.status_code != 200):
print(f'\nError while starting profile: {r.text}\n')
else:
print(f'\nProfile {PROFILE_ID} started.\n')

selenium_port = response.get('status').get('message')
driver = webdriver.Remote(command_executor=f'{LOCALHOST}:{selenium_port}', options=ChromiumOptions())

return driver

def stop_profile() -> None:
r = requests.get(f'{MLX_LAUNCHER}/profile/stop/p/{PROFILE_ID}', headers=HEADERS)

if(r.status_code != 200):
print(f'\nError while stopping profile: {r.text}\n')
else:
print(f'\nProfile {PROFILE_ID} stopped.\n')


token = signin()
HEADERS.update({"Authorization": f'Bearer {token}'})

driver = start_profile()
driver.get('https://multilogin.com/')
time.sleep(5)
stop_profile()



Check out our latest news, research, and tutorials


Has your issue been resolved?