无法从隐藏的下拉列表中 select 元素 - Python Selenium

Unable to select element from Dropdown that hides - Python Selenium

我正在尝试进入搜索结果页面,但我必须先单击下拉选项才能完成搜索。当我手动执行此操作时,如果我没有在出现时正确单击它,下拉菜单就会隐藏,当我对其进行编码时,我会收到以下错误:

ElementNotInteractableException: Message: Element <div id="_esgratingsprofile_autocomplete-results-container" class="autocomplete-results-container msci-ac-search-data-dropdown"> could not be scrolled into view

这是我目前为止的代码,您可以访问url,看看自己的情况如何:

from selenium.webdriver import Firefox
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = Firefox(options=opts)
browser.get('https://www.msci.com/esg-ratings')
search_form = browser.find_element_by_id('_esgratingsprofile_keywords')
search_form.send_keys('MSFT')
browser.find_element_by_xpath("//div[@id='_esgratingsprofile_autocomplete-results-container']/ul[@id='ui-id-1']/li[@class='msci-ac-search-section-title ui-menu-item']").click()

我查看了许多其他答案,但他们似乎没有处理下拉列表不是可直接单击的元素或如果您没有正确单击它 隐藏 的情况离开。感谢任何帮助。

试试下面的代码,这段代码对我有用。如果它显示任何错误,请告诉我。

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

driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)

driver.get("https://www.msci.com/esg-ratings")

Drop_Down = driver.find_element_by_xpath('//*[@id="_esgratingsprofile_keywords"]')
Drop_Down.send_keys("MSFT")

# Select the First Result from the search.
Result = wait.until(
    EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'autocomplete-results-container')]/ul/li[1]")))
action.move_to_element(Result).click().perform()