SecurityError:使用 Selenium Python 单击 iframe 中的下载链接时出现跨源对象访问属性“文档”的权限被拒绝

2024-01-18

我正在开发一个自动化项目,我正在尝试从网站下载 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的解决方案吗?


要单击您必须诱导的元素WebDriver等待 https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808为了element_to_be_clickable()您可以使用以下任一方法定位策略 https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "download"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#download"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='download']"))).click()
    
  • Note:您必须添加以下导入:

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

参考

您可以在以下位置找到详细讨论:

  • 错误:由于使用 Selenium 的同源/跨源策略,访问属性“x”的权限被拒绝? https://stackoverflow.com/questions/55621387/error-permission-denied-to-access-property-x-due-to-same-cross-origin-policy/55628911#55628911
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SecurityError:使用 Selenium Python 单击 iframe 中的下载链接时出现跨源对象访问属性“文档”的权限被拒绝 的相关文章

随机推荐