简体   繁体   English

Select 从 HTML 导出图片 selenium python

[英]Select export picture from HTML with selenium python

I tried to export the generated chart to png file from the menu in this website .我试图从本网站的菜单中将生成的图表导出为 png 文件。 After I manage to enter a city name and Visualize Results with script, the website shows some information and chart where I can export to png, either with small or large option.在我设法输入城市名称并使用脚本可视化结果后,该网站显示了一些信息和图表,我可以在其中导出到 png,有小或大选项。 However, I could not manage to export large png file (option that I selected) of the chart because the script reach the timeout exception.但是,我无法设法导出图表的大 png 文件(我选择的选项),因为脚本到达超时异常。 The following is line that I've tried:以下是我试过的行:

elem = wait(driver, 10).until(EC.presence_of_element_located((By.ID, 'tr_exportpngl')))
elem.click()

i tried also solutions from other questions such as wait until 'lement_to_be_clickable' or finding by XPATH, still no success.我还尝试了其他问题的解决方案,例如等待“lement_to_be_clickable”或在 XPATH 之前找到,但仍然没有成功。

elem = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[@id="tr_exportpngl"]')))
elem.click()

How can i make this work?我怎样才能使这项工作?

i look forward for your suggestion.我期待着您的建议。 thank you in advance.先感谢您。

在此处输入图像描述

First, before clicking this button you need to click the export button which opens the dropdown with the options.首先,在单击此按钮之前,您需要单击导出按钮,它会打开带有选项的下拉菜单。 This code did the thing:这段代码做了这件事:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep


driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('https://re.jrc.ec.europa.eu/pvg_tools/en/')
driver.find_element(By.ID, 'map').click()
sleep(2)
driver.find_element(By.ID, 'tr_visualize').click()
sleep(2)
driver.find_element(By.XPATH, '//*[@class="highcharts-button"]').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'tr_exportpngl'))).click()

To enable download csv button you need first to click on the map.要启用下载 csv 按钮,您需要先单击 map。
So, after opening the link you need to wait for map to become clickable, then click on it.所以,打开链接后需要等map变为可点击,再点击。
Add a short delay to allow the file to be generated and then click on download csv button.添加一个短暂的延迟以允许生成文件,然后单击下载 csv 按钮。
The following code works clearly:下面的代码可以清楚地工作:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://re.jrc.ec.europa.eu/pvg_tools/en/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".ol-viewport canvas"))).click()
time.sleep(0.5)
wait.until(EC.element_to_be_clickable((By.ID, "horizondownloadcsv"))).click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM