You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python的Selenium中批量获取标签名含button的元素

问题:Selenium无法获取标签名含"button"的自定义元素

需求

用Python的Selenium获取所有**标签名包含"button"**的元素(例如icon-button这类自定义标签),但使用contains的XPATH查询始终返回0个元素,需要修正方法。

尝试过的代码

以下代码执行后结果均为长度0:

# 错误写法:查找form元素的属性而非元素本身
buttons = driver.find_elements(By.XPATH,"//form/@*[contains(name(), 'button')]")
print(len(buttons))

# 尝试过的XPATH写法,均返回0
buttons = driver.find_elements(By.XPATH,".//*[contains(name(),'button')]")
buttons = driver.find_elements(By.XPATH, ".//*[contains(name(), 'button')]")
buttons = driver.find_elements(By.XPATH, "//tag[contains(@tag='button')]") # 语法错误

示例HTML

目标元素示例:

<icon-button data-location="item_details:item_info:comment_icon_button" location-2="comment_icon_button" variant="with-text" label="コメント" defined="" data-js-focus-visible="">
    <icon-comment-square-outline color="primary" defined=""></icon-comment-square-outline>
</icon-button>

最新测试代码及结果

driver.get(url)
sleep(10)
# 普通button标签能找到6个
buttons = driver.find_elements(By.TAG_NAME, "button")
print(location(), len(buttons)) # 输出:6

# 标签名含button的元素返回0
buttons = driver.find_elements(By.XPATH,"//*[contains(name(), 'button')]")
print(location(), len(buttons)) # 输出:0

# 通过label属性查找也返回0
button = driver.find_element(By.CSS_SELECTOR, "[label='コメント']")
print(location(), len(buttons)) # 输出:0

button = driver.find_element(By.CSS_SELECTOR, "[text='コメント']")
print(location(), len(buttons)) # 输出:0

解决方案及修正点

1. 修正XPATH写法(区分大小写)

部分浏览器/环境中,name()返回的标签名是大写(如ICON-BUTTON),需统一转小写后匹配:

buttons = driver.find_elements(By.XPATH, "//*[contains(lower-case(name()), 'button')]")
print(len(buttons))

2. 改用JavaScript查询(更可靠)

直接通过JS遍历所有元素,检查标签名是否包含"button",避免XPATH的兼容性问题:

buttons = driver.execute_script("""
return Array.from(document.querySelectorAll('*')).filter(el => el.tagName.toLowerCase().includes('button'));
""")
print(len(buttons))

3. 检查元素是否在iframe/Shadow DOM中

  • 如果在iframe内:需先切换到iframe再查找:
# 定位并切换到目标iframe
iframe = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(iframe)
# 执行查找操作
buttons = driver.find_elements(By.XPATH, "//*[contains(lower-case(name()), 'button')]")
print(len(buttons))
# 切回主文档
driver.switch_to.default_content()
  • 如果是Shadow DOM元素:自定义元素(如icon-button)可能属于Shadow DOM,需获取shadow root后再查询:
# 先获取自定义元素
icon_button = driver.find_element(By.TAG_NAME, "icon-button")
# 获取shadow root
shadow_root = driver.execute_script("return arguments[0].shadowRoot", icon_button)
# 在shadow root内查找元素(如果需要找内部元素)
inner_elements = shadow_root.find_elements(By.XPATH, ".//*[contains(lower-case(name()), 'button')]")

4. 替换sleep为显式等待

sleep(10)不一定能确保自定义元素加载完成,改用显式等待更可靠:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get(url)
# 等待20秒,直到至少一个目标元素出现
wait = WebDriverWait(driver, 20)
buttons = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[contains(lower-case(name()), 'button')]")))
print(len(buttons))

内容的提问来源于stack exchange,提问作者dominic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 23:27:38