pandas 如何识别一个可点击的按钮并点击它- Selenium

mfpqipee  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(114)

在本网页中
https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals
有一个可单击的按钮,称为“导出到Excel”,如下图所示。

我需要从Python中点击它(以便下载Excel)。
我试过这个代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome(r"./driver/chromedriver")
driver.execute("get", {'url': 'https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals'})
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.ID, "Export to excel"))).click()

但我得到了这个错误:
追溯(最近一次调用):

File ~/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/Library/CloudStorage/GoogleDrive-glevorato@provenir.com/Shared drives/Provenir AI Team/Data Science/Data Science/09 AML/OFAC - SDN.py:359
    WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.ID, "Export to excel"))).click()

  File ~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:95 in until
    raise TimeoutException(message, screen, stacktrace)

TimeoutException

谁能告诉我,我做错了什么?

f87krz0w

f87krz0w1#

有两个问题,我认为:

1.您的定位器策略不正确,请更改如下:

# WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.ID, "Export to excel"))).click()
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Export to excel']"))).click()

1.您需要先单击AcceptCookies按钮,因为此元素与所需元素重叠,您可能会得到ElementClickInterceptedException。请参阅下面的代码以供参考。

请查看以下工作代码:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals')
wait = WebDriverWait(driver, 30)
# accept cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept all']"))).click()
# click on Export to excel button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Export to excel']"))).click()

相关问题