发布于2026-07-12 阅读(0)
扫一扫,手机访问
# 传统contains匹配(部分包含) driver.find_element(By.XPATH, '//div[contains(@id, "user-profile")]') # 正则匹配(精确模式) driver.find_element(By.XPATH, '//div[matches(@id, "^user-profile-\d+$")]')
| 函数 | 语法示例 | 适用场景 |
|---|---|---|
matches() | matches(@attr, 'pattern') | 完整正则匹配(XPath 3.0+) |
regexp: | //div[@id=regexp:'user-profile-.*'] | 部分浏览器扩展语法 |
translate() | translate(@class, '0-9', '') | 预处理后再匹配 |
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
# 匹配动态ID(Chrome扩展语法)
element = driver.find_element(
By.XPATH,
'//div[starts-with(@id, "temp-") and contains(@id, "-container")]'
# 或尝试(部分浏览器支持):
# '//div[@id=regexp:"temp-.*-container"]'
)
import re
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# 获取所有div元素
divs = driver.find_elements(By.XPATH, '//div')
# 使用Python正则筛选
target_div = None
for div in divs:
if re.match(r'^user-profile-\d+$', div.get_attribute('id')):
target_div = div
break
from lxml import html
import requests
# 获取网页内容
page = requests.get("https://example.com")
tree = html.fromstring(page.content)
# 使用XPath 3.0正则匹配
elements = tree.xpath('//div[matches(@id, "^user-profile-\d+$")]')
for el in elements:
print(el.text_content())
products = driver.find_elements(By.XPATH, '//li[starts-with(@data-product-id, "prod_")]')
for product in products:
product_id = re.search(r'prod_([a-f0-9]+)', product.get_attribute('data-product-id')).group(1)
print(f"商品ID: {product_id}, 名称: {product.text}")
解决方案:
# 方法1:使用contains()组合
buttons = driver.find_elements(By.XPATH, '//button[contains(@class, "submit-btn") and contains(@class, "btn-")]')
# 方法2:Python正则处理
buttons = driver.find_elements(By.XPATH, '//button[contains(@class, "submit-btn")]')
valid_buttons = [
btn for btn in buttons
if re.search(r'btn-\d{8}\d{4}', btn.get_attribute('class'))
]
解决方案:ERR_404
页面未找到
# 匹配包含特定错误码的提示框
error_div = driver.find_element(
By.XPATH,
'//div[contains(@class, "alert") and .//p[matches(@class, "code") and text()="ERR_404"]]'
)
candidates = driver.find_elements(By.XPATH, '//div[@id]') # 先找所有带ID的div
targets = [d for d in candidates if re.match(r'^temp-\d+$', d.get_attribute('id'))]
**避免过度正则**:优先使用`starts-with()`、`contains()`这些基础函数
# 优于正则的方案 driver.find_element(By.XPATH, '//input[starts-with(@name, "user_") and contains(@name, "_email")]')**缓存结果**:对重复使用的正则表达式进行编译
import re pattern = re.compile(r'^user-profile-\d+$') # 使用时直接调用 pattern.match(string)
# 替代方案1:使用CSS选择器+正则组合
elements = driver.find_elements(By.CSS_SELECTOR, 'div[id^="temp-"]')
valid_elements = [el for el in elements if re.search(r'temp-\d+-container', el.get_attribute('id'))]
# 替代方案2:使用Ja vaScript执行XPath(高级)
**解决方案**:订单号: ORD20230415001
# 方法1:XPath 2.0+(需特定环境)
# driver.find_element(By.XPATH, '//div[matches(text(), "订单号: ORD\d{11}")]')
# 方法2:Python处理
divs = driver.find_elements(By.XPATH, '//div[contains(text(), "订单号:")]')
for div in divs:
match = re.search(r'订单号: (ORD\d{11})', div.text)
if match:
print("找到订单:", match.group(1))
def generate_xpath_regex(base_path, attr_name, pattern):
"""动态生成带正则的XPath
:param base_path: 基础路径如 '//div'
:param attr_name: 属性名如 '@id'
:param pattern: 正则表达式字符串
"""
# 对于支持regexp:的浏览器
xpath_regexp = f"{base_path}[{attr_name}=regexp:'{pattern}']"
# 通用方案(Python处理)
xpath_contains = f"{base_path}[contains({attr_name}, '{pattern.split('*')[0]}')]"
return {
'regexp_syntax': xpath_regexp,
'contains_fallback': xpath_contains
}
# 使用示例
paths = generate_xpath_regex('//button', '@class', 'btn-submit-*')
print("扩展语法:", paths['regexp_syntax'])
print("备用方案:", paths['contains_fallback'])
下一篇:怎么查看java在哪
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8