如何使用 Selenium 和 Python 单击带有工具提示的元素作为导出为 CSV

How to click on the element with tooltip as Export as CSV using Selenium and Python

我正在尝试使用 selenium https://monitoringpublic.solaredge.com/solaredge-web/p/site/public?name=Alva%20Court%20E5&locale=en_GB#/dashboard 单击此网页上的 "export as csv" 按钮(该按钮位于 "Power and Energy" 标题旁边)。一旦我 运行 程序弹出站点但未单击下载按钮,导致超时异常

然而,该代码适用于我从另一个 Whosebug 问题 https://www.rotowire.com/football/injury-report.php 中找到的以下站点(尽管一旦我 运行 程序和站点弹出,我必须手动接受 cookie下载文件的顺序,但这是另一个问题)。

我的问题是为什么第二个 link 有效而第一个无效?

代码如下:

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

browser = webdriver.Chrome("C:/Path/chromedriver.exe")
url = 'https://monitoringpublic.solaredge.com/solaredge-web/p/site/public? name=Alva%20Court%20E5&locale=en_GB#/dashboard'
browser.get(url)

button = wait(browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "is-csv")))
button.click()

browser.close()

class 名称不正确。尝试使用以下 class 名称,

button = wait(browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "se-button-btn export_csv_btn")))
button.click()

对于功率和能量选择器是#power_energy_panel button[class*=export]
因为比较能量#se-comparative-energy-panel button[class*=export]

url = "https://monitoringpublic.solaredge.com/solaredge-web/p/site/public?name=Alva%20Court%20E5&locale=en_GB#/dashboard"

browser.get(url)
button = WebDriverWait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#power_energy_panel button[class*=export]")))
button.click()

要使用 工具提示 文本作为 导出为 CSV 在元素上调用 click() 你必须引入 WebDriverWait 对于 element_to_be_clickable() 你可以使用下面的 :

  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='x-panel-header-text' and text()='Power and Energy']//following::button[1]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照: