JAVA 使用VMLogin自动化API接口启动浏览器代码

admin4年前浏览器自动化1739
package com.ruoyi.common.spider.reptile;

import cn.hutool.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author vmlogin
 *         <dependency>
 *             <groupId>org.seleniumhq.selenium</groupId>
 *             <artifactId>selenium-java</artifactId>
 *             <version>3.141.59</version>
 *         </dependency>
 */
public class ProductChrome {

    public static void main(String[] args) throws Exception {

        ProductChrome pc = new ProductChrome();
        String profileId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
        //根据profileId打开并获取远程调试地址
        URL url = new URL(pc.startProfile(profileId));

        //使用远程调试地址连接到打开的chrome浏览器
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("debuggerAddress", url.getAuthority());
        WebDriver driver = new ChromeDriver(chromeOptions);

        //访问vmlogin
        driver.get("https://www.vmlogin.com/");
        System.out.println(driver.getTitle());
        driver.quit();
    }


    private String startProfile(String profileId) throws Exception {

        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();
        JSONObject jsonResponse = new JSONObject(response.toString());
        return jsonResponse.getStr("value");
    }
}


标签: JAVAapiselenium

相关文章

VMLogin 本地 API 接口进行常规自动化操作

1. 刷新页面  http://127.0.0.1:35000/api/v1/profile/refresh?profileId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxx...

Selenium浏览器自动化

Selenium浏览器自动化

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

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。