使用Python + Selenium从列表的下拉菜单中选择一个项目

时间:2019-04-17 23:48:59

标签: python selenium xpath css-selectors webdriverwait

我正在尝试在Python中使用Selenium从无序列表中选择一个元素。我当前的代码单击下拉菜单并打开它,但是我无法让它单击列表中的项目。

我试图让它单击“非活动”。

这是HTML代码段:

<form method="post" action="/user/admin/user/practice/edit/18" id="qf_admin_practice_edit" class="qf-form"
      onsubmit="return validate(this)">
    <fieldset id="qf_admin_practice_edit__data" class="qf-fieldset">
        <legend>Edit Practice</legend>
        <div class="qf-select-wrapper" id="qf_admin_practice_edit__data__status_id__wrapper">
        <span class="qf-label-span" id="qf_admin_practice_edit__data__status_id__label_span">
            <label id="qf_admin_practice_edit__data__status_id__label" for="qf_admin_practice_edit__data__status_id">Practice Status</label>
        </span>
            <span class="qf-select-span" id="qf_admin_practice_edit__data__status_id__span">
            <span class="qf-select-inner" id="qf_admin_practice_edit__data__status_id__inner">
                <div class="selectric-wrapper selectric-qf-select selectric-above selectric-open">
                    <div class="selectric-hide-select">
                <select title="Practice Status" name="admin_practice_edit__data__status_id"
                        id="qf_admin_practice_edit__data__status_id" class="qf-select" tabindex="-1">
                    <option value="1" class="qf-option">Active</option>
                    <option value="2" class="qf-option">Inactive</option>
                    <option value="3" class="qf-option">Pending</option>
                    <option value="4" class="qf-option">Billing Suspension</option>
                    <option value="5" class="qf-option">Activity Suspension</option>
                    <option value="6" class="qf-option">Declined</option>
                    <option value="7" selected="selected" class="qf-option">Deleted</option>
                    <option value="8" class="qf-option">Cancelled</option>
                    <option value="9" class="qf-option">Reschedule</option>
                    <option value="10" class="qf-option">Expired</option>
                    <option value="11" class="qf-option">New</option>
                </select>
                    </div>
                    <div class="selectric">
                    <span class="label">Deleted</span>
                        <b class="button">▾</b>
                </div>
                    <div class="selectric-items" tabindex="-1" style="width: 718px; height: 300px;">
                        <div class="selectric-scroll">
                            <ul>
                                <li data-index="0" class="qf-option">Active</li>
                                <li data-index="1" class="qf-option">Inactive</li>
                                <li data-index="2" class="qf-option">Pending</li>
                                <li data-index="3" class="qf-option">Billing Suspension</li>
                                <li data-index="4" class="qf-option">Activity Suspension</li>
                                <li data-index="5" class="qf-option">Declined</li>
                                <li data-index="6" class="qf-option selected highlighted">Deleted</li>
                                <li data-index="7" class="qf-option">Cancelled</li>
                                <li data-index="8" class="qf-option">Reschedule</li>
                                <li data-index="9" class="qf-option">Expired</li>
                                <li data-index="10" class="qf-option last">New</li>
                            </ul>
                        </div>
                    </div>
                    <input class="selectric-input" tabindex="0">
                </div>
        </span>
      </span>
        </div>
    </fieldset>
    <div class="qf-button-wrapper" id="qf_admin_practice_edit__submit__wrapper">
        <button type="submit" value="Submit" id="qf_admin_practice_edit__submit" class="qf-button">
            <span>Submit</span>
        </button>
    </div>
    <div>
        <input value="2449978437" type="hidden" name="qf" class="qf-hidden-input qf-input">
    </div>
</form>

我正在使用以下方法选择/打开下拉菜单:

clickDropDown = driver.find_element_by_id('qf_admin_practice_edit__data__status_id__wrapper').click()

4 个答案:

答案 0 :(得分:0)

您应该能够通过文本或它的值来选择选项:

dropDown = Select(driver.find_element_by_id("admin_practice_edit__data__status_id"))
dropDown.click()
# Select by text
dropDown.select_by_visible_text("Inactive")
# or by value
dropDown.select_by_value('2')

您可以在此处找到有关使用下拉菜单的更多信息:https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html

答案 1 :(得分:0)

您的定位器ID不能识别Select元素。但是,选择元素并不需要单击该元素。您可以先通过定位器分配选择元素,然后使用以下访问元素的方法。

element.select_by_visible_text("text")
element.select_by_index(index number)
element.select_by_value("option value")

但是,最好的做法是在访问任何网页时使用WebDriverWait。我已提供了代码,希望对您有所帮助。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
from selenium.webdriver.support.select import Select

driver=webdriver.Chrome()
driver.get("url here")
dropdownelement=WebDriverWait(driver,20).until(expected_conditions.element_to_be_clickable((By.ID,'qf_admin_practice_edit__data__status_id')))
select=Select(dropdownelement)
select.select_by_visible_text("Inactive")

OR

select.select_by_index(1)

OR

select.select_by_value("2")

答案 2 :(得分:0)

所需的 DropDown 不是<select>标记,因为它具有class 属性 selectric-hide-select < / strong>。要使用Selenium通过 Python DropDown 中使用文本为 Active 的元素click(),您需要诱导< em> WebDriverWait ,您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    driver.find_element_by_css_selector("div.qf-select-wrapper#qf_admin_practice_edit__data__status_id__wrapper").click()
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.selectric-items>div.selectric-scroll li.qf-option[data-index='0']"))).click()
    
  • 使用XPATH

    driver.find_element_by_xpath("//div[@class='qf-select-wrapper' and @id='qf_admin_practice_edit__data__status_id__wrapper']").click()
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='selectric-items']/div[@class='selectric-scroll']//li[@class='qf-option' and text()='Active']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

答案 3 :(得分:0)

弄明白了,但这是我不喜欢的丑陋解决方案,但是它可以工作。

        openDropDown = driver.find_element_by_id('qf_admin_practice_edit__data__status_id__wrapper').click()
        swapToActive = driver.switch_to.active_element
        swapToActive.send_keys(Keys.UP)
        swapToActive.send_keys(Keys.UP)
        swapToActive.send_keys(Keys.UP)
        swapToActive.send_keys(Keys.UP)
        swapToActive.send_keys(Keys.UP)
        swapToActive.send_keys(Keys.RETURN)