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

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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

10 Comments

Thanks! But this is throwing a TimeoutException error: raise TimeoutException(message, screen, stacktrace) TimeoutException
@BobG : Can you check whether element is inside any iframe?
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
@BobG : just curious is this main page or is it any child window on main page?
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: i.imgur.com/RVuwMwC.png
|
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
    

6 Comments

Hi, this is throwing: raise TimeoutException(message, screen, stacktrace) TimeoutException
Checkout the updated answer and let me know the status
Still raises the TimeoutException :(
Does the drop down menu and opens it up as I have used your id in the form of a css_selector and xpath
Yes it opens the drop down menu but then it doesn't seem to be able to click on or select the option within the menu. Screenshot of where the program gets to just before throwing that error: i.imgur.com/sW2KkDQ.png
|
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

4 Comments

This returns: UnexpectedTagNameException: Select only works on <select> elements, not on <div>
@RKelley change your locator to "qf_admin_practice_edit__data__status_id" and it should work - you want to target the <select> element itself.
@TodorMinakov tried changing that and it still doesn't seem to work
@TodorMinakov Thanks! I should have looked closer at the id. I took that from the code instead of the html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.