F_Keisuke
F_Keisuke

Reputation: 33

Selenium webdriver with Python - driver.title can't get title

I'm new to Selenium. Could you explain why driver.title can't get the value? Below is a simple webdriver script. I was able to get the value on https://www.google.com, but not on https://twitter.com.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome(options=options)

driver.get("https://twitter.com")

print(driver.title)

Upvotes: 3

Views: 778

Answers (2)

Sameer Arora
Sameer Arora

Reputation: 4507

The title for twitter is getting rendered a little slow, so you can use explicit wait here to wait for the element to get loaded first and then you can fetch the title.

You can do it like:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome(options=options)
driver.get("https://twitter.com")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Log in']")))
print(driver.title)

Note: You have to add the following imports:

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

Upvotes: 3

Viki
Viki

Reputation: 157

Because google they have value within title tag <title>Google</title> but not Twitter so you would not get anything if it is not there :)

Upvotes: -1

Related Questions