Selenium浏览器自动化执行任务,以及在VMLogin 中文版反指纹浏览器中使用Selenium自动执行任务说明

浏览器自动化允许您在VMLogin 中文版的浏览器配置文件中自动执行任务。从创建简单的自动化脚本到复杂的Web爬虫,可以搜索、收集Web数据并与之交互。

VMLogin 中文版反指纹浏览器自动化基于Selenium WebDriver。

通常情况下,如果您运行Selenium代码,首先将连接到Chrome驱动,然后设置您所需要的功能。而将VMLogin 中文版与Selenium代码结合使用时,您无需这样操作。您将使用Remote Web Driver程序,通过本地端口连接到VMLogin 中文版应用或某浏览器配置文件,设置所需功能,在预定义的浏览器配置文件中执行Selenium命令。

支持的语言
Selenium框架提供了多种可搭配使用的语言,因此VMLogin自动化也可以在多种编码语言上运行。但是目前,我们仅为Java和Python供技术支持。

在 VMLogin 中文版中使用Selenium
定义VMLogin端口
您需要提前定义软件端口以使用Selenium自动化。以下是定义端口的方法:

在软件《我的帐户》中打开启用浏览器自动化设置,并在监听端口中设置能使用端口,这里默认是35000,另外你也可以设置一个访问密码。

随后,您就可以通过定义的端口连接到VMLogin了。

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://vmlogin.com/");
    Assert.assertEquals("VMLOGIN - 虚拟多登浏览器",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 Vmlogin 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");
}

}

接口还可以传入代理服务器信息,如果传入代理信息会覆盖配置文件里的代理信息,这种覆盖是临时性的,不会真的修改配置文件,只对自动化接口有效:

http://127.0.0.1:35000/api/v1/profile/start?automation=true&profileId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&proxytype=socks5&proxyserver=ip&proxyport=1080&proxyusername=&proxypassword=
代理类型可能是这三种:
proxytype=socks5
proxytype=socks4
proxytype=http
代理用户名和密码可以不传为空。

Python 案例:

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 VMlogin 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://vmlogin.com/')
assert "VMLOGIN - 虚拟多登浏览器" in driver.title
driver.quit()

请关注VMLogin 中文版反指纹浏览器的官网,了解最新动态:https://cn.vmlogin.com/

你可能感兴趣的:(VMLogin.cc,指纹浏览器,selenium,python,java,web,docker)