消息:尝试通过 Selenium 单击下拉菜单中的选项时,无法将元素 <option> 滚动到视图中

新手上路,请多包涵

我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python。

这是我的问题:当我尝试选择一个选项时,出现以下错误:

 selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.

我尝试了各种方法来解决这个问题,但似乎都没有用。以下是我尝试过的一些方法。

 mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
dropDownMenu.select_by_visible_text('Professional')

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDown = Select(mySelectElement)
for option in dropDown.options:
    message = option.get_attribute('innerText')
    print(message)
    if message == 'Professional':
        print("Exists")
        dropDown.select_by_visible_text(message)
        break

element = browser.find_element_by_id('providerTypeDropDown')
browser.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Professional")

HTML 代码遵循通常的选择标签和选项标签。任何帮助表示赞赏。 HTML 代码包含在下面。

 <select data-av-chosen="providerTypes" id="providerTypeDropDown" data-placeholder="Please Select a Provider Type" name="providerTypeDropDown"
class="chzn-select input-full ng-pristine chzn-done ng-invalid ng-invalid-provider-type" data-ng-options="providerType.value for providerType in request.models.providerTypes"
data-ng-model="request.models.providerType" data-av-validator-field="providerType" data-disable-search-threshold="5" style="display; none;">
    <option value="" class="">Please Select a Provider Type</option>
    <option value="0">Professional</option>
    <option value="1">Institutional</option>
</select>

打印语句用于测试/代码跟踪目的。

原文由 S.Srikanth 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 982
2 个回答

这个错误信息…

 selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.

…意味着您的程序试图与之交互的 <option> 项目无法滚动到视图中。

所需元素的 HTML 可以让我们了解错误背后的原因。然而,所需的元素似乎不在 clickable / 在 Viewport 中。要解决此问题,您必须引入 WebDriverWait 以使 _元素可点击_,您可以使用以下解决方案:

 mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='providerTypeDropDown']//options[contains(.,'Professional')]")))
dropDownMenu.select_by_visible_text('Professional')

注意:您必须添加以下导入:

 from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

尝试添加等待:

 mySelectElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "providerTypeDropDown")))
mySelectElement.click()

它会等待至少 10 秒,直到元素可点击,然后点击。

此外,我在您的代码中没有看到您单击打开下拉菜单的下拉按钮。找到此按钮,还要等待并在选择该选项之前单击它。希望能帮助到你。

注意:对于此代码,您必须添加一些导入:

 from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

原文由 Andrei Suvorkov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题