Hi YI Wen Edwin, I am glad that I could help you with your issue. I am sharing the code snippet which would resolve your query on how to customize the filters and then download the stocks file:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("C:\\\\Users\\\\Abha_Rathour\\\\Downloads\\\\ExtractedFiles\\\\chromedriver_win32\\\\chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(2)
driver.get("https://www2.sgx.com/securities/stock-screener")
driver.find_element_by_xpath('//*[@id="beta-warning-dialog"]/div[2]/div/div/div/sgx-input-checkbox/label/span').click()
driver.find_element_by_xpath('//*[@id="beta-warning-dialog"]/div[2]/div/div/div/div[3]/button').click()
driver.find_element_by_xpath('//*[@id="gdpr-banner"]/div/button').click()
time.sleep(5)
driver.find_element_by_xpath("//button[@class='widget-stock-screener-button widget-stock-screener-filters-toggle']").click()
driver.find_element_by_xpath("//input[@name='sector']").click()
driver.find_element_by_xpath("//span[contains(text(),'Energy')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Tot. Rev ($M)')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Mkt Cap ($M)')]").click()
driver.find_element_by_xpath("//span[contains(text(),'52-wk %Pr. Chg.')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Net Profit %')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Download')]").click()
time.sleep(3)
driver.close()
Now, read the following steps to understand the code better:
1. First import the ChromeDriver and use some of its basic methods like maximize_window() or implicitly_wait(time_in_seconds) to setup the driver properties.
2. Then to close the pop-up which shows every-time you open the page, write first 3 lines of driver.find_element... code.
3. Now, use driver.find_element_by_xpath to click on Select Sector dropdown and Show More Filters. You can use this method also for selecting which Sector you want to choose. Eg. I chose Energy sector in my code.
4. Next, use same driver.find_element_by_xpath method individually to select all the Selection Criteria you wish to choose.
5. Finally write the driver method for downloading the file.
Note: I used time.sleep(time) at 2 places so that page could all the elements properly and you could see the automation process.