Tự động hóa trình duyệt Selenium
Các bài liên quan
Tự động hóa trình duyệt cho phép bạn tự động hóa các tác vụ bên trong hồ sơ trình duyệt Multilogin, từ tạo tập lệnh tự động hóa đơn giản đến trình thu thập dữ liệu web phức tạp như tìm kiếm, thu thập và tương tác với dữ liệu web.
Tự động hóa trình duyệt Multilogin dựa trên Selenium WebDriver.
Thông thường, nếu bạn đang chạy code Selenium, trước tiên bạn sẽ kết nối với Firefox (Gecko) hoặc Chrome, sau đó đặt các khả năng mong muốn. Khi sử dụng Multilogin với code Selenium của bạn, bạn không cần phải làm điều đó.
Thay vào đó, bạn sử dụng Remote Web Driver để kết nối với ứng dụng Multilogin hoặc hồ sơ trình duyệt qua local port và đặt các khả năng mong muốn để thực thi các lệnh Selenium trong hồ sơ trình duyệt được xác định trước.
Ngôn ngữ được hỗ trợ
Vì Selenium framework cung cấp nhiều ràng buộc ngôn ngữ khác nhau, Multilogin automation cũng có thể chạy trên nhiều ngôn ngữ code. Tuy nhiên, hiện tại, hỗ trợ kỹ thuật chỉ có cho ngôn ngữ Python.
Selenium với Multilogin
Phân bổ cổng Multilogin
Bạn cần xác định trước cổng ứng dụng để sử dụng tự động hóa Selenium.
- Chuyển đến thư mục C:\Users\%username%\.multiloginapp.com và mở tệp app.properties bằng bất kỳ ứng dụng chỉnh sửa text nào
- Thêm chuỗi sau vào tệp:
multiloginapp.port=[PORT_NUMBER]
- Lưu tệp app.properties
Sau đó, bạn sẽ có thể vào ứng dụng Multilogin thông qua cổng này.
Ví dụ Java
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 | Scale Fast With 1000s Of Profiles",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");
}
}
Ví dụ Python
from selenium import webdriver
from selenium.webdriver.chromium.options import ChromiumOptions
from selenium.webdriver.firefox.options 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()
print(json)
#Instantiate the Remote Web Driver to connect to the browser profile launched by previous GET request
# In case of using Mimic browser
driver = webdriver.Remote(command_executor=json['value'], options=ChromiumOptions())
# In case of using Stealthfox browser
#driver = webdriver.Remote(command_executor=json['value'], options=Options())
#Perform automation
driver.get('https://multilogin.com/')
print(driver.title)
driver.quit()