Usage sample
- Start MoreLogin and log in successfully, then maintain the logged-in state while using the API.
- Go to Browser profile > API to check the successful status of the API interface.
- Obtain the API address; port number; and API Key in the API page.
- 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)
- 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.
# 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)
- 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.
- 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)