How to get the title and URL of the page in Selenium with python?


We can get the title and URL of the page in Selenium.

For getting the title of the browser, the title method is to be used.

For getting the URL of the page, the current_url method is to be used.

Both these methods can be used for testing if we are navigated to the correct page and page title.

Example

Code Implementation

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
# to print the page title in console
print(driver.title)
# to print the current URL in console
print(driver.current_url)
#to refresh the browser
driver.refresh()
#to close the browser
driver.close()

Updated on: 29-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements