如何自动点击网页上的按钮以下载文件?

huangapple go评论98阅读模式
英文:

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上执行此操作的一种方法。

首先:

  • 按照说明安装Selenium

  • 从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:

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.

huangapple
  • 本文由 发表于 2023年6月27日 20:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定