-
Google Chrome Google Chrome 是一种由 Google 开发的网页浏览器,用于访问和查看网页。
-
ChromeDriver ChromeDriver 是一个独立的服务器,用于与 Google Chrome 浏览器进行通信。它实现了 WebDriver 协议,允许程序自动控制 Chrome 浏览器。当使用 Selenium 进行自动化测试时,ChromeDriver 充当 Selenium WebDriver 和浏览器之间的桥梁。
-
Selenium Selenium 是一个广泛使用的自动化测试框架,支持多种浏览器(如 Chrome、Firefox、Safari 等)。通过 Selenium,开发者可以编写脚本来模拟用户在浏览器中的操作,例如打开网页、点击按钮、输入文本等。Selenium 通过 WebDriver 接口与浏览器进行交互,而在使用 Chrome 时则需要 ChromeDriver 来实现这一功能。
Selenium 通过 ChromeDriver 控制 Chrome 浏览器执行自动化操作。安装 Google Chrome 和 ChromeDriver,并且保证两者的大版本号相同,版本号不同无法驱动浏览器。
windows 软件
- Google Chrome 便携不自动升级版 https://github.com/zzp198/Google-Chrome-Portable
- ChromeDriver
- 版本在 114 以下:http://chromedriver.storage.googleapis.com/index.html
- 版本在 127 以上:https://googlechromelabs.github.io/chrome-for-testing/#stable
- 其他版本下载,改版本号下载,如版本
133.0.6943.99
64 位下载地址是:https://storage.googleapis.com/chrome-for-testing-public/133.0.6943.99/win64/chromedriver-win64.zip
- Selenium 安装 Python 环境之后,
pip3 install selenium
镜像构建
pct enter 120
root@debian12-staging:~# cd /mnt/storage/files/wwwroot/xxxx/docker_python/
docker build -f Dockerfile -t 192.168.0.114:8039/python:3.9-slim-v20241127 .
docker push 192.168.0.114:8039/python:3.9-slim-v20241127
docker save -o python.3.9-slim-v20241127.tar 192.168.0.114:8039/python:3.9-slim-v20241127
scp -P 22 python.3.9-slim-v20241127.tar 74.xx.xx.157:/root
docker load -i python.3.9-slim-v20241127.tar
docker-compose.yml
docker exec -it app_python /bin/bash
version: "3.8"
services:
app_python:
image: 192.168.0.114:8039/python:3.9-slim-v20241127
container_name: app_python
restart: always
ports:
- 127.0.0.1:4444:4444
user: 0:0 # 使用 root 用户
volumes:
- ${WEB_ROOT}/app_python:/app
environment:
- TZ=Asia/Shanghai
command:
/bin/bash -c 'pip3 install requests && tail -f /dev/null'
# VARIABLE=value #comment
WEB_ROOT=/mnt/storage/files/wwwroot/code
APP_DATA=/mnt/storage/files/wwwroot/code
XPath
# 滚动到页脚
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 等待点击不存在抛出异常
xxxx_button_element = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//button[@id='xxxx']"))
)
# 等待显示不存在抛出异常
xxxx_input_element = WebDriverWait(driver, 60).until(
EC.visibility_of_element_located((By.XPATH, "//input[@name='xxxx']"))
)
def find_element_and_contain_text(d, by, exp, text):
"""
检查元素存在并且文本匹配
Parameters:
d (driver): webdriver
by (by): By.ID or By.CLASS_NAME or By.XPATH ...
exp (str): exp
text (str): text to be match
"""
element = d.find_element(by, exp)
return (element.is_displayed()) and (text in element.text)
def find_element_exist_by_exp(d, by, exp):
"""
查找是否查找指定元素,依赖包
from selenium.common.exceptions import NoSuchElementException
使用示例
button_complete_element = driver.find_element(By.XPATH, "//button[@id='xxx']")
注意事项
driver.find_element(By.CLASS_NAME, 'a b'),By.CLASS_NAME 方法只接受单个类名作为参数,而不支持包含空格的多个类名。多个样式可以用 driver.find_element(By.CSS_SELECTOR, '.a.b')
driver.find_element(By.XPATH, "//div[@class='a b']") 匹配完全等于的元素
Parameters:
d (driver): webdriver
by (by): By.ID or By.CLASS_NAME or By.XPATH ...
exp (str): exp
"""
try:
return d.find_element(by, exp)
except NoSuchElementException:
return None