9
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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

7

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • how about if the page title like [this](https://hapondo.qa/rent/al-daayen/lusail/les-maisons-blanches/3-bedroom-furnished-apartment-2266) how can i add more strings? – αԋɱҽԃ αмєяιcαη Nov 11 '20 at 23:55
  • @αԋɱҽԃαмєяιcαη The phrase `hapondo` is a string which I'm sure would be present in the _Page Title_ for any webpage with `https://hapondo.qa`. You can use any string which suits your requirement. – undetected Selenium Nov 12 '20 at 07:29
  • @αԋɱҽԃαмєяιcαη I had a look at the **Rent** section but was unable to conclude which string among `bedroom`, `furnished`, `apartment`, `in` or `by` could be common within the _Page Title_ for all the rented properties. You can use the common term as the expected _text_ within the Expected Conditions. – undetected Selenium Nov 12 '20 at 14:10
-2

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:

lionrocker221
  • 171
  • 3
  • 8