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

如何获取select元素中各option的value值?目标元素仅含自定义属性

Looks like you're trying to pull the value attributes from options in a select that only has an ng-model attribute to identify it—no problem, we can use attribute selectors to target that select element easily. Here are solutions for common tools you might be using:

How to Extract Option Values from a Select with ng-model Attribute

1. Quick Test in Browser Console (Plain JavaScript)

If you just need to grab the values directly from your browser's dev tools:

// Target the select using its ng-model attribute
const selectElement = document.querySelector('select[ng-model="currentTab"]');
// Convert options to an array and extract their value attributes
const optionValues = Array.from(selectElement.options).map(opt => opt.value);
// Output as space-separated string (matches your desired result)
console.log(optionValues.join(' ')); // Prints: "0 1"

2. Selenium WebDriver (Python Example)

If you're using Selenium for web automation/scraping:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("your-target-page-url")

# Locate the select element via ng-model attribute selector
select_element = driver.find_element(By.CSS_SELECTOR, 'select[ng-model="currentTab"]')
# Get all child option elements
options = select_element.find_elements(By.TAG_NAME, 'option')
# Extract and format the value attributes
values = [opt.get_attribute('value') for opt in options]
print(' '.join(values)) # Outputs: "0 1"

driver.quit()

3. Cypress (End-to-End Testing)

For Cypress users looking to extract values during tests:

cy.get('select[ng-model="currentTab"]')
  .find('option')
  .then(($options) => {
    // Collect all value attributes into an array
    const values = $options.map((index, opt) => opt.value).get();
    cy.log(values.join(' ')); // Logs: "0 1"
  });

Key Tips:

  • The attribute selector select[ng-model="currentTab"] is the core here—make sure the ng-model value (currentTab) matches exactly what's in your HTML (it's case-sensitive!).
  • If multiple elements have the same ng-model (unlikely in this case), you can narrow it down by adding parent context, like .select-wrap select[ng-model="currentTab"].

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:32:08