A2oz

How Do You Open a URL in Selenium?

Published in Web Development 2 mins read

You can open a URL in Selenium using the get() method of the WebDriver object. This method takes the URL as an argument and navigates the browser to the specified web page.

Here's a simple example:

from selenium import webdriver

# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Open the URL
driver.get("https://www.example.com")

# Close the browser
driver.quit()

Understanding the Code

  • from selenium import webdriver: This line imports the necessary WebDriver module from the Selenium library.
  • driver = webdriver.Chrome(): This line creates a new instance of the Chrome WebDriver. You can use other browsers like Firefox, Edge, or Safari by changing the webdriver class.
  • driver.get("https://www.example.com"): This line opens the URL "https://www.example.com" in the browser controlled by the WebDriver.
  • driver.quit(): This line closes the browser window and ends the Selenium session.

Practical Insights

  • You can use the get() method to open any valid URL, including websites, web applications, and even local HTML files.
  • You can use the current_url attribute to get the current URL of the page loaded in the browser.
  • Selenium can be used for various purposes, including web automation, web scraping, and testing web applications.

Related Articles