Python selenium get page title

Viewed 6778
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

2 Answers

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:

By "page title", I'm assuming you mean the text that appears on the tab at the top of the browser.

Solution that changes little of your code:

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


driver = webdriver.Firefox(executable_path=r"[path]")
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.get_attribute("innerHTML"))
Output: Studio Apartments for rent in Doha | hapondo

Another way of getting that text is by simply using driver.title.

"The title method is used to retrieve the title of the webpage the user is currently working on. "

Source: GeeksForGeeks

from selenium import webdriver
import time
driver = webdriver.Firefox(executable_path=r"[PATH]")
driver.get("https://hapondo.qa/rent/doha/apartments/studio")
time.sleep(2)

print(driver.title)
#Output: Studio Apartments for rent in Doha | hapondo

Alternate solution which changes very little:

Related