Selenium WebDriver
Selenium WebDriver is the core component of Selenium; it provides an API for interacting with browsers, allowing developers to control the browser and perform various operations through a programming language.
Launch the browser
Selenium WebDriver supports multiple browsers (such as Chrome, Firefox, Edge, etc.).
Before launching the browser, you need to download and configure the corresponding browser driver (such as ChromeDriver, GeckoDriver, etc.); for details, see selenium installation.
Code example for launching the browser (Python).
In Selenium 4, a Service object was introduced to set the driver path, which is more flexible and aligns with Selenium 4's design specifications.
Selenium 4 example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
service
=
ChromeService
(
executable_path
=
"PATH_TO_DRIVER"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
-
Import the required modules (
webdriverandChromeService). -
creation
ChromeServiceobject, specifying the path to ChromeDriver. -
creation
ChromeOptionsobject, used to configure browser launch options. -
Use
webdriver.Chrome()Launch the Chrome browser, passing inserviceandoptionsparameter was added.
In Selenium 3 and earlier versions, the driver path was typically set directly in webdriver.Chrome(), for example:
Selenium 3 example
from
selenium
import
webdriver
Set the ChromeDriver path
driver_path
=
"/path/to/chromedriver"
Launch the Chrome browser
driver
=
webdriver.
Chrome
(
executable_path
=
driver_path
)
Close the browser
Use the quit() method to close the browser and end the WebDriver session.
driver.quit()
Opening and closing web pages
Use the get() method to open the specified URL.
driver.get("https://www.example.com")
Close the current tab
Use the close() method to close the current tab.
driver.close()
If there is only one tab, close() will close the browser.
Browser window operations
Maximize the window
Use the maximize_window() method to maximize the browser window.
driver.maximize_window()
Minimize window
Use the minimize_window() method to minimize the browser window.
driver.minimize_window()
Set window size
Use the set_window_size(width, height) method to set the browser window size.
driver.set_window_size(1024, 768) # 设置窗口大小为 1024x768 像素
Fullscreen mode
Use the fullscreen_window() method to set the browser window to fullscreen mode.
driver.fullscreen_window()
Page navigation
Forward and backward
Use the back() method to return to the previous page.
Use the forward() method to go forward to the next page.
driver.get("https://www.baidu.com")
driver.get("https://www.runoob.com")
driver.back() # 返回 baidu.com
driver.forward() # 前进到 runoob.com
Refresh the page
Use the refresh() method to reload the current page.
driver.refresh()
Get the page title and URL
Get the page title
Use the title property to get the current page's title.
title = driver.title
print("页面标题:", title)
Get the current URL
Use the current_url property to get the current page's URL.
url = driver.current_url
print("当前 URL:", url)
Comprehensive Example
Below is a complete example demonstrating how to launch the browser, open a web page, manipulate the window, navigate pages, and retrieve page information:
Example
Import the required modules
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
Configure browser options
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Open a web page
driver.
get
(
"https://www.runoob.com"
)
Maximize the window
driver.
maximize_window
(
)
Get the page title and URL
(
"Page title:"
,
driver.
title
)
(
"Current URL:"
,
driver.
current_url
)
Navigate to another page
driver.
get
(
"https://www.jyshare.com"
)
Return to the previous page
driver.
back
(
)
Refresh the page
driver.
refresh
(
)
Close the browser
driver.
quit
(
)
After the above code runs, it will perform the following steps:
-
Launch the Chrome browser.
-
Open the specified web page (
https://www.runoob.com).
-
Maximize the browser window.
-
Get and print the page title and URL.
-
Navigate to another web page (
https://www.jyshare.com).
-
Return to the previous page.
-
Refresh the current page.
-
Close the browser.
The output is:
页面标题: Runoob Tutorial - 学的不仅是技术,更是梦想!
当前 URL: https://www.runoob.com/