0

I'm trying to select an element from an unordered list using Selenium in Python. My current code clicks on the drop down menu and opens it up, but I'm having trouble getting it to click on an item in the list.

I'm trying to get it to click on Inactive.

Here's the HTML snippet:

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

I'm selecting/opening up the drop down menu using:

clickDropDown = driver.find_element_by_id('qf_admin_practice_edit__data__status_id__wrapper').click()
S Ahmed
  • 1,454
  • 1
  • 8
  • 14
Bob G
  • 13
  • 1
  • 6

4 Answers4

1

Figured it out but it's an ugly solution that I don't like, but it works.

        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)
Bob G
  • 13
  • 1
  • 6
0

You should be able to select the option by the text or it's value:

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

You can find more information about working with dropdowns here: https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html

RKelley
  • 1,099
  • 8
  • 14
  • This returns: UnexpectedTagNameException: Select only works on – Bob G Apr 18 '19 at 02:05
  • @RKelley change your locator to `"qf_admin_practice_edit__data__status_id"` and it should work - you want to target the ` – Todor Minakov Apr 18 '19 at 05:57
  • @TodorMinakov tried changing that and it still doesn't seem to work – Bob G Apr 18 '19 at 13:59
  • @TodorMinakov Thanks! I should have looked closer at the id. I took that from the code instead of the html. – RKelley Apr 18 '19 at 17:18
0

Your locator ID is wrong to identify the Select element.However to select an element it isn't necessary to click on the element.You can assign the select element first by locator and then use following method to access the element.

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

However it is best practice to use WebDriverWait when accessing any webpage.I have provided the code hope this helps.

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")
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Thanks! But this is throwing a TimeoutException error: `raise TimeoutException(message, screen, stacktrace) TimeoutException` – Bob G Apr 18 '19 at 13:23
  • @BobG : Can you check whether element is inside any iframe? – KunduK Apr 18 '19 at 13:53
  • It's not in an iframe. I updated the snippet of HTML code in the main post, check it out but idk if that will help at all – Bob G Apr 18 '19 at 14:00
  • @BobG : just curious is this main page or is it any child window on main page? – KunduK Apr 18 '19 at 14:10
  • I think it is a child window but I'm not entirely sure, it's a drop down located on another page that has one other drop-down menu, screenshot: https://i.imgur.com/RVuwMwC.png – Bob G Apr 18 '19 at 14:19
  • I think you need to switch child window first then my code will work.do you know how switch window right? – KunduK Apr 18 '19 at 14:24
  • no I do not know, can you explain pls? if by child window you mean it pops up its own individual window and isnt located on the main page then no, it's a main page, there's no pop-up that happens when i click the drop-down, the menu just shows as expected – Bob G Apr 18 '19 at 14:51
  • I am really confuse now.The html content which you have posted it is working with my code.However if possible can you please share url here. – KunduK Apr 18 '19 at 15:15
  • Unfortunately I can't link as it's part of a admin portal of a site – Bob G Apr 18 '19 at 15:44
  • @KunduK The ` – S Ahmed Apr 18 '19 at 19:44
0

The desired DropDown is not the <select> tag as it is having the class attribute as selectric-hide-select. To click() on the element with text as Active from the DropDown using Selenium through Python you need to induce WebDriverWait and you can use either of the following Locator Strategies:

  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352