首页 > 解决方案 > Python Selenium click()不适用于某些按钮

问题描述

我的目标是自动从 Fidelity 下载 1 分钟刻度数据 (csv)。

问题是 webdriver 没有打开显示不同间隔选项的下拉菜单。

我确定没有单击按钮的方法是,如下所述,python 代码仅打印“ElementNotInteractableException”语句,并且在运行代码时我没有看到打开的下拉菜单。

到目前为止,我尝试的是下拉按钮元素的 xpath 和选择器路径,标题为“DAILY”。我还尝试在 HTML 代码中使用下拉按钮周围的元素,例如“每日”旁边的小三角形。我也尝试过使用 Select 但由于元素是 div 而出现错误。

也许相关的是,我也无法单击包含间隔下拉列表的容器中的其他按钮,例如更改显示价格范围的按钮。而且我设法在屏幕上打开了其他下拉菜单,即允许下载 csv 的下拉菜单。最后,“不可点击”按钮位于屏幕底部附近。

欢迎所有建议。如果要包含代码详细信息或 html 片段,请告诉我。

网站(需要访问的富达账户): https ://screener.fidelity.com/ftgw/etf/gotoCL/snapshot/advancedChart.jhtml?symbols=KO

蟒蛇代码:

from selenium import webdriver as webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By as By
import selenium.common.exceptions as exceptions
from selenium.webdriver.support import (
    expected_conditions as EC)
import time

class Scraper:

    def __init__(self):

        self.driver = webdriver.Safari()

    def scrapes(self):

        try:

            self.signIn()
            time.sleep(3)
            self.getCSV('KO')

        except exceptions.TimeoutException:

            print('TimeoutException')

        except exceptions.ElementNotInteractableException:

            print('ElementNotInteractableException')

        self.driver.close()

    def signIn(self):

        driver = self.driver
        driver.get('https://www.fidelity.com/')

        #enter username
        ecTuple = (By.CSS_SELECTOR, 'input#userId-input')
        elPresent = EC.element_to_be_clickable(ecTuple)
        WebDriverWait(driver, 30).until(elPresent).send_keys(
            "USERNAME")

        #enter password
        driver.find_element_by_css_selector(
            'input#password').send_keys("PASSWORD")

        #click login button
        driver.find_element_by_css_selector(
            'button#fs-login-button').click()

    def getCSV(self, tickerStr):

        driver = self.driver
        stockURL = (
            'https://screener.fidelity.com/' +
            'ftgw/etf/gotoCL/snapshot/advancedChart' +
            '.jhtml?symbols=' + tickerStr
            )
        driver.get(stockURL)

        # open intervals dropdown
        drpDnXP = '//*[@id="selected_frequency"]'
        ecTuple = (By.XPATH, drpDnXP)
        elPresent = EC.element_to_be_clickable(ecTuple)
        WebDriverWait(driver, 30).until(elPresent).click()

        # choose one minute intervals
        minute1XP = '//*[@id="frequency_0"]'
        driver.find_element_by_xpath(minute1XP).click()

        print(1)

Scraper().scrapes()

有问题的下拉按钮元素的 html:

<div id="selected_frequency" class="ng-binding ng-scope">DAILY</div>

一些周围的html代码:

<div class="selected-item-container">
        <div class="selected-item ng-binding">
                <div id="selected_frequency" class="ng-binding ng-scope">DAILY</div>
        </div>
        <span class="triangle"></span>
</div>

标签: pythonseleniumselenium-webdriver

解决方案


推荐阅读