python - 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>
解决方案
推荐阅读
- python - 如何优化 3 个单独数组上的三重嵌套 for 循环(Python)
- javascript - 加载页面后,如何在我的 c# 程序中恢复通过 cefSharp 调用的 javascript 函数发送的列表?
- javascript - 如何将 props.theme.breakpoints 与 makeStyles 挂钩使用?
- latex - APA6 一位作者乳胶引用不起作用
- swiftui - 如何增加 SwiftUI 选择器中显示的最大行数?
- optimization - Adobe 分析与 adobe 目标的集成
- python - 尝试通过列表传递for循环时出现索引超出范围错误
- rxjs - rxjs:我可以在定义 observable 之前订阅吗?
- typescript - 定义和使用固定数组类型
- javascript - 减少循环或代码可读性问题的数量