How to get title of a webpage using Selenium in Python?
Published on Aug. 22, 2023, 12:15 p.m.
To get the title of a webpage using Selenium in Python, you can use the title
attribute of the WebDriver
object that you have instantiated. Here’s an example:
from selenium import webdriver
# Create a new instance of the browser driver
driver = webdriver.Chrome()
# Navigate to the desired web page
driver.get('https://www.example.com')
# Get the title of the web page
title = driver.title
# Print the title to the console
print(title)
# Close the browser window
driver.quit()
In this example, we first create a new instance of the Chrome browser driver using the webdriver.Chrome()
method. We then navigate to the desired webpage using the get()
method, which loads the page in the browser window. Next, we use the title
attribute of the WebDriver
object to get the title of the webpage.
Finally, we print the title to the console using the print()
function, and close the browser window using the quit()
method of the WebDriver
object.
You can also use similar code to extract other elements of the webpage using Selenium.