【问题标题】:How to find combobox and set value using Selenium and find_element_by_xpath如何使用 Selenium 和 find_element_by_xpath 查找组合框并设置值
【发布时间】:2026-01-24 20:45:01
【问题描述】:

我正在尝试使用 selenium 在组合框中设置一个值。但是 find_element_by_xpath 语句无法按类或 ng-model 找到组合框 (具体来说,我正在尝试将股票的时间段从一天更改为一周)

网页包含我正在搜索的元素的以下 javascript

**<select ng-model="analysisGraph.periodselector.period" class="ng-valid ng-dirty ng-valid-parse user-success ng-touched">**
    <!-- ngIf: !analysisGraph.periodselector.dayOnly -->
    <option ng-if="!analysisGraph.periodselector.dayOnly" value="INTRADAY" class="ng-binding ng-scope">Intradag</option><!-- end ngIf: !analysisGraph.periodselector.dayOnly --><!-- ngIf: !analysisGraph.periodselector.dayOnly -->
    <option ng-if="!analysisGraph.periodselector.dayOnly" value="ONE_WEEK" class="ng-binding ng-scope">1 uke</option><!-- end ngIf: !analysisGraph.periodselector.dayOnly -->
        <option value="ONE_MONTH" class="ng-binding">1 mnd</option><!-- ngIf: !analysisGraph.periodselector.hideOption.hide3m -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide3m" value="THREE_MONTHS" class="ng-binding ng-scope">3 mnd</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide3m --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide6m -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide6m" value="SIX_MONTHS" class="ng-binding ng-scope">6 mnd</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide6m -->
        <option value="YTD" class="ng-binding">YTD</option><!-- ngIf: !analysisGraph.periodselector.hideOption.hide1y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide1y" value="ONE_YEAR" class="ng-binding ng-scope">1 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide1y --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide3y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide3y" value="THREE_YEARS" class="ng-binding ng-scope">3 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide3y --><!-- ngIf: !analysisGraph.periodselector.hideOption.hide5y -->
    <option ng-if="!analysisGraph.periodselector.hideOption.hide5y" value="FIVE_YEARS" class="ng-binding ng-scope">5 år</option><!-- end ngIf: !analysisGraph.periodselector.hideOption.hide5y --><!-- ngIf: hasLaunchData -->
</select>

我已经尝试了以下代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import pymysql
import ose_data

chrome_options = Options()
driver = webdriver.Chrome(executable_path='chromedriver', options=chrome_options)
driver.get('https://www.oslobors.no/markedsaktivitet/#/details/ADE.OSE/overview')
time.sleep(5)
bs = BeautifulSoup(driver.page_source, 'html.parser')
**driver.find_element_by_xpath("//select[@class='ng-valid ng-dirty ng-valid-parse user-success ng-touched']/option[text()='ONE_WEEK']").click()**

但它失败并出现以下错误:

错误:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//select[@class='ng -valid ng-dirty ng-valid-parse user-success ng-touched']/option[text()='ONE_WEEK']"} (会话信息:chrome=79.0.3945.130)

我也尝试过搜索 driver.find_element_by_xpath("//select[@ng-model='analysisGraph.periodselector.period']/option[text()='ONE_WEEK']").click()

有同样的错误,任何人都从一个新手 Python 程序员那里看到任何明显的错误? :)

【问题讨论】:

    标签: javascript python-3.x selenium-webdriver


    【解决方案1】:

    Xpath 错误,使用select[ng-model="analysisGraph.periodselector.period"] css 选择器获取元素。该元素是带有select tag 的下拉菜单,要选择它,请使用Select 类。添加了WebDriverWait 以等待元素可点击后再与之交互。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import time
    from bs4 import BeautifulSoup
    import pymysql
    import ose_data
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.select import Select
    
    chrome_options = Options()
    driver = webdriver.Chrome(executable_path='chromedriver', options=chrome_options)
    driver.get('https://www.oslobors.no/markedsaktivitet/#/details/ADE.OSE/overview')
    wait = WebDriverWait(driver, 10)
    
    # bs = BeautifulSoup(driver.page_source, 'html.parser')
    details_period = Select(wait.until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'select[ng-model="analysisGraph.periodselector.period"]'))))
    details_period.select_by_value('ONE_WEEK')
    
    # You can also use
    # details_period.select_by_visible_text()
    # details_period.select_by_index()
    

    【讨论】: