4

I print out the title, then put it into the function "is_visible()" It always returns False, but it should be True since I use the title the computer returns to me.
Any solution?

from playwright.sync_api import sync_playwright
from playwright.sync_api import expect, Page


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=1000)
    context = browser.new_context()
    page = context.new_page()
    page.goto('https://www.duckduckgo.com')
    page.locator('#search_form_input_homepage').fill('panda')
    page.locator('#search_button_homepage').click()
    expect(page).to_have_title('panda at DuckDuckGo')
    print(page.title())
    print(page.get_by_title('panda at DuckDuckGo').is_visible())
ggorlen
  • 44,755
  • 7
  • 76
  • 106
akuan10
  • 111
  • 6

1 Answers1

0

You may be confusing two things:

  1. The page title, which is the thing you see written in the browser tab. For this page, running document.title in the browser console gives 'python - Playwright, method "is_visible()" always returns False - Stack Overflow'. You can assert that the page title has a certain value with to_have_title(), as you're correctly doing.
  2. An element's title attribute. This is not the page title, rather, it's <span title="Issues count">25 issues</span>. You can retrieve this element with get_by_title().

With this in mind,

print(page.get_by_title('panda at DuckDuckGo').is_visible())

doesn't make sense. It's confusing the page title with the title= attribute of an element. No element in the DOM has a title="panda at DuckDuckGo" so the locator times out.

I suppose you could retrieve the <title>panda at DuckDuckGo</title> element with a page.locator("title") and assert its contents, but I don't see the point to doing that since to_have_title already achieves this more succinctly.

In short, remove the get_by_title line. There's no need to check the visibility of a page title--you've already asserted all there is to assert about the page title with to_have_title().

ggorlen
  • 44,755
  • 7
  • 76
  • 106