简体   繁体   English

Python selenium select style =“display: none;”时不可见下拉

[英]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.我正在尝试 select 并使用 selenium 网络驱动程序单击一个不可见的下拉菜单。

HTML: 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:还尝试按值 select:

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两者都会导致相同的错误: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似乎我能够找到该元素,但无法找到 select 然后单击下拉列表中的选项

You first need to display the dropdown to select the option.您首先需要显示 select 选项的下拉列表。

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. driver.find_element_by_xpath() 在最新版本中已弃用。

What I like to use (I use C#, hopefully I've translated well enough to python) is the driver's javascript execute script functionality.我喜欢使用(我使用 C#,希望我已经足够好地翻译成 python)是驱动程序的 javascript 执行脚本功能。 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.这将使用 javascript 与元素交互,而不是仅仅尝试单击它。 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!谢谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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