如何使用硒从非选择下拉菜单中单击项目

问题描述 投票:1回答:1

[尝试选择其他财务报表(默认为损益表),以及在第二个年度/季度列表之间切换。我可以缩小范围以获取列表的属性,但无法与列表进行交互。

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')
python selenium-webdriver iframe drop-down-menu webdriverwait
1个回答
0
投票

要选择文本为资产负债表选项,必须为element_to_be_clickable()引入WebDriverWait,并且可以使用以下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()
    
© www.soinside.com 2019 - 2024. All rights reserved.