Scroll list with keys ARROW_DOWN to a visible element (Selenium, Python)

This issue comes up often. You want to select a value in a dropdown list, but when you click to show the values only the first several are listed, others are hidden (so to speak) and need to be scrolled to. You cannot click the selection if it’s not visible. Here is a way to scroll down to the desired value.

Import the keys module

from selenium.webdriver.common.keys import Keys

Click on the dropdown to show the initial values

self.driver.find_element_by_css_selector(".list_dropdown_class").click()

Now send some ARROW_DOWN keys to the ul element in question until your desired selection is visible

self.driver.find_element_by_css_selector("ul.list-with-results").send_keys(Keys.ARROW_DOWN)
self.driver.find_element_by_css_selector("ul.list-with-results").send_keys(Keys.ARROW_DOWN)
self.driver.find_element_by_css_selector("ul.list-with-results").send_keys(Keys.ARROW_DOWN)

Now select it and click it

self.driver.find_element_by_css_selector("li#SelectedList_21").click()

This is not the most elegant solution, but it does somewhat mimic what an end user would do, scroll down to the selection.