HyperionGray / trio-chrome-devtools-protocol

Trio driver for Chrome DevTools Protocol (CDP)
MIT License
60 stars 17 forks source link

How to launch trio-chrome fully programmatically ? #13

Open benoitgaly opened 4 years ago

benoitgaly commented 4 years ago

Hi,

In the examples it says : "The URL that Chrome is listening on is displayed in the terminal after Chrome starts up." This works fine, but how to do handle all this programmatically ? Copy-pasting an url is not possible in production.

Have you a way to get this url automatically ? Isn't there a way to bypass this ? I mean, other library like Pychrome (outdated), were simply asking for the url 'pychrome.Browser(url="http://127.0.0.1:9222")'

In python, Selenium is allowing : driver.execute_cdp_cmd('Target.getTargets', {}). Those libraries are not as complete as trio-chrome, but I do not need to copy paste an url by hand.

benoitgaly commented 4 years ago

Best I have found so far with Selenium is : chrome_options.add_argument(f'--remote-debugging-port={self.dev_tools_port}')

chrome_options.add_argument("--user-data-dir=C:\Applications\profile")

    chrome = webdriver.Chrome(executable_path=self.driver_exe, chrome_options=chrome_options, service_log_path=os.devnull)
    chrome.get(f'http://127.0.0.1:{self.dev_tools_port}/json/version')
    pre = chrome.find_element_by_tag_name("pre").text
    data = json.loads(pre)
    self.webSocketDebuggerUrl = data['webSocketDebuggerUrl']
mehaase commented 4 years ago

In our own use case, we create the browser in a separate component and pass the URL into Trio CDP, but as you point out many libraries have this feature and it would be good to add it here. I will leave this ticket open as a reminder.

In the meantime, here is some sample code for getting the chrome URL programmatically:

import asks

port = 9000
url = "http://localhost:{}/json/version".format(port)
response = await asks.get(url)
data = response.json()
debugger_url = data["webSocketDebuggerUrl"]

# Now `debugger_url` can be passed into Trio CDP