fate0 / pychrome

A Python Package for the Google Chrome Dev Protocol [threading base]
Other
610 stars 111 forks source link

How to get Tab's attributes? #69

Open gnyuan opened 2 months ago

gnyuan commented 2 months ago
tabs = browser.list_tab()
for tab in tabs:
    print(f"Tab atrrs: {tab.id} {tab.type}  {tab.status}  {tab.debug}")

I've just discovered that these attributes are functional. However, how can I retrieve other tab attributes such as the title and URL?

dstampher commented 2 months ago

I had the same question. It seems one way to do it is to send a request to the browser that is ran with --remote-debugging-port=9222.

Send a GET request to localhost:9222/json after the browser is launched with the flag, and you will get a response that includes the titles/urls of the tabs.

I think this ability should be included in this library. I want to execute some javascript in an opened tab based on a regex match of the tabs title. And so I need to know the titles of the tabs to accomplish this use case.

I guess for now I will need to use the CDP REST API manually for my usage at the moment.

gnyuan commented 2 months ago

I had the same question. It seems one way to do it is to send a request to the browser that is ran with --remote-debugging-port=9222.

Send a GET request to localhost:9222/json after the browser is launched with the flag, and you will get a response that includes the titles/urls of the tabs.

I think this ability should be included in this library. I want to execute some javascript in an opened tab based on a regex match of the tabs title. And so I need to know the titles of the tabs to accomplish this use case.

I guess for now I will need to use the CDP REST API manually for my usage at the moment.

I concur with your perspective. My current approach involves establishing connections to each tab and retrieving attributes through network requests. However, my objective is to interact exclusively with the tab that has the URL www.google.com. The current method necessitates the opening of every tab, which is proving to be quite inefficient and time-consuming.

I am seeking a more streamlined solution that would allow me to interact with the specific tab without the overhead of opening all tabs. Is there a way to refine this process to target and manipulate the desired tab directly?

My code like this

for tab in tabs:
    tab.start()
    tab.Network.enable()
    tab.DOM.enable()
    document = dom.getDocument()
    root_node_id = document.get('root', {}).get('nodeId') # get everything about tab