Search code examples
pythonseleniumplaywright

Get tab name in Chrome with Playwright


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/'>>

Solution

  • 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