Clicky

Selenium browser automation

Updated 1 month ago by Yvonne

Browser automation allows you to automate tasks inside Multilogin browser profiles. From creating simple automation scripts to complex web crawlers that search, collect and interact with web data.

Multilogin browser automation is based on the Selenium WebDriver.

In the usual scenario, if you are running Selenium code, you would first connect to Firefox (Gecko) or Chrome driver first and then set desired capabilities. When using Multilogin with your Selenium code, you do not need to do that. Instead, you use the Remote Web Driver to connect to the Multilogin application or browser profile via the local port and set desired capabilities to execute Selenium commands in a pre-defined browser profile.

Supported languages

Because Selenium framework offers a variety of language bindings, Multilogin automation can be also run on multiple coding languages.  However, currently, technical support is only provided for Java and Python language.

Selenium on Multilogin 3.X

Multilogin port allocation

On 3.x versions of Multilogin, you need to predefine the application port in order to utilize Selenium automation. Here is how you can predefine Multilogin port in 3.X:

  1. Please go to C:\Users\%username%\.multiloginapp.com directory and open app.properies file
  2. Add the following string: multiloginapp.port=[PORT_NUMBER]
  3. Save the app.properties file

Thereafter, you will be able to refer to the Multiogin application through this port.

Java example:
import org.junit.Assert;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BrowserProfile {
public static void main(String[] args) throws Exception {
BrowserProfile bp = new BrowserProfile();
//TODO replace with existing profile ID. Define the ID of the browser profile, where the code will be executed.
String profileId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

//Define DesiredCapabilities
DesiredCapabilities dc = new DesiredCapabilities();

//Instantiate the Remote Web Driver to connect to the browser profile launched by startProfile method
RemoteWebDriver driver = new RemoteWebDriver(new URL(bp.startProfile(profileId)), dc);

//Perform automation
driver.get("https://multilogin.com/");
Assert.assertEquals("Multilogin - Replace Multiple Computers With Virtual Browser Profiles - Multilogin",driver.getTitle());
driver.quit();
}

private String startProfile(String profileId) throws Exception {
/*Send GET request to start the browser profile by profileId. Returns response in the following format:
'{"status":"OK","value":"http://127.0.0.1:XXXXX"}', where XXXXX is the localhost port on which browser profile is
launched. Please make sure that you have Multilogin listening port set to 35000. Otherwise please change the port
value in the url string*/
String url = "http://127.0.0.1:35000/api/v1/profile/start?automation=true&profileId=" + profileId;

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//Get JSON text from the response and return the value by key "value"
JSONObject jsonResponse = new JSONObject(response.toString());
return jsonResponse.getString("value");
}
}
Python example: 
from selenium import webdriver
from selenium.webdriver.firefox import options
import requests

#TODO replace with existing profile ID. Define the ID of the browser profile, where the code will be executed.
mla_profile_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

mla_url = 'http://127.0.0.1:35000/api/v1/profile/start?automation=true&profileId='+mla_profile_id

"""
Send GET request to start the browser profile by profileId. Returns response in the following format: '{"status":"OK","value":"http://127.0.0.1:XXXXX"}', where XXXXX is the localhost port on which browser profile is launched. Please make sure that you have Multilogin listening port set to 35000. Otherwise please change the port value in the url string
"""
resp = requests.get(mla_url)

json = resp.json()

#Define DesiredCapabilities
opts = options.DesiredCapabilities()

#Instantiate the Remote Web Driver to connect to the browser profile launched by previous GET request
driver = webdriver.Remote(command_executor=json['value'], desired_capabilities={})

#Perform automation
driver.get('https://multilogin.com/')
assert "Multilogin - Replace Multiple Computers With Virtual Browser Profiles - Multilogin" in driver.title
driver.quit()

Selenium on Multilogin 2.X

Java example:
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import org.apache.http.client.utils.URIBuilder;
import java.net.URI;

public class BrowserAutomationRemoteWebDriverSample {
public void simpleTest() throws Exception {
//TODO replace with existing profile ID. Define the ID of the browser profile, where the code will be executed.
String profileId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

//Define Multilogin local port
int mlaClientPort = 35000;

//Define the URL where the Remote Web Driver should connect to Multilogin
URI url = new URIBuilder("http://127.0.0.1:" + mlaClientPort + "/api/v1/webdriver").build();

//Set desired capabilities to connect to a pre-defined browser profile
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("multiloginapp-profileId", profileId);

//Instantiate the Remote Web Driver to connect to a Multiloginapp local connection
RemoteWebDriver driver = new RemoteWebDriver(url.toURL(), dc);

//Perform automation
driver.get("https://multilogin.com/");
Assert.assertEquals("Multilogin - Replace Multiple Computers With Virtual Browser Profiles - Multilogin",driver.getTitle());
driver.quit();
}
}
Python example:
from selenium import webdriver

#TODO replace with existing profile ID. Define the ID of the browser profile, where the code will be executed.
mla_profile_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

#Define Multilogin local port
mla_port = '35001'

#Define the URL where the Remote Web Driver should connect to
mla_url = 'http://127.0.0.1:' + mla_port + '/api/v1/webdriver'

#Set desired capabilities to connect to a pre-defined browser profile and instantiate the Remote Web Driver to connect to a Multilogin local connection
driver = webdriver.Remote(command_executor=mla_url, desired_capabilities={'multiloginapp-profileId': mla_profile_id})

#Perform automation
driver.get('https://multilogin.com/')
assert "Multilogin - Replace Multiple Computers With Virtual Browser Profiles - Multilogin" in driver.title
driver.quit()


Was this article helpful?