How to use Selenium WebDriver in Python
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.
