YouTip LogoYouTip

Selenium Webdriver

Selenium WebDriver |

\\n\\n

Selenium WebDriver is the core component of Selenium. It provides APIs for interacting with browsers, allowing developers to control browsers and perform various operations using programming languages.

\\n

Launching the Browser

\\n

Selenium WebDriver supports multiple browsers (such as Chrome, Firefox, Edge, etc.).

\\n

Before launching the browser, you need to download and configure the corresponding browser driver (such as ChromeDriver, GeckoDriver, etc.). For details, please refer to Selenium Installation.

\\n

Code example for launching the browser (Python).

\\n

In Selenium 4, the Service object was introduced to set the driver path. This approach is more flexible and aligns with Selenium 4's design specifications.

\\n

Selenium 4 Example

\\n
from selenium import webdriver\\n\\nfrom selenium.webdriver.chrome.service import Service as ChromeService\\n\\nservice = ChromeService(executable_path="PATH_TO_DRIVER")\\n\\n options = webdriver.ChromeOptions()\\n\\n driver = webdriver.Chrome(service=service, options=options)
\\n
    \\n
  1. Import the required modules (webdriver and ChromeService).
  2. \\n
  3. Create a ChromeService object and specify the path to ChromeDriver.
  4. \\n
  5. Create a ChromeOptions object to configure browser launch options.
  6. \\n
  7. Use webdriver.Chrome() to launch the Chrome browser, passing the service and options parameters.
  8. \\n
\\n

In Selenium 3 and earlier versions, the driver path was typically set directly in webdriver.Chrome(), for example:

\\n

Selenium 3 Example

\\n
from selenium import webdriver\\n\\n# Set ChromeDriver path\\n\\n driver_path ="/path/to/chromedriver"\\n\\n# Launch Chrome browser\\n\\n driver = webdriver.Chrome(executable_path=driver_path)
\\n

Closing the Browser

\\n

Use the quit() method to close the browser and end the WebDriver session.

\\n
driver.quit()
\\n

Opening and Closing Webpages

\\n

Use the get() method to open a specified URL.

\\n
driver.get("https://www.example.com")
\\n

Closing the Current Tab

\\n

Use the close() method to close the current tab.

\\n
driver.close()
\\n

If there is only one tab, close() will close the entire browser.

\\n
\\n

Browser Window Operations

\\n

Maximizing the Window

\\n

Use the maximize_window() method to maximize the browser window.

\\n
driver.maximize_window()
\\n

Minimizing the Window

\\n

Use the minimize_window() method to minimize the browser window.

\\n
driver.minimize_window()
\\n

Setting Window Size

\\n

Use the set_window_size(width, height) method to set the size of the browser window.

\\n
driver.set_window_size(1024, 768) # Set window size to 1024x768 pixels
\\n

Fullscreen Mode

\\n

Use the fullscreen_window() method to set the browser window to fullscreen mode.

\\n
driver.fullscreen_window()
\\n
\\n

Page Navigation

\\n

Forward and Backward Navigation

\\n

Use the back() method to go back to the previous page.

\\n

Use the forward() method to go forward to the next page.

\\n
driver.get("https://www.baidu.com") driver.get("") driver.back() # Return to Baidu.com driver.forward() # Go forward to 
\\n

Refreshing the Page

\\n

Use the refresh() method to refresh the current page.

\\n
driver.refresh()
\\n
\\n

Getting Page Title and URL

\\n

Getting the Page Title

\\n

Use the title attribute to get the title of the current page.

\\n
title = driver.title print("Page Title:", title)
\\n

Getting the Current URL

\\n

Use the current_url attribute to get the URL of the current page.

\\n
url = driver.current_url print("Current URL:", url)
\\n

Comprehensive Example

\\n
\\n

The following is a complete example demonstrating how to launch a browser, open a webpage, manipulate windows, navigate pages, and retrieve page information:

\\n

Example

\\n
# Import required modules\\n\\nfrom selenium import webdriver\\n\\nfrom selenium.webdriver.chrome.service import Service as ChromeService\\n\\n# Set correct driver path\\n\\n service = ChromeService(executable_path="./chromedriver-mac-arm64/chromedriver")\\n\\n# Configure browser options\\n\\n options = webdriver.ChromeOptions()\\n\\n driver = webdriver.Chrome(service=service, options=options)\\n\\n# Open webpage\\n\\n driver.get("")\\n\\n# Maximize window\\n\\n driver.maximize_window()\\n\\n# Get Page Title and URL\\n\\nprint("Page Title:", driver.title)\\n\\nprint("Current URL:", driver.current_url)\\n\\n# Navigate to another page\\n\\n driver.get("https://www.jyshare.com")\\n\\n# Go back to previous page\\n\\n driver.back()\\n\\n# Refresh page\\n\\n driver.refresh()\\n\\n# Close browser\\n\\n driver.quit()
\\n

After executing the above code, the following steps will be performed:

\\n
    \\n
  • Launch the Chrome browser.
  • \\n
  • Open the specified webpage ().
  • \\n
\\nImage 1\\n
    \\n
  • Maximize the browser window.
  • \\n
  • Retrieve and print the page title and URL.
  • \\n
  • Navigate to another webpage (https://www.jyshare.com).
  • \\n
\\nImage 2\\n
    \\n
  • Go back to the previous page.
  • \\n
  • Refresh the current page.
  • \\n
  • Close the browser.
  • \\n
\\n

The output will be:

\\n

Page Title: - More than just technology, it's a dream! Current URL:

← Selenium Ele OperatorSelenium Intro β†’