【问题标题】:anchor tag with java Script onclick event using selenium in Python在 Python 中使用 selenium 的带有 javaScript onclick 事件的锚标记
【发布时间】:2022-08-19 15:44:31
【问题描述】:

在此网站 (https://bvmf.bmfbovespa.com.br/NegociosRealizados/Registro/Index/pt-BR?Length=8) 上,您可以通过 javascript onclick 事件下载附加在以下锚标记上的 csv 文件:

<a href=\"#divListaArquivos\" onclick=\"carregarDownloadArquivo(\'11-08-2022\')\">Negociação Balcão - 11/08/2022</a>

我正在尝试编写一个 Python 脚本来下载这个文件,但是当我使用点击()下面代码中的方法,没有任何反应。我首先使用具有相同结果的链接名称定位元素。现在,即使在通过 xpath 定位元素之后,文件仍然不会被下载。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

driver.get(r\"https://bvmf.bmfbovespa.com.br/NegociosRealizados/Registro/Index/pt-BR?Length=8\")

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,\"/html/body/div/div[4]/div[3]/div/div/div/div[1]/div[2]/p/a\")))
element.click()

    标签: python selenium web-scraping


    【解决方案1】:

    不是纯selenium,但是可以直接使用driver.execute_script()触发功能。

    像这样的东西应该工作:

    driver.get("https://bvmf.bmfbovespa.com.br/NegociosRealizados/Registro/Index/pt-BR?Length=8")
    driver.execute_script("carregarDownloadArquivo('11-08-2022')")
    

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 2012-08-15
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多