Testing the Link Functionality Using Selenium

To test the link functionality using Selenium, I followed a series of steps:

Step 1: Install Packages

First, I installed the necessary packages in my environment by running the following commands to ensure that Selenium and Google Colab's Selenium integration were available:

%pip install -q google-colab-selenium
%pip install -q google-colab-selenium
%pip install -q google-colab-selenium[undetected]
Query Execution Result 1
Step 2: Import Libraries

Then, I imported the required libraries to work with Selenium and Google Colab's Selenium support. I imported the By module from selenium.webdriver.common.by and the google_colab_selenium library as gs for handling the driver setup:

from selenium.webdriver.common.by import By
import google_colab_selenium as gs
Step 3: Initialize Browser

I initialized an undetected Chrome browser instance using gs.UndetectedChrome(), which would allow me to avoid detection by the website. I then used this browser instance to load the BBC website:

driver = gs.UndetectedChrome()
driver.get("https://www.bbc.com/")
Step 4: Test the Link Functionality

Next, I tested the link functionality by finding the "News" link on the BBC homepage. I located the link using its link text ("News") and clicked on it:

news_link = driver.find_element(By.LINK_TEXT, "News")
news_link.click()

Afterward, I printed the URL of the page I was redirected to after clicking the link:

print(driver.current_url)
Step 5: Close the Browser

Once I finished testing, I closed the browser using the quit() method:

driver.quit()
Query Execution Result 2
Step 6: Second Test with Partial Link Text

In a second part of the test, I repeated similar steps but clicked on a different link—this time, using the PARTIAL_LINK_TEXT method to locate a part of the text ("How to be happier"). I followed the same steps to load the page, click the link, and print the URL:

news_link = driver.find_element(By.PARTIAL_LINK_TEXT, "How to be happier")
news_link.click()
print(driver.current_url)
Step 7: Close Browser Again

Finally, I closed the browser once more:

driver.quit()
Query Execution Result 3
Query Execution Result 4

Here is the link to the Google Colab Selenium implementation.