簡體   English   中英

使用 Selenium 通過 Python 下載 XLS

[英]Downloading XLS with Python using Selenium

我的目標是從特定月份的每一天下載 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 文件? 非常感謝!

這是用作容器的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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM