Usage sample

Updated by MoreLogin

  1. Start MoreLogin and log in successfully, then maintain the logged-in state while using the API.
  2. Go to Browser profile > API to check the successful status of the API interface.
  1. Obtain the API address; port number; and API Key in the API page.
  1. Use "Get browser profile list" endpoint to obtain a list of browser profiles, or you can copy the browser profile ID directly. Go to "Browser profile" section, click the "More" button in the action column, and select the "Copy ID" option from the dropdown menu.
# python3
# pip install requests
import requests
import pprint

headers = {
'x-api-key': 'c7d3b3d1-af9d-4359-8adf-632c2715704a',
}
params = {
"page": 1,
"page_size": 10,
}
res = requests.get("http://local.morelogin.com:25520/profile/list", params=params, headers=headers)
data = res.json()
pprint.pprint(data)

  1. Assuming you have obtained the browser profile ID as "26841222374912" in the fourth step, you can start browser profile API to initiate the browser profile.

For python example, please refer to the script below:

# python3
# pip install requests
import requests
import pprint

headers = {
'x-api-key': 'c7d3b3d1-af9d-4359-8adf-632c2715704a',
}
payload = {
"id": 26868295929665,
}
res = requests.post("http://local.morelogin.com:25520/profile/start/browser", json=payload, headers=headers)
data = res.json()
pprint.pprint(data)

For JavaScript example, please refer to the script below:

import axios from "axios";
import puppeteer from "puppeteer-core";

const API_KEY = "***58f-b74d-4fb3-943e-e4f11e739da7";
const API_URL = "http://local.morelogin.com:25520";

async function start(api_url: string, api_key: string, profile_id: number) {
console.log(`Start profile :>> api_url=${api_url}, profile_id=${profile_id}`);
const resp = await axios.post(
api_url + "/profile/start/browser",
{
id: profile_id,
},
{
headers: {
"x-api-key": api_key,
},
}
);
console.log("resp :>> ", resp.data);
let data = resp.data;
if (data.ret != 0) {
console.log("Start fail :>> ", data);
return;
}
const body = data.data;

const version = await axios.get(`${body.webdriver_url}/json/version`);
const driver = await puppeteer.connect({
browserWSEndpoint: version.data.webSocketDebuggerUrl,
});
// Open tab
const page = await driver.newPage();
await page.goto("https://morelogin.com");

console.log(">> Start success");
}

// Parameters
const profile_id = 27048731904685;
// Run
start(API_URL, API_KEY, profile_id);

  1. Assuming that you have successfully started the browser profile in the fifth step, you can now retrieve the webdriver port number associated with that browser profile. This will allow you to perform automated browser actions using tools like Selenium or Puppeteer.
{
"ret": 0,
"data": {
"webdriver_url": "http://localhost:46300",
"port": 46300
}
}

If you need to use the WebDriver mode, please make sure to enable the "Debug Port" switch in the "Settings" - "Local Settings" menu.

  1. Here is an example of running a webdriver (the remote port number should be based on the actual obtained port number):
from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
import time


def open_profile(api_key, profile_id):
headers = {
"x-api-key": api_key,
}
payload = {
"id": profile_id,
}
res = requests.post(
"http://local.morelogin.com:25520/profile/start/browser",
json=payload,
headers=headers,
)
print(res.json())
data = res.json()["data"]
return data["core_type"], data["port"], data["version"]


def stop_profile(api_key, profile_id):
headers = {
"x-api-key": api_key,
}
payload = {
"id": profile_id,
}
res = requests.post(
"http://local.morelogin.com:25520/profile/stop/browser",
json=payload,
headers=headers,
)
data = res.json()
print(data)


def get_driver(core_type, port, version):
if core_type == 0:
opts = webdriver.ChromeOptions()
opts.set_capability("browserVersion", str(version))
opts.add_experimental_option("debuggerAddress", f"127.0.0.1:{port}")
driver = webdriver.Chrome(options=opts)
else:
raise Exception()
return driver


def test_webdriver(driver):
# time.sleep(5)
driver.switch_to.new_window("tab")
driver.get("https://www.selenium.dev/selenium/web/web-form.html")

title = driver.title
assert title == "Web form"

driver.implicitly_wait(0.5)

text_box = driver.find_element(by=By.NAME, value="my-text")
submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")

text_box.send_keys("Selenium")
submit_button.click()

message = driver.find_element(by=By.ID, value="message")
value = message.text
assert value == "Received!"

driver.quit()


if __name__ == "__main__":
api_key = "c7d3b3d1-af9d-4359-8adf-632c2715704a"
# profile_id = 26868295929665
profile_id = 26871691303507
core_type, port, version = open_profile(api_key, profile_id)
print(core_type, port, version)
time.sleep(10)
driver = get_driver(core_type, port, version)
test_webdriver(driver)
stop_profile(api_key, profile_id)


How did we do?