How to find chart for given symbol and date and screenshot it using python Selenium?

Question:

I am upgrading discord crypto bot and I need chart as an image to be able to be sent on discord. So when user type command (!info btc 7) (bitcoin 7 days chart) it should give him bitcoin chart for the past 7 days, and you can get it either on 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 and then I should screenshot that chart and send it to discord as image. In try block program can’t find tag name ‘text’ even though it exists, and it just print ‘Not found’. Any suggestion?

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()
Asked By: Ged0jzn4

||

Answers:

First you need to click the svg element with text and induce waiting for the element. Send keys to the input tag. Find the svg tag and implement a screenshot function.

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')

Import:

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
Answered By: Arundeep Chohan