{"id":524,"date":"2023-02-07T09:51:24","date_gmt":"2023-02-07T01:51:24","guid":{"rendered":"https:\/\/www.xlogin.us\/help\/docs\/document\/api-automation\/java-uses-code-for-using-xlogin-automation-api-interface-to-launch-browser\/"},"modified":"2024-12-04T15:26:13","modified_gmt":"2024-12-04T07:26:13","slug":"java-code-to-launch-browser","status":"publish","type":"docs","link":"https:\/\/www.xlogin.us\/help\/docs\/api-automation\/java-code-to-launch-browser\/","title":{"rendered":"JAVA Uses Code for Using XLogin Automation API Interface to Launch Browser"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package com.ruoyi.threed;\r\n\r\n\r\nimport com.alibaba.fastjson2.JSONObject;\r\nimport org.openqa.selenium.remote.DesiredCapabilities;\r\nimport org.openqa.selenium.remote.RemoteWebDriver;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.openqa.selenium.chrome.ChromeOptions;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.InputStreamReader;\r\nimport java.net.HttpURLConnection;\r\nimport java.net.URL;\r\nimport java.util.Map;\r\n\r\npublic class BrowserProfile {\r\n    public static void main(String&#91;] args) throws Exception {\r\n        BrowserProfile bp = new BrowserProfile();\r\n\r\n        String profileId = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\";\r\n\r\n        String ss = bp.startProfile(profileId);\r\n        System.out.println(\"ss:\"+ss);\r\n\r\n\r\n        Map&lt;String,Object> _map =  JSONObject.parseObject( ss.toString(),Map.class);\r\n        String vaa = _map.get(\"value\").toString();\r\n        System.out.println(\"value()\"+vaa.toString());\r\n        URL url = new URL(vaa);\r\n        String  dizip =url.getAuthority().replace(\"http:\/\/\",\"\");\r\n        System.out.println(\"dizip:\"+dizip);\r\n\r\n        String drvstr = _map.get(\"chromedriver\").toString();\r\n\r\n        System.setProperty(\"webdriver.chrome.driver\",drvstr);\r\n       \r\n        ChromeOptions options = new ChromeOptions();\r\n        options.setExperimentalOption(\"debuggerAddress\",dizip);\r\n        WebDriver driver = new ChromeDriver(options);\r\n  \r\n\r\n        \/\/visit xlogin\r\n        driver.navigate().to(\"http:\/\/www.xlogin.us\");\r\n        System.out.println(driver.getTitle());\r\n        driver.quit();\r\n    }\r\n\r\n    private String startProfile(String profileId) throws Exception {\r\n\r\n  \r\n        String url = \"http:\/\/127.0.0.1:35000\/api\/v1\/profile\/start?skiplock=true&amp;profileId=\" + profileId;\r\n     \r\n        URL obj = new URL(url);\r\n        HttpURLConnection con = (HttpURLConnection) obj.openConnection();\r\n\r\n        con.setRequestMethod(\"GET\");\r\n\r\n        BufferedReader in = new BufferedReader(\r\n                new InputStreamReader(con.getInputStream()));\r\n        String inputLine;\r\n        StringBuffer response = new StringBuffer();\r\n\r\n        while ((inputLine = in.readLine()) != null) {\r\n            response.append(inputLine);\r\n        }\r\n        in.close();\r\n        System.out.println(\"response.toString()\"+response.toString());\r\n    \r\n        Thread.sleep(3000);\r\n        return response.toString();\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><br><br><\/p>\n","protected":false},"featured_media":0,"parent":810,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-524","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":4,"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/524\/revisions"}],"predecessor-version":[{"id":2275,"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/524\/revisions\/2275"}],"up":[{"embeddable":true,"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/810"}],"next":[{"title":"How to Integrate XLogin with Puppeteer?","link":"https:\/\/www.xlogin.us\/help\/docs\/api-automation\/xlogin-with-puppeteer\/","href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/527"}],"prev":[{"title":"Selenium Browser Automation","link":"https:\/\/www.xlogin.us\/help\/docs\/api-automation\/selenium-browser-automation\/","href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/docs\/513"}],"wp:attachment":[{"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.xlogin.us\/help\/wp-json\/wp\/v2\/doc_tag?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}