无法使用Selenium Python选择下拉列表值

时间:2019-10-16 06:08:19

标签: python selenium selenium-webdriver

我试图通过“选择”选择下拉列表值之一,但我无法识别该元素。

这是HTML代码:

<input class="select-dropdown dropdown-trigger" type="text" readonly="true" data-target="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b4" style="" xpath="1">
<ul id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b4" class="dropdown-content select-dropdown" tabindex="0" style="" xpath="1"><li class="disabled" id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b40" tabindex="0"><span>Choose</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b41" tabindex="0"><span>Marriage</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b42" tabindex="0" class="selected"><span>Home Renovation</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b43" tabindex="0"><span>Holiday Planning</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b44" tabindex="0"><span>Debt Refinancing</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b45" tabindex="0"><span>Medical Emergency</span></li><li id="select-options-6a055dc1-379b-0ad4-c88a-bfd07a8356b46" tabindex="0"><span>Others</span></li></ul>
<svg class="caret" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" xpath="1"><path d="M7 10l5 5 5-5z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg>
<select class="" type="select" id="7" options="[object Object]" name="loan_purpose" tabindex="-1" xpath="1"><option value="" disabled="" selected="">Choose</option><option value="1">Marriage</option><option value="2">Home Renovation</option><option value="3">Holiday Planning</option><option value="4">Debt Refinancing</option><option value="5">Medical Emergency</option><option value="6">Others</option></select>

我可以使用以下代码单击下拉列表:

WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='select-dropdown dropdown-trigger']"))).click()

self.driver.find_element_by_xpath("//select[@name='loan_purpose']/option[text()='Home Renovation']").click()

当我尝试使用此代码选择值时,收到错误消息:

unable to select the value in the dropdown list,getting the following error message:
ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated (Session info: chrome=77.0.3865.90)

1 个答案:

答案 0 :(得分:0)

这些选项在下拉菜单中不可见,因此您无法直接单击它们。由于下拉列表是<select>元素,因此您可以使用Select

dropdown = self.driver.find_element_by_name('loan_purpose')
select = Select(dropdown)
select.select_by_visible_text('Home Renovation')