Getting Started with Selenium WebDriver
1. What is Selenium WebDriver
WebDriver drives the browser in a localized manner, just like the user locally Or as done on a remote machine using Selenium server, this marks a leap in browser automation.
Selenium WebDriver refers to the implementation of language binding and each browser control code. This is often called WebDriver.
Selenium WebDriver is a W3C recommendation.
WebDriver is designed to be a simple and concise programming interface.
WebDriver is a simple object-oriented API.
It drives the browser effectively.
2. Install Selenium WebDriver
The environment used in this article is python3.11 win10 64-bit firefox browser, so the browser driver used in this article is Firefox’s geckodriver. If If you are using another browser, just select your corresponding browser driver.
2.1 Install the selenium class library
The easiest way is to install it directly using pip
pip install selenium
2.2 Install the browser driver
Through WebDriver, Selenium supports all major browsers on the market, such as Chrome, Firefox, Internet Explorer, Edge and Safari. WebDriver tries to use the browser's built-in automation support to drive the browser.
Since except Internet All driver implementations outside of Explorer are provided by the browser vendors themselves, so these drivers are not included in the standard Selenium distribution. This section describes the basic requirements for using different browsers.
Open The following URL https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/install_drivers/
finds the link to download the browser driver. Here you can see multiple browser drivers supported by Selenium. , just download the corresponding driver for whatever browser you have installed on your computer. This article uses Firefox, so choose the Firefox driver.
Click the firefox driver download link and go to the releases page of github. You can see the drivers of each version. This article is win10 64-bit, and the selected one is geckodriver-v0. 32.0-win-aarch74.zip, just select the corresponding file according to your system.
After decompression, it is an executable file, as shown below:
2.3 Configure environment variables
Open My Computer->Properties->Advanced System Settings->Environment Variables, double-click path, click New, enter the directory where the driver geckodriver.exe is located, and then click OK all the way.
Simply verify, open a new console, enter the driver file name, this It is geckodriver.exe, and it can be opened normally
Of course, there is another way, which is to directly put the driver into the Scripts file in your Python directory folder, you can also open it directly when executing a python script. For example, my directory is D:\Python\Python311\Scripts
3. Write the first Selenium script
After you complete the Selenium installation and driver installation, you can start writing Selenium scripts .
All Selenium does is sends commands to the browser to perform some action or make a request for information. Most of the things you will do with Selenium are a combination of the following basic commands:
1. Open a session using the driver instance
driver = webdriver.Firefox()
2. Perform operations on the browser
In this example, we navigate to a web page.
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
3. Request browser information
You can request a series of information about the browser, including window handle, browser size/location, cookies, alerts, etc.
title = driver.title
4. Create a waiting strategy
Connect the code with Synchronizing the current state of the browser is one of the biggest challenges with Selenium, and getting it right is an advanced topic. Basically, you want to make sure that an element is on the page before you try to position it, and that you want to make sure that the element is on the page before you try to interact with it. The element is interactive. Implicit wait is rarely the best solution, but is easiest to demonstrate here
driver.implicitly_wait(0.5)
5.发送命令 查找元素
大多数Selenium会话中的主要命令都与元素相关, 如果不先找到元素, 就无法与之交互
text_box = driver.find_element(by=By.NAME, value="my-text") submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button")
6.操作元素
对于一个元素, 只有少数几个操作可以执行, 但您将经常使用它们
text_box.send_keys("Selenium") submit_button.click()
7.获取元素信息
value = message.text
8.结束会话
这将结束驱动程序进程, 默认情况下, 该进程也会关闭浏览器. 无法向此驱动程序实例发送更多命令
driver.quit()
让我们将这8个部分组合成一个完整的脚本, 包括需要使用的库
from selenium import webdriver from selenium.webdriver.common.by import By def test_eight_components(): driver = webdriver.Firefox() driver.get("https://www.selenium.dev/selenium/web/web-form.html") title = driver.title assert title == "Web form" driver.implicitly_wait(0.5) text_box = driver.find_element(by=By.NAME, value="my-text") submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button") text_box.send_keys("Selenium") submit_button.click() message = driver.find_element(by=By.ID, value="message") value = message.text assert value == "Received!" driver.quit()
The above is the detailed content of How to use Selenium WebDriver in Python. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
