Search code examples
pythonseleniumselenium-webdriverwebdriverwaitpage-title

Python selenium get page title


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://hapondo.qa/rent/doha/apartments/studio")
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "/html/head/title"))
)

print(element.text)

Unable to get page title under headless option? Tried to wait and even tried driver.title


Solution

  • You need to take care of a couple of things as follows:

    • To retrieve the Page Title instead of using a you need to use driver.title
    • The hapondo website contains JavaScript enabled elements.

    Solution

    To extract the Page Title you need to induce WebDriverWait for the title_contains() and you can use either of the following Locator Strategy:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      options = webdriver.ChromeOptions() 
      options.add_argument('--headless')
      options.add_argument('--window-size=1920,1080')
      driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get('https://hapondo.qa/rent/doha/apartments/studio')
      WebDriverWait(driver, 10).until(EC.title_contains("hapondo"))
      print(driver.title)
      
    • Console Output:

      Studio Apartments for rent in Doha | hapondo
      

    References

    You can find a couple of relevant detailed discussions in: