首页 > 解决方案 > 在 Python Selenium 中选择具有不同 ID 的多个下拉菜单

问题描述

我正在尝试填写一个表格,其中每个订单号都有一个下拉菜单。

<select name="order(889673519).box(1).shippingmethod" onclick="" onchange="" 
id="order(889673519).box(1).shippingmethod"><option value="" 
id="order(889673519).box(1).shippingmethod.blank"></option>

对于每个下拉菜单,名称 css 选择器中的数字都会改变,所以第一个是 889673519 但第二个是

<select name="order(889711159).box(1).shippingmethod" onclick="" onchange="" 
id="order(889711159).box(1).shippingmethod"><option value="" 
id="order(889711159).box(1).shippingmethod.blank"></option>

我使用什么路径来选择具有不同名称的多个元素,这样我就可以遍历它们来选择我的选项。

标签: pythonseleniumselectxpathcss-selectors

解决方案


使用contains功能:

elements = driver.find_elements_by_xpath("//select[contains(@name, 'order') and contains(@name, 'shippingmethod')]")

推荐阅读