使用Selenium使用Python下载XLS

2024-06-16 14:06:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是从特定月份的每一天下载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文件?非常感谢


Tags: divnonestylergbitemhighchartscursorclass
1条回答
网友
1楼 · 发布于 2024-06-16 14:06:56

这是用作容器的div元素。这也不是haveonClick()事件。通过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()

相关问题 更多 >