2

Trying to select other financial statements (default is income statement), as well as toggle between the second list of Annual/Quarterly. I can narrow down to get the attributes of the lists but I'm not able to interact with the list.

import time
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver

symbol = 'bmo'
driver = webdriver.Chrome()
driver.get('https://web.tmxmoney.com/financials.php?qm_symbol={}'.format(symbol))
time.sleep(2)
dropdown = driver.find_elements_by_css_selector('a.qmod-dropdown_toggle.qmod-type-toggle + ul.qmod-dropdown-menu > li > a')

for option in dropdown:
    if option.get_attribute('innerText') == 'Balance Sheet':
        option.send_keys('Balance Sheet')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
z.Rick
  • 23
  • 2

1 Answers1

0

To select the option with text as Balance Sheet you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

  • xpath:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get('https://web.tmxmoney.com/financials.php?qm_symbol=bmo')
    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h4[text()='Financials for Bank of Montreal']")))
    driver.execute_script("window.scrollBy(0,600)")
    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='qmod-dropdown_toggle qmod-type-toggle']/span[text()='Income Statement']")))).perform()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='qmod-dropdown-menu']//li/a[text()='Balance Sheet']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352