How to open Webpage URLs in Selenium

Open Webpage Urls In Python Using Selenium

In this article, we’ll learn how to access and open webpage URLs in Selenium. Python Selenium is a powerful tool for programmatically manipulating a web browser. It is compatible with all browsers, runs on all major operating systems, and its scripts are written in a variety of languages, including Python, Java, C#, and others. Out of which we will be working with Python.

Selenium Python bindings provide a straightforward API for accessing Selenium WebDrivers such as Firefox, Internet Explorer, Chrome, Remote, and others. Python versions 2.7, 3.5, and higher are presently supported by Selenium.

Recommended Read: Introduction to Python Selenium – Installation and Setup

The code sample given below will surely help you to open webpage URLs in Python:

Opening URLs using Selenium

Let’s now learn how to access web pages and open URLs in Python Selenium. This is the most basic requirement of using Selenium. Once you understand this, you only have to play around with XPaths and identify how to use the data that you scrape with Python Selenium

1. Installing Python Selenium

We’ll use the pip command to install the selenium package.

python -m pip install selenium

2. Importing modules

Let’s now import the selenium modules within our Python code to start working with it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('./chromedriver.exe')

Note: Now we have installed Selenium but to access open web browsers and make them accessible for our code, we need to download the browser’s official driver and note the path of it

Here we gave the path as ‘./chromedriver.exe’ because we have put the driver in the same directory as of Python script, if you save it anywhere else then you will have to provide a full path to it.

3. Opening URL example

The URL in Python Selenium is opened or fetched using the get() method of the selenium module

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver.exe')

driver.get("https://www.google.com")
driver.close()

This opens Google’s site in Chrome in a new test browser window.

The close() method is used to close the browser window.

4. Title of the Webpage

We can get the title of the webpage opened as text output in our console/terminal window by using the following python command:

print(driver.title)

5. Search for a query on Google

Syntax: website/search?q=’topic to search’

This shows results on python in Google search in Chrome browser in a new window.

driver.get("https://www.google.com/search?q =Python")

6. Move Back and Forth within the Browser History

The Back driver goes one step backward in browser history.

Syntax: driver.back()

The forward driver goes one step forward in browser history

Syntax: driver.forward()

Example Implementation:

from selenium import webdriver

driver = webdriver.Chrome("./chromedriver.exe")

# opens Google
driver.get("https://www.google.com")

# open python official website
driver.get("https://www.python.org")

Now, here firstly Google will open in a new window and then python official website in the same window and over the Google website

driver.back()
# will go to Google

driver.forward()
# will go to python official website

You would need to use something like time.sleep(5) between back and forward methods to actually notice the transition.

Conclusion

Hope you have learned to open webpage URLs in Python using the Selenium library and are ready to try it yourself.