【问题标题】:Downloading XLS with Python using Selenium使用 Selenium 通过 Python 下载 XLS
【发布时间】:2020-01-24 14:27:44
【问题描述】:

我的目标是从特定月份的每一天下载 XLS 文件。但是,当我使用 chrome 的开发人员工具检查时,XLS 文件的下载按钮根本不是一个按钮。那么如何下载 XLS 呢?这里不是按钮的“按钮”的HTML...

<div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download XLS</div>

这个类在另一个类中。完整代码在这里

<div class="highcharts-menu" style="box-shadow: rgb(136, 136, 136) 3px 3px 10px; border: 1px solid rgb(153, 153, 153); background: rgb(255, 255, 255); padding: 5px 0px;">
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Print chart</div>
    <hr>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download PNG image</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download JPEG image</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download PDF document</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download SVG vector image</div>
    <hr>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download CSV</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Download XLS</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">View data table</div>
    <div class="highcharts-menu-item" style="cursor: pointer; padding: 0.5em 1em; color: rgb(51, 51, 51); background: none; font-size: 11px; transition: background 250ms ease 0s, color 250ms ease 0s;">Open in Highcharts Cloud</div></div>

加上 Xpath 总是在变化,我如何下载那个 XLS 文件?非常感谢!

【问题讨论】:

  • 如果它是公开的,你能分享应用程序的网址吗?你能看到下载文件的网址吗?
  • 很遗憾,该网页不公开。不,我看不到下载文件的 URL。

标签: python html selenium web-scraping download


【解决方案1】:

这是用作容器的div 元素。这也没有onClick() 事件。通过 html,它似乎是不可点击的元素,但您可以手动点击。你可以试试下面的代码。

它会找到元素,如果它是可点击的,那么它就会点击。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//div[.= 'Download XLS']")))

    If(myElem.is_enabled() and myElem.is_displayed()):

    myElem.click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2017-11-04
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多