Opening Links Using Selenium in Python


When working with automation tasks, opening links programmatically is a very common requirement. Selenium, a popular web testing framework, provides powerful tools to handle web pages and perform various actions like opening links and etc. In this article, we will learn various methods to open links in Selenium using Python.

Prerequisites

Before we get started just make sure that you have the following software installed:

  • Python: Install Python, if you haven't already.

  • Selenium: Install Selenium by running pip install selenium in your command prompt.

  • Web Driver: Selenium requires a web driver to interface with the chosen browser. You need to download the browser-specific web driver.

pip install selenium

Approach 1: Opening Links Using get() Method

The easiest method to open a link using Selenium is by using the get() method of the WebDriver object. This method instructs the browser to navigate to the specified URL.

Syntax

get()

driver.get(url)

Parameters:

  • Url:Link that you intend to open.

Explanation

  • Import the webdriver class from selenium..

  • Create a driver object and call the get() method by passing the desired url to open.

Example

from selenium import webdriver

# initialize the web driver
driver = webdriver.Firefox()

# Open the tutorials point website using get() method
driver.get("https://www.tutorialspoint.com")

Output

Approach 2: Opening Links by Clicking Elements

Suppose you have some links embedded in a webpage, for example, buttons, images, and links. In that scenario, we cannot directly use the get() method to open those links. We need to locate the elements using selenium and then perform the click operation to open the links.

Syntax

find_element() : The find_element() is used to locate the element in a webpage, find_element() can be used with Ids, classes and xpaths.

driver.find_element(By.XPATH, "xpath")
  • xpath:Xpath of the element

click(): the click() method is used to perform a click operation on an HTML element.

element.click()

Explanation

  • Open the page in which you want to open the links.

  • Use the find_element() method to locate the element to be clicked. We are using XPath in this scenario.

  • The find_element() method will return an element object, and perform the click operation on the element by using the click() method.

Example

from selenium import webdriver
from selenium.webdriver.common.by import By

# initialize the web driver
driver = webdriver.Firefox()

# Open the tutorials point website using get() method
driver.get("https://www.tutorialspoint.com/index.htm")

# clicking the courses tab in homepage.
driver.find_element(By.XPATH,"/html/body/header/nav/div/div[1]/ul[2]/li[2]/a").click()

Output

Approach 3: Opening Links in New Tabs or Windows

Now let's discuss how we can open links in new tabs or new windows. This can be very handy when we want to work with multiple tabs.

Syntax

execute_script()

execute_script(script)
  • script: The script that you want to execute.

Explanation

  • Use the exeute_script() method to open a new window by using the command window.open().

  • Switch to the newly opened window using the switch_to.window() method.

  • Now as usual use the driver.get() method to open the link.

Example

from selenium import webdriver
from selenium.webdriver.common.by import By

# initialize the web driver
driver = webdriver.Firefox()

# Open a new tab
driver.execute_script("window.open();")

# Switch to the newly opened tab
driver.switch_to.window(driver.window_handles[1])

# Open the tutorials point website using get() method
driver.get("https://www.tutorialspoint.com")

Output

Conclusion

In this article, we have learned multiple approaches to open links using Selenium in Python. including opening links directly with the get() method, clicking elements containing links, or opening links in new tabs/windows. Depending on your use case, you can choose the approach that suits you best.

Updated on: 09-Aug-2023

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements