Skip to content
CodeKruPro

Visit Two Pages with driver.get()

2/80

Visit Two Pages with driver.get()

Each get replaces the current page. current_url is the address the browser landed on.

Task:

  1. Open Text Fields and print its title
  2. Open Buttons and print its title
  3. Print the Buttons URL

Print the lines below exactly.

Expected output:

Page 1: Text Fields
Page 2: Buttons
URL: https://testkru.com/Elements/Buttons

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

  1. Load the Text Fields page and print its title after the label Page 1:.
  2. Load the Buttons page using a second call to get() on the same driver.
  3. Print the new title after the label Page 2:.
  4. 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 with https://, 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.

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.

main.py
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()