Python Selenium 查找给定交易品种和日期的图表并截图

Python Selenium find chart for given symbol and date and screenshot it

我正在升级 discord 加密机器人,我需要图表作为图像才能在 discord 上发送。因此,当用户键入命令 (!info btc 7)(比特币 7 天图表)时,它应该给他过去 7 天的比特币图表,你可以在 coingecko 上获取它(https://www.coingecko.com/en/coins/bitcoin) which I worked on, or tradingview. There is chart and in the top right corner I should enter current_date - 7d 然后我应该截取该图表并将其作为图像发送到 discord。

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.coingecko.com/en/coins/bitcoin")
driver.maximize_window()
try:
    d = driver.find_element(By.TAG_NAME, 'text')
    # <text>value</value>  value should be set to current_date - 7 days
    # and then there will be chart that I need to screenshot and send to discord
except:
    print('Not found')
driver.get_screenshot_as_file('chart.png')
driver.close()

在 try 块程序中找不到标记名称 'text',即使它存在,它只是打印 'Not found'。有什么建议吗?

首先,您需要单击带有文本的 svg 元素并引发等待该元素。将密钥发送到输入标签。找到svg标签,实现截图功能

wait=WebDriverWait(driver, 60)
driver.get("https://www.coingecko.com/en/coins/bitcoin")
driver.maximize_window()

try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"svg > g.highcharts-range-selector-group > g > g:nth-child(2) > text"))).click()
    
    driver.find_element(By.XPATH, "(//input[@class='highcharts-range-selector'])[1]").send_keys(datetime.date.fromordinal(datetime.date.today().toordinal()-7).strftime("%F"))

    element=driver.find_element(By.CSS_SELECTOR, "svg.highcharts-root")
    element.screenshot('foo.png')
    # <text>value</value>  value should be set to current_date - 7 days
    # and then there will be chart that I need to screenshot and send to discord
except:
    print('Not found')

导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import datetime