【问题标题】:Select multiple drop down menus with different id's in Python Selenium在 Python Selenium 中选择具有不同 ID 的多个下拉菜单
【发布时间】:2020-06-05 07:02:06
【问题描述】:

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

<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>

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

【问题讨论】:

    标签: python selenium select xpath css-selectors


    【解决方案1】:

    使用contains函数:

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

    【讨论】:

      【解决方案2】:

      使用以下Locator Strategies 识别&lt;select&gt; 节点:

      • 使用css_selectorid 属性:

        elems = driver.find_elements_by_css_selector("select[id^='order'][id*='box'][id$='shippingmethod']")
        
      • 使用css_selectorname 属性:

        elems = driver.find_elements_by_css_selector("select[name^='order'][name*='box'][name$='shippingmethod']")
        
      • 使用xpathname / id 属性:

        elems = driver.find_elements_by_xpath("//select[starts-with(@name, 'order') and contains(@id, 'shippingmethod')]")
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 2022-11-11
        • 2018-02-17
        • 2019-02-04
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2023-01-19
        相关资源
        最近更新 更多