在这种错误中,我建议使用XPath和显式等待来处理它。尝试首先导入所有需要的包:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
现在,初始化驱动程序。我在用Chrome,所以:
driver = webdriver.Chrome()
driver.get('URL_OF_YOUR_PAGE') # Replace with the actual URL
最后,等待元素出现并可交互:
try:
# wait for the container div to be present
wait = WebDriverWait(driver, 10)
container = wait.until(EC.presence_of_element_located(
(By.CLASS_NAME, 'Transactions_csvButtonsContainer__dhv_J')))
# find the second SVG element within the container
download_button = container.find_elements(By.TAG_NAME, 'svg')[1]
# click the download button
download_button.click()
except Exception as e:
print(f"An error occurred: {e}")
finally:
driver.quit()
我正在使用异常处理,所以如果发生任何错误并且这些步骤无法工作,您可以尝试调试或其他策略。请随时更新,希望对我有所帮助!