英文:
How to automate clicking on a button in webpage to download a file?
问题
在以下网页中:
https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals
有一个表格和一个"导出到Excel"按钮,如下图所示。
我需要做的是读取突出显示的按钮下面的表格,但问题是该表格分布在多个页面上,并且页面数量可能随时间而变化。
问题。是否可以从Python中"导出到Excel"该表格(即编写一个给"导出到Excel"按钮发送"点击"命令的代码)?
请问有人可以帮助我吗?
英文:
In the following web page:
https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals
There is a table and an "Export to Excel" button, as shown in the following image.
What I need to do is to read in the table below the highlighted button, but the problem is that that table is spread over multiple pages and the number of pages might vary over time.
Question. Is it possible to "Export to Excel" that table from Python (i.e.: writing a code that gives the command to "click" on "Export to Excel" button)?
Can anyone help me please?
答案1
得分: 1
以下是Windows上执行此操作的一种方法。
首先:
-
从https://chromedriver.chromium.org/downloads下载
ChromeDriver
,并将其保存到您选择的目录(比如说"C:/temp")。
然后:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# 设置
service = Service("C:/temp/chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# 转到页面
driver.get("https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals")
# 单击"Export to Excel"按钮
element = driver.find_element(By.XPATH, '//*[@id="santioned-table-element"]/button')
driver.execute_script("arguments[0].click();", element)
element = driver.find_element(By.XPATH, '//*[@id="santioned-table-element"]/button')
webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
driver.close()
您将在默认下载目录中找到文件**"Report_en.xls"**。
英文:
Here is one way to it on Windows.
First:
-
follow the instructions to install Selenium
-
download
ChromeDriver
from https://chromedriver.chromium.org/downloads and save it to the directory of your choice (say "C:/temp").
Then:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# Setup
service = Service("C:/temp/chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# Go to the page
driver.get("https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals")
# Clic "Export to Excel" button
element = driver.find_element(By.XPATH, '//*[@id="santioned-table-element"]/button')
driver.execute_script("arguments[0].click();", element)
element = driver.find_element(By.XPATH, '//*[@id="santioned-table-element"]/button')
webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
driver.close()
You will find the file "Report_en.xls" in your default download directory.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论