How to select dropDown options when is not with <Select> tag with Selenium in Python

Viewed 85

Would like to select the date in the following URL (Yellow highlighted):

https://www.hkex.com.hk/mutual-market/stock-connect/statistics/historical-daily?sc_lang=en-hk#select4=3&select5=1&select3=0&select1=1&select2=6

enter image description here

And when inspecting the HTML code, I couldn't find any "select" tag as below: enter image description here

My code:

chrome_options  =   Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("window-size=1920,1080")
driver=webdriver.Chrome(options=chrome_options, executable_path=r'/Users/Woodylin/Desktop/Python Learnings/chromedriver')
#driver=webdriver.Chrome(options=chrome_options, executable_path=r'C:/Users/wolin/Desktop/Python/chromedriver.exe')

url1 = 'https://www.hkex.com.hk/Mutual-Market/Stock-Connect/Statistics/Historical-Daily?sc_lang=zh-HK#select4=2&select5=1&select3=0&select1=1&select2=6' #滬港通 

driver.get(url1)
sleep(3)
ele_date          =    driver.find_element_by_id('select-target-date') #Target Date
DropDown1         =   Select(ele_date)
DropDown1.select_by_visible_text('7')

So how could I set the select the option?

Current Update:

Found that if I use the following XPATH will select "5th" day

driver.find_element_by_xpath("//*[@id='mainform']/div[8]/main/section/div[2]/div[1]/div[2]/div[**5**]/a").click()

#However, if I use the command below to select "31st" day, it returns below error:

Err msg: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

driver.find_element_by_xpath("//*[@id='mainform']/div[8]/main/section/div[2]/div[1]/div[2]/div[**31**]/a").click()
1 Answers

Select Docs:

Constructor. A check is made that the given element is, indeed, a SELECT tag.......

Your target element isn't SELECT tag. So you can't use Select

Try another approach with css selector. You need click first the date element to make dropdown bring up the date list, and before click element target, use location_once_scrolled_into_view first.

driver.find_element_by_css_selector('div.csm-dailystat-day-selection').click()
target_date = '7'
ele_date = driver.find_element_by_css_selector('div.csm-dailystat-day-selection div[data-value="{}"]'.format(target_date))
ele_date.location_once_scrolled_into_view
ele_date.click()
Related