About this lesson
Selenium Python driver.get and current_url: visit two pages and compare them
Selenium Python AssignmentsBeginnerGetting Started
How it works
A new driver starts with one browser tab, and driver.get() points that tab somewhere new. The second call does not open a second window. It replaces the first page, and the old document is gone from the screen.
After a load you can ask the driver two different questions. driver.title is the text in the browser tab, taken from the title tag of the page. driver.current_url is the address the browser ended up on. They are separate attributes, both plain strings, and neither takes parentheses.
Because both values are strings, building a report line is simple string joining.
About the assignment
You visit two practice pages in one session, Text Fields and then Buttons. The assignment checks that you read each title at the right moment and that you report where the browser finally stands.
What you will do
- Load the Text Fields page and print its title after the label
Page 1:. - Load the Buttons page using a second call to
get()on the same driver. - Print the new title after the label
Page 2:. - Print the current address of the browser after the label
URL:. Read it from the address attribute, not from the title.
Common mistakes
Reading the first title too late
If you load both pages and then print both titles, each print shows the title of the page that is open now, which is the second one. Print page one before the second
get().Using title where the URL belongs
The URL line needs
current_url. A title is a short phrase, an address starts withhttps://, and mixing them up gives a line that looks well formed but holds the wrong text.Treating get() as if it returned something
The call returns
None. A line that stores its result in a variable gives you nothing to read. Ask the driver for the title and address afterwards.
In a real test suite
Teams keep the base address in one place, often a fixture or a settings file, and build each page address from it. A page object then exposes an open() method so that tests never carry raw URLs.
Every page you load here is stored in the tab history. The next lesson uses that history to step backwards and forwards.
Related lessons
- Open Chrome and Read the Page TitleCovers the first load, the title attribute and closing the browser in finally.
- Go Back and ForwardMoves between the pages you just loaded, using the history buttons.
- Open a Tab and Return to the Original WindowOpens a second tab and returns to the first window.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
driver = webdriver.Chrome()
try:
# Open https://testkru.com/Elements/TextFields and https://testkru.com/Elements/Buttons, print both titles, then the Buttons URL
pass
finally:
driver.quit()