首页 > 解决方案 > Selenium 单击 SharePoint 列表中的“导出”CSV 按钮

问题描述

我需要单击位于 SharePoint 列表顶部命令栏中的两个按钮来完成 CSV 下载。

Excel 按钮 CSV 按钮

我尝试使用下面的 2 个定位器和下面的显式等待,但没有成功。

driver.find_element_by_xpath('//*[@id="appRoot"]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div/div/div/div/div/div/div[1]/div[4]/button/span').click()
driver.find_element_by_xpath('//*[@id="id__850-menu"]/div/ul/li[2]/button/div/span').click()

excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()

导出按钮 html:

<span class="ms-Button-flexContainer flexContainer-124" data-automationid="splitbuttonprimary"><i data-icon-name="svg/excel_16x1_5.svg" aria-hidden="true" class="ms-Button-icon icon-127"><img alt="" src="https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/brand-icons/product/svg/excel_16x1_5.svg"></i><span class="ms-Button-textContainer textContainer-119"><span class="ms-Button-label label-125" id="id__850">Export</span></span><i data-icon-name="ChevronDown" role="presentation" aria-hidden="true" class="ms-Icon root-33 css-81 ms-Button-menuIcon is-expanded menuIcon-187" style="font-family: FabricMDL2Icons;">&lt;/i><span class="ms-layer"></span></span>

CSV 按钮 html:

<div class="ms-ContextualMenu-linkContent linkContent-199"><span class="ms-ContextualMenu-itemText label-205">CSV</span></div

在此处输入图像描述 在此处输入图像描述

错误:

Traceback (most recent call last):
  File "C:\Users\main.py", line 44, in <module>
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
  File "C:\Users\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

我的代码:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pandas
from tkinter import messagebox

driver = webdriver.Chrome(r"C:\Users\chromedriver.exe")  # change path 

target_url = 'some_url'

driver.get(target_url)

email_field = (By.ID, "i0116")
password_field = (By.ID, "i0118")
next_btn = (By.ID, "idSIButton9")
two_factor_btn = (By.ID, 'idSubmit_SAOTCC_Continue')

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(email_field)).send_keys("username")

# Click Next
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()

# wait for password field and enter password
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(password_field)).send_keys("password")

# Click Login
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()

# shows message box to promp input
messagebox.showinfo(title='2-step verification', message='Input on 2-step verification, (30 seconds timeout)')
time.sleep(30)

# 2 factor auth
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(two_factor_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()

# download csv 
excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()

# driver.find_element_by_xpath('//*[@id="id__124-menu"]/div/ul/li[1]/button/div')
# driver.find_element_by_xpath('//*[@id="ContextualMenu427"]/div/ul/li[2]/button/div/span')

谢谢你的帮助!

标签: pythonseleniumxpathwebdriverwebdriverwait

解决方案


我没有找到ID您提供的任何此类 HTML。尝试使用以下 xpath。

excel_btn = (By.XPATH, "//span[text()='Export']")
csv_btn = (By.XPATH, "//Span[text()='CSV']")

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()

希望这能解决您的问题。


推荐阅读