Selenium WebDriver |
\\n\\nSelenium 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.
\\nLaunching the Browser
\\nSelenium WebDriver supports multiple browsers (such as Chrome, Firefox, Edge, etc.).
\\nBefore 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.
\\nCode example for launching the browser (Python).
\\nIn 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.
\\nSelenium 4 Example
\\nfrom 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
- Import the required modules (
webdriverandChromeService). \\n - Create a
ChromeServiceobject and specify the path to ChromeDriver. \\n - Create a
ChromeOptionsobject to configure browser launch options. \\n - Use
webdriver.Chrome()to launch the Chrome browser, passing theserviceandoptionsparameters. \\n
In Selenium 3 and earlier versions, the driver path was typically set directly in webdriver.Chrome(), for example:
Selenium 3 Example
\\nfrom 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)\\nClosing the Browser
\\nUse the quit() method to close the browser and end the WebDriver session.
driver.quit()\\nOpening and Closing Webpages
\\nUse the get() method to open a specified URL.
driver.get("https://www.example.com")\\nClosing the Current Tab
\\nUse the close() method to close the current tab.
driver.close()\\nIf there is only one tab, close() will close the entire browser.
\\n
Browser Window Operations
\\nMaximizing the Window
\\nUse the maximize_window() method to maximize the browser window.
driver.maximize_window()\\nMinimizing the Window
\\nUse the minimize_window() method to minimize the browser window.
driver.minimize_window()\\nSetting Window Size
\\nUse the set_window_size(width, height) method to set the size of the browser window.
driver.set_window_size(1024, 768) # Set window size to 1024x768 pixels\\nFullscreen Mode
\\nUse the fullscreen_window() method to set the browser window to fullscreen mode.
driver.fullscreen_window()\\n\\n
Page Navigation
\\nForward and Backward Navigation
\\nUse the back() method to go back to the previous page.
Use the forward() method to go forward to the next page.
driver.get("https://www.baidu.com") driver.get("") driver.back() # Return to Baidu.com driver.forward() # Go forward to \\nRefreshing the Page
\\nUse the refresh() method to refresh the current page.
driver.refresh()\\n\\n
Getting Page Title and URL
\\nGetting the Page Title
\\nUse the title attribute to get the title of the current page.
title = driver.title print("Page Title:", title)\\nGetting the Current URL
\\nUse the current_url attribute to get the URL of the current page.
url = driver.current_url print("Current URL:", url)\\nComprehensive 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:
\\nExample
\\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()\\nAfter executing the above code, the following steps will be performed:
\\n- \\n
- Launch the Chrome browser. \\n
- Open the specified webpage (
).\\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
- \\n
- Go back to the previous page. \\n
- Refresh the current page. \\n
- Close the browser. \\n
The output will be:
\\nPage Title: - More than just technology, it's a dream! Current URL:
YouTip