Get tab name in Chrome with Playwright
Question:
I am trying to fetch the tab name with playwright, but my code only outputs the page url.
For instance, I want to go to https://www.rottentomatoes.com/ and print the name being shown on the tab:
Rotten Tomatoes: Movies/ Tv Shows/ Movie Trailers…
I tried to use page.title and a few other options, but it is not working.
from playwright.sync_api import sync_playwright
website = "https://www.rottentomatoes.com/"
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(website)
print(page.content)
print(page.inner_text)
print(page.title)
print(page.context)
My output is:
<bound method Page.content of <Page url='https://www.rottentomatoes.com/'>>
<bound method Page.inner_text of <Page url='https://www.rottentomatoes.com/'>>
<bound method Page.title of <Page url='https://www.rottentomatoes.com/'>>
Answers:
As mentioned in the comments by other user. You need to pass selector as an arugument in the method inner_text(Selector)
to get the value.
from playwright.sync_api import sync_playwright
website = "https://www.rottentomatoes.com/"
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(website)
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='movies'] a[slot='link']"))
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='tv'] a[slot='link']"))
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='trivia'] a[slot='link']"))
This will print in the console.
MOVIES
TV SHOWS
MOVIE TRIVIA
As stated in the comment by G. Anderson, right now you’re just referencing the method but not calling it, so page.title
should be page.title()
as shown in the page title docs. Which is why the output was just saying “bound method…”
Hope that helps!
I am trying to fetch the tab name with playwright, but my code only outputs the page url.
For instance, I want to go to https://www.rottentomatoes.com/ and print the name being shown on the tab:
Rotten Tomatoes: Movies/ Tv Shows/ Movie Trailers…
I tried to use page.title and a few other options, but it is not working.
from playwright.sync_api import sync_playwright
website = "https://www.rottentomatoes.com/"
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(website)
print(page.content)
print(page.inner_text)
print(page.title)
print(page.context)
My output is:
<bound method Page.content of <Page url='https://www.rottentomatoes.com/'>>
<bound method Page.inner_text of <Page url='https://www.rottentomatoes.com/'>>
<bound method Page.title of <Page url='https://www.rottentomatoes.com/'>>
As mentioned in the comments by other user. You need to pass selector as an arugument in the method inner_text(Selector)
to get the value.
from playwright.sync_api import sync_playwright
website = "https://www.rottentomatoes.com/"
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(website)
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='movies'] a[slot='link']"))
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='tv'] a[slot='link']"))
print(page.inner_text("rt-header-nav[slot='nav-dropdowns'] [slot='trivia'] a[slot='link']"))
This will print in the console.
MOVIES
TV SHOWS
MOVIE TRIVIA
As stated in the comment by G. Anderson, right now you’re just referencing the method but not calling it, so page.title
should be page.title()
as shown in the page title docs. Which is why the output was just saying “bound method…”
Hope that helps!