简体   繁体   中英

Python selenium select invisible drop-down when style =“display: none;"

I'm trying to select and click in an invisible dropdown menu using selenium webdriver.

HTML:


<div id = "ID_1" class="mb-outer-container" style="display: none;">
.....
     <select style="font-size:10px", onchange="dg_send('contractNonParticipationsDatagrid2080-form');>
        <option selected="selected" value="10">10</option>
        <option selected="selected" value="20">20</option>
        <option selected="selected" value="30">30</option>

What I've been trying to do:

sel = Select(browser.find_element_by_xpath("//select[@style='font-size:10px']"))
sel.select_by_visible_text("20")

also tried to select by value:

sel.select_by_value("20")

Both lead to the same error: ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated

It seems like I'm able to find the elemtent, but unable to select the and click the option in the dropdown

You first need to display the dropdown to select the option.

driver.find_element(By.XPATH, '//select[@style="font-size:10px"]').click()

This will click on the dropdown and show the options of the dropdown. Then you can click on the option.

driver.find_element(By.XPATH, '//option[@value="20"]').click()

Also need to import this

from selenium.webdriver.common.by import By

The driver.find_element_by_xpath() is deprecated in newest version.

What I like to use (I use C#, hopefully I've translated well enough to python) is the driver's javascript execute script functionality. Here's how I normally get around not interactable errors.

driver.execute_script("arguments[0].click()", driver.find_element(By.XPATH, "xpath"));

This will use javascript to intereact with the element instead of just trying to click on it. Hope this helps:)

Found a solution:

wait = WebDriverWait(browser, timeout=10, poll_frequency=2,
                                         ignored_exceptions=[NoSuchElementException,
                                         ElementNotVisibleException,
                                         ElementNotSelectableException])
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="contractNonParticipationsDatagrid2080-form"]/table[1]/tbody/tr/td[1]/span/select')))
sel = Select(element)
sel.select_by_index(1)

Thank you all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM