Detailed explanation of Selenium framework 1

Selenium is a tool for web application testing that runs directly in the browser, just like a real user would do. Supported browsers include Chrom, Firefox, Safari, IE; supported scripting languages ​​include Java, C, Python, Javascript, Ruby, PHP, etc.

Selenium mainly supports automation of all major browsers by using WebDriver, enabling different browser backends to be used transparently, thus enabling cross-browser and cross-platform automation. WebDriver is an API and protocol that defines an interface for controlling the behavior of a web browser. Each browser is supported by a specific WebDriver, called a driver.
The driver is the component responsible for delegating to the browser and handling the communication between Selenium and the browser.

1. How Selenium works

Selenium is mainly divided into three parts, script files, webdriver, and browser. The script file is used to start the webdriver, act as a server, and then send a request, and the webdriver parses the request information, starts the browser, and executes the user request.

insert image description here

2. Install

pip install selenium

3. Install the browser driver

Selenium supports all major browsers, Chrome/Chromium, Firefox, Internet Explorer, Edge and Safari through WebDriver.
For the download of the driver, you can refer to the selenium official website .

3.1 Driver Management Software

Most machines automatically update their browsers, but drivers don't. To ensure you get the correct browser driver, there are third-party libraries available.

from selenium import webdriver
from selenium.webdriver.chrom.service import Service
from webdriver_manager.chrome import ChromeDriverManager  #导入WebDriver管理器,其他浏览器替换即可

#使用install获取管理器使用的位置,将其传递给服务类
service = Service(executable_path=ChromeDriveManager().install())   

#初始化驱动时使用Service实例
driver = webdriver.Chrome(service=service)

3.2 Hardcoded location

This method requires manually specifying the driver location.

from selenium.webdriver,chrom.service import Service
from selenium import webdriver

#executable_path指定本地驱动程序路径
service = Service(executable_path='/path/to/chromedriver')
   
driver = webriver.Chrom(service=service) 

4. Simple use of selenium

Selenium sends browser commands to do something or request information. The following describes the complete process involved in using selenium.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

#①初始化驱动
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))   

#②发送请求
driver.get("https://www.selenium.dev/selenium/web/web-form.html")

#③请求浏览器多种类型的信息,包括窗口句柄、浏览器大小/位置、cookie等
title = driver.title

#④建立等待策略,在使用selenium与浏览器交互过程中,容易出现两者不同步的现象,为实现状态同步,使用隐式等待。
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

#⑧结束会话。将结束驱动程序进程,默认情况下会关闭浏览器。
driver.quit()

A detailed explanation of each part follows.

Selenium framework detailed explanation two

Guess you like

Origin blog.csdn.net/weixin_48030475/article/details/127252042