python - Selenium 单击 SharePoint 列表中的“导出”CSV 按钮
问题描述
我需要单击位于 SharePoint 列表顶部命令栏中的两个按钮来完成 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;"></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')
谢谢你的帮助!
解决方案
我没有找到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()
希望这能解决您的问题。
推荐阅读
- python-3.x - 在 __add__ 和 __radd__ 中编写相同的代码,更好的方法是什么?
- javascript - 未捕获(承诺中)类型错误:无法读取未定义的属性“启用”
- mysql - 我的 SUM 查询返回 2 行,需要一些建议
- html - 无法将 CSS 文件链接到 HTML 文件
- javascript - 有没有办法制作没有边框的Material ui Card
- javascript - 如何摆脱反应图标(引导程序)中的可用空间?
- python - 为什么我不能使用 Gensim 下载 API 下载数据集
- c# - 在 C# 中连接到 MS Access 数据库时出现错误
- python - 尽管网络大、样本量小且数据经过预处理,Tensorflow LSTM 仍无法学习
- swiftui - 检测用户何时停止/暂停在 TextField 中写入 - SwiftUI