Selenium
How to use in Selenium
- Download the corresponding Webdriver from Stable Version Downloads or All Version Downloads
- Launch nstbrowser agent client, the client will listen on port:
8848
- Get the remote debugging address of the browser:
debuggerAddress
- Connect to the browser through selenium
Common Code
import requests
from requests.exceptions import HTTPError
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
# get_debugger_port: Get the debugger port
def get_debugger_port(url: str):
try:
resp = requests.get(url).json()
if resp['data'] is None:
raise Exception(resp['msg'])
port = resp['data']['port']
return port
except HTTPError:
raise Exception(HTTPError.response)
def exec_selenium(debugger_address: str):
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", debugger_address)
# Replace with the corresponding version of WebDriver path.
chrome_driver_path = r'your webdriver path'
service = ChromeService(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://nstbrowser.io")
driver.save_screenshot('screenshot.png')
driver.close()
driver.quit()
1. LaunchExistBrowser
::: tip URLhttp://localhost:8848/devtool/launch/{profileId}?x-api-key={x-api-key}&config={config}
- Connect to or start an existing browser,you need to create the corresponding profile in advance
- Support custom config
:::
import json
from urllib.parse import quote
from urllib.parse import urlencode
# launch_and_connect_to_browser: Launch the specified browser and connect
# You need to create the corresponding profile in advance
# Support custom config
def launch_and_connect_to_browser(profile_id: str):
host = '127.0.0.1'
api_key = 'your apiKey'
config = {
'headless': False, # support: true or false
'autoClose': True,
}
query = urlencode({
'x-api-key': api_key, # required
'config': quote(json.dumps(config))
})
url = f'http://{host}:8848/devtool/launch/{profile_id}?{query}'
print('devtool url: ' + url)
port = get_debugger_port(url)
debugger_address = f'{host}:{port}'
print("debugger_address: " + debugger_address)
exec_selenium(debugger_address)
launch_and_connect_to_browser('your profileId')
2. CreateAndConnectToBrowser
- Create a new Profile based on config and launch the browser
- Support custom config
import json
from urllib.parse import quote
from urllib.parse import urlencode
# create_and_connect_to_browser: launch new browser and connect
# Create a new Profile based on config and launch the browser
# Support custom config
def create_and_connect_to_browser():
host = '127.0.0.1'
api_key = 'your apiKey'
config = {
'name': 'custom browser',
'platform': 'windows', # support: windows, macos, linux
'kernel': 'chromium', # only support: chromium
'kernelMilestone': '124', # support: 113, 120, 124
'once': True,
'headless': True, # support: true or false
'autoClose': True,
'remoteDebuggingPort': 9222,
'proxy': '', # input format: schema://user:password@host:port eg: http://user:password@localhost:8080
'fingerprint': {
'hardwareConcurrency': 4, # support: 2, 4, 8, 10, 12, 14, 16
'deviceMemory': 4, # support: 2, 4, 8
}
}
query = urlencode({
'x-api-key': api_key, # required
'config': quote(json.dumps(config))
})
url = f'http://{host}:8848/devtool/launch?{query}'
print('devtool url: ' + url)
port = get_debugger_port(url)
debugger_address = f'{host}:{port}'
print("debugger_address: " + debugger_address)
exec_selenium(debugger_address)
create_and_connect_to_browser()
3. Config parameters notes
Parameter | Type | Description | |
---|---|---|---|
config.name | String | Browser name | |
config.platform | String | Operating system type (supported: windows, macos, linux) | |
config.kernel | String | Kernel type (only supported: chromium) | |
config.kernelMilestone | String | Kernel version (supported: 113, 120, 124) | |
config.once | Boolean | Disposable browser that does not create a Profile and automatically closes after execution; (only supported by "CreateAndConnectToBrowser") | |
config.headless | Boolean | true: enable headless; "new": enable new headless mode; difference reference https://developer.chrome.com/docs/chromium/new-headless. and automatically close after execution | |
config.autoClose | Boolean | Whether to automatically close the connection after the browser is closed | |
config.timedCloseSec | Number | Timed close (s) | |
config.remoteDebuggingPort | Number | Remote debugging port | |
config.proxy | String | Proxy server address and port number (format: schema://user:password@host:port, e.g. http://user/:password@localhost:8080/) | |
config.args | Object | Browser args, invalid to LaunchExistBrowser API |
|
config.fingerprint | Object | Browser fingerprint configuration; invalid to LaunchExistBrowser API |
|
hardwareConcurrency | Number | CPU core count (supported: 2, 4, 8, 10, 12, 14, 16) | |
deviceMemory | Number | Device memory size (supported: 2, 4, 8) | |
userAgent | String | UserAgent |
the full config
refer to LaunchNewBrowser.
Modified at 20 days ago