Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Tab's attributes? #69

Open
gnyuan opened this issue Jul 30, 2024 · 2 comments
Open

How to get Tab's attributes? #69

gnyuan opened this issue Jul 30, 2024 · 2 comments

Comments

@gnyuan
Copy link

gnyuan commented Jul 30, 2024

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
Copy link

dstampher commented Aug 1, 2024

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
Copy link
Author

gnyuan commented Aug 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants