gpt4 book ai didi

python - 安全错误 : Permission denied to access property "document" on cross-origin object error clicking on download link in iframe using Selenium Python

转载 作者:行者123 更新时间:2023-12-04 04:05:03 28 4
gpt4 key购买 nike

我正在从事一个自动化项目,并且正在尝试从网站下载 pdf。该网站仅包含 pdf,但网页的文件类型为 HTML。 pdf 使用 PDF.js 显示,PDF.js 查看器也在 iframe 中。
当我尝试使用浏览器 javascript 单击元素时,返回与跨站点脚本相关的安全错误。

SecurityError: Permission denied to access property "document" on cross-origin object
我想从我的脚本中下载 pdf,用 python 编写,使用 selenium。当我尝试这个时:
driver.find_element_by_id('download').click()
没有产生任何结果,即使我将焦点切换到 selenium 中的 iframe,也没有点击下载按钮。
有人知道如何下载pdf的解决方案吗?

最佳答案

要单击元素,您必须诱导 WebDriverWaitelement_to_be_clickable()您可以使用以下任一 Locator Strategies :

  • 使用 ID :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "download"))).click()
  • 使用 CSS_SELECTOR :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#download"))).click()
  • 使用 XPATH :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='download']"))).click()
  • 备注 :您必须添加以下导入:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

  • 引用
    您可以在以下位置找到详细讨论:
  • Error: Permission denied to access property “x” due to same/cross origin policy using Selenium?
  • 关于python - 安全错误 : Permission denied to access property "document" on cross-origin object error clicking on download link in iframe using Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62643276/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com