Search code examples
pythonseleniumselenium-webdriverwebdriverwaitpage-title

Selenium is not properly determining if a page has a <title> html tag


I'm trying to get Selenium to wait until the title tag of a web page is present when loading with Python.

I've tried testing this code with other types of HTML tags and only the <body> tag didn't result in an error.

wait = WebDriverWait(driver, 10)
driver.get(link)
wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'div')))

I expected the code to evaluate to completion but I got the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Solution

  • The title tag is never visible. You can wait for its presence, though:

    wait.until(EC.presence_of_element_located((By.TAG_NAME, 'title')))
    

    The title also has its own expected conditions, title_is() and title_contains(). For example:

    wait.until(EC.title_is("Google"))
    

    You can review the complete list of supported expected conditions in the documentation.