Created
September 22, 2021 15:21
-
-
Save marcosan93/f1d14abe161d6ad620cb78daf38f8e00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Initiating options | |
| options = Options() | |
| # Performing without GUI | |
| options.headless = True | |
| options.add_argument("--window-size=1920,1200") | |
| # Accepting downloads without GUI | |
| options.add_experimental_option("prefs", { | |
| "download.default_directory": r"/Users/marcosantos/Downloads", | |
| "download.prompt_for_download": False, | |
| "download.directory_upgrade": True, | |
| "safebrowsing_for_trusted_sources_enabled": False, | |
| "safebrowsing.enabled": False | |
| }) | |
| # The browser | |
| driver = webdriver.Chrome( | |
| executable_path="/Users/marcosantos/Downloads/chromedriver", | |
| options=options | |
| ) | |
| # Designating which site to open to | |
| driver.get("https://www.google.com") | |
| print("Opened Google") | |
| # Typing into search | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, "//input[@class='gLFyf gsfi']") | |
| )).send_keys("yahoo finance", Keys.ENTER) | |
| print("Entered google search") | |
| # Clicking on the desired result | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, "//div[@class='tF2Cxc']") | |
| )).click() | |
| print("Clicked on google search result") | |
| # Searching a Ticker | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, "//input[@id='yfin-usr-qry']") | |
| )).send_keys("amc", Keys.ENTER) | |
| print("Entered ticker in yahoo finance search") | |
| # Clicking Historical Data | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, "//li[@data-test='HISTORICAL_DATA']") | |
| )).click() | |
| print("Adjusting historical date range") | |
| # Getting historical date range | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, | |
| "//*[@id='Col1-1-HistoricalDataTable-Proxy']/section/div[1]/div[1]/div[1]/div/div/div[1]/span") | |
| )).click() | |
| # Getting max historical range | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, "//*[@id='dropdown-menu']/div/ul[2]/li[4]/button") | |
| )).click() | |
| # Applying the changes | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, | |
| "//*[@id='Col1-1-HistoricalDataTable-Proxy']/section/div[1]/div[1]/button/span") | |
| )).click() | |
| # Downloading the file | |
| WebDriverWait(driver, 3).until( | |
| EC.element_to_be_clickable( | |
| (By.XPATH, | |
| "//*[@id='Col1-1-HistoricalDataTable-Proxy']/section/div[1]/div[2]/span[2]/a/span") | |
| )).click() | |
| print("Downloaded File!") | |
| # Closing the window but allowing some time for download | |
| time.sleep(1) | |
| driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment