我正在升級不和諧加密機器人,我需要圖表作為影像才能在不和諧中發送。因此,當用戶輸入命令 (!info btc 7)(位元幣 7 天圖表)時,它應該為他提供過去 7 天的位元幣圖表,您可以在 coingecko(https://www.coingecko.com/en/硬幣/位元幣)我作業過,或交易視圖。有圖表,在右上角我應該輸入 current_date - 7d然后我應該截圖該圖表并將其作為影像發送到不和諧。
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 塊中,即使它存在,程式也找不到標簽名稱“文本”,它只是列印“未找到”。有什么建議嗎?
uj5u.com熱心網友回復:
首先,您需要單擊帶有文本的 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362316.html
