10
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

4
  • what is the error you are getting? You are using incorrect locator - css_selector or xpath ? Commented Nov 11, 2020 at 20:33
  • @Sureshmani sorry, just corrected it now. but still not solved the issue. Commented Nov 11, 2020 at 20:37
  • Where is your WebDriverWait line? because it works okay for me if I have it before print(driver.title) Commented Nov 11, 2020 at 20:53
  • @MatthewKing i tried multiple times but getting empty print. repl.it/@AmericanY/issue#main.py Commented Nov 11, 2020 at 20:58

2 Answers 2

8

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:

Sign up to request clarification or add additional context in comments.

3 Comments

how about if the page title like this how can i add more strings?
@αԋɱҽԃαмєяι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.
@αԋɱҽԃαмєяι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.
-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:

7 Comments

That's still not answering the question. i need that to done on the website which i provided. i tried driver.title and got nothing printed out
@αԋɱҽԃαмєяιcαη You should update your code to show the headless option is set as that is related to cause of the problem
@αԋɱҽԃαмєяιcαη, Nope, worked for me: Studio Apartments for rent in Doha | hapondo was outputted
@MatthewKing you are correct. the headless option is the cause of the issue. still trying to figure out a way for it.
@lionrocker221 sorry, i noticed that the issue is regarding headless browser. do you have any clue ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.