【问题标题】:Can not select drop down menu using python Selenium无法使用 python Selenium 选择下拉菜单
【发布时间】:2023-04-01 02:13:01
【问题描述】:

您好,我正在使用 selenium,并已成功使用 id historicalDatePicker 设置文本字段,但无法从下拉菜单中选择选项<div class="dropdown historical__month"><div class="dropdown historical__year"> 我很困惑,因为没有 id 并且我不擅长使用 xpath。这是HTML代码

<div class="section section--green">
    <div class="forms">
        <div class="form">
            <div class="form__fields form__fields--widthAuto">
                <div class="form__field">
                    <div class="form__field__label">SEARCH BY DATE</div>
                    <div class="form__field__text">
                        <input type="text" name="date" autocomplete="new-password" id="historicalDatePicker" data-latest="1577448902000">
                    </div>
                </div>
                <div class="form__field">
                    <button class="form__button" id="historicalSearchBtn">SEARCH</button>
                </div>
                <div class="form__field"></div>
                <div class="form__field"></div>
            </div>
        </div>
        <div class="form__ruler form__ruler--vertical"></div>
        <div class="form">
            <div class="form__fields form__fields--widthAuto">
                <div class="form__field">
                    <div class="form__field__label">SEARCH BY SYMBOL</div>
                    <div class="historical__search autocomplete">
                        <input id="historicalSymbolSearch" autocomplete="new-password" type="text" placeholder="Search for symbols or company"><i class="icon-search"></i></div>
                </div>
                <div class="form__field">
                    <div class="form__field__label"> </div>
                    <div class="dropdown historical__month">
                        <select class="dropdown__select" name="sector">
                            <option value="">Month</option>
                            <option value="1">January</option>
                            <option value="2">February</option>
                            <option value="3">March</option>
                            <option value="4">April</option>
                            <option value="5">May</option>
                            <option value="6">June</option>
                            <option value="7">July</option>
                            <option value="8">August</option>
                            <option value="9">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>
                    </div>
                </div>
                <div class="form__field">
                    <div class="form__field__label"> </div>
                    <div class="dropdown historical__year">
                        <select class="dropdown__select" name="sector">
                            <option value="">Year</option>
                            <option value="2019">2019</option>
                            <option value="2018">2018</option>
                            <option value="2017">2017</option>
                            <option value="2016">2016</option>
                            <option value="2015">2015</option>
                        </select>
                    </div>
                </div>
                <div class="form__field">
                    <button class="form__button" id="historicalSymbolBtn">SEARCH</button>
                </div>
            </div>
        </div>
        <div class="form__ruler form__ruler--vertical"></div>
        <div class="form">
            <div class="form__fields">
                <div class="form__field">
                    <div class="form__field__label"> </div><span> To download daily market summary, visit <br>  <a href="/daily-downloads" target="blank"><strong>Daily Downloads</strong></a> page.</span></div>
            </div>
        </div>
    </div>
</div>

我的python代码是

from selenium import webdriver
from selenium.webdriver.support.ui import Select
chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://dps.psx.com.pk/historical")


textField = driver.find_element_by_id("historicalSymbolSearch")
textField.send_keys('DWAE')

select = Select(driver.find_element_by_xpath("//select[@name='sector']"))
# all_options = select.find_elements_by_tag_name("option")

有什么帮助吗?

【问题讨论】:

    标签:
    python
    forms
    selenium
    web-scraping
    drop-down-menu