【问题标题】:How do I get Python to click on a button, download the Excel file and save it into a designated folder?如何让 Python 点击​​按钮,下载 Excel 文件并将其保存到指定文件夹中?
【发布时间】:2019-11-22 08:29:12
【问题描述】:

已编辑 - 数据是机密数据并导致网站崩溃。

【问题讨论】:

  • 您的问题似乎过于笼统,最好先专注于修复该错误。
  • 你为什么要使用 selenium、requests 和 beautifulsoup 来完成相同的任务?而且您从未启动过webdriver。对于错误,如果您在其中,您需要通过代理,或者您可以使用 time.sleep 等待。您也可以在 requests.get 中使用 verify=False 作为参数。
  • @datacookies,您编辑了声明数据是机密的问题。请注意,问题版本被保留,因此任何人仍然可以使用原始文本。如果确实有必要从站点中删除该数据,您有两种选择:check this post 自己删除问题。该网站可能会拒绝这样做。如果是这种情况,您可以尝试将您的问题标记为需要主持人干预,并解释您的请求。

标签: python-3.x


【解决方案1】:

这是使用 selenium 打开 chrome 并通过单击按钮下载文件的代码。

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Options for Chrome WebDriver
op = Options()
op.add_argument('--disable-notifications')
op.add_experimental_option("prefs",{
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True 
})

# Download Path
download_dir = 'D:\\'

# Initializing the Chrome webdriver with the options
driver = webdriver.Chrome(options=op)

# Setting Chrome to trust downloads
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
command_result = driver.execute("send_command", params)

driver.implicitly_wait(5)

# Opening the page
driver.get("https://apa.nexregreporting.com/home/portfoliocompression")

# Click on the button and wait for 10 seconds
driver.find_element_by_xpath('//*[@class="btn btn-default"]').click()
time.sleep(10)

# Closing the webdriver
driver.close()

【讨论】:

  • 自从我使用find_elements_by_css_selector('btn.btn-default') 并试图访问列表的索引以单击它以来,我一直在忙于寻找一种单击方式。你使用xpath 做得很好
  • 你也可以使用 CSS 选择器driver.find_elements_by_css_selector('input.btn.btn-default').click()
  • 让我们处理这个案例。有趣stackoverflow.com/questions/58949031/…
  • @αԋɱҽԃαмєяιcαη 我已经回答了提到的问题。希望它有效。
  • 你有我的回应:)
猜你喜欢
  • 2014-02-26
  • 2019-11-18
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多