Random Web Browser URL Navigation using Selenium

  • Share this:

Code introduction


This function uses the Selenium library to open a random web browser and navigate to the provided URL.


Technology Stack : Selenium, Web Browser Automation

Code Type : Python Function

Code Difficulty : Intermediate


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

def random_web_browser_url(url):
    """
    This function opens a web browser and navigates to a given URL.
    """
    # Select a random web browser
    browsers = ['firefox', 'chrome', 'edge']
    browser_name = random.choice(browsers)

    # Initialize the web driver
    if browser_name == 'firefox':
        driver = webdriver.Firefox()
    elif browser_name == 'chrome':
        driver = webdriver.Chrome()
    elif browser_name == 'edge':
        driver = webdriver.Edge()

    # Navigate to the provided URL
    driver.get(url)

    # Close the browser after navigation
    driver.quit()

    return "Navigated to " + url