系列文章目录
selenium
macOS selenium
前言
selenium:Web应用程序测试工具
一、ChromeDriver下载
ChromeDriver
chromedriver_mac64_m1.zip
- Latest stable release: ChromeDriver 102.0.5005.61
方法2 通过brew安装
brew install --cask chromedriver
╰─➤ brew install --cask chromedriver 1 ↵
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 6 taps (homebrew/cask-versions, homebrew/command-not-found, homebrew/core, homebrew/cask, homebrew/cask-fonts and homebrew/cask-drivers).
==> New Formulae
bore-cli dtrx flix hatch naml terramate xpipe
cxgo erlang@24 gokart helmify opentelemetry-cpp tinysearch
==> Updated Formulae
Updated 398 formulae.
==> New Casks
bili-downloader cardpresso hdfview oso-cloud rustdesk xmind8
bilibili-official contour input-source-pro roam-research swiftcord
==> Updated Casks
Updated 335 casks.
==> Deleted Casks
coarchi font-gentium-plus-book race-into-space xmind-zen
crystax-ndk plistedplus stubbymanager yammer
==> Downloading https://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_mac64_m1.zip
######################################################################## 100.0%
==> Installing Cask chromedriver
==> Linking Binary 'chromedriver' to '/opt/homebrew/bin/chromedriver'
🍺 chromedriver was successfully installed!
╰─➤ chromedriver --version
╰─➤ chromedriver --version
ChromeDriver 102.0.5005.61 (0e59bcc00cc4985ce39ad31c150065f159d95ad3-refs/branch-heads/5005@{#819})
brew 更新 ChromeDriver 124.0.6367.78
brew reinstall --cask chromedriver
╰─➤ chromedriver --version
ChromeDriver 124.0.6367.78
二、安装selenium
pip
╰─➤ pip install selenium
- 验证
╰─➤ python
Python 3.9.12 (main, May 8 2022, 17:57:49)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import selenium
>>>
selenium 安装
pip3 install selenium
pip 24.0
python3.11 -m pip install --upgrade pip
╰─➤ pip3 --version
pip 24.0 from /opt/homebrew/lib/python3.11/site-packages/pip (python 3.11)
三、selenium基本使用
1. web-form 测试
web-form_test.py
from selenium import webdriver
from selenium.webdriver.common.by import By # 导入 By 类
import time
# 1. 创建浏览器对象
driver = webdriver.Chrome()
# 2. 打开浏览器
url = "https://www.selenium.dev/selenium/web/web-form.html"
driver.get(url)
# 3. 请求浏览器信息
title = driver.title
# 4. 隐式等待
driver.implicitly_wait(2) # 等待2秒
# 5. 发送命令 查找元素
text_box = driver.find_element(By.NAME, "my-text")
submit_button = driver.find_element(By.CSS_SELECTOR, "button")
# 6. 操作元素
text_box.send_keys("Selenium")
time.sleep(2) # 等待2秒
submit_button.click()
time.sleep(2) # 等待2秒
# 7. 获取元素信息
# 假设您要获取的是提交后显示的消息元素,您需要先定位到这个元素
# 这里需要知道消息元素的定位方式,比如它的id、class或其他属性
# 假设消息元素的id是"message"
message = driver.find_element(By.ID, "message")
text = message.text
# 8. 结束会话
# 退出浏览器
driver.quit()
2.测试
test01.py
from selenium import webdriver
driver = webdriver.Chrome()
base_url = 'https://www.baidu.com'
driver.get(base_url)
3. 打开百度搜索内容
from selenium import webdriver
import time
# 创建浏览器对象
driver = webdriver.Chrome()
# 打开浏览器
url = 'https://www.baidu.com'
driver.get(url)
time.sleep(2)
# 获取输入框对象
input = driver.find_element_by_id("kw")
# 输入内容
input.send_keys("macos")
time.sleep(2)
# 获取点击按钮对象
button = driver.find_element_by_id("su")
# 点击按钮
button.click()
time.sleep(5)
# 退出浏览器
driver.quit()