SecurityError:拒绝访问属性“文档"的权限关于跨域对象错误,请使用Selenium Python单击iframe中的下载链接 [英] SecurityError: Permission denied to access property "document" on cross-origin object error clicking on download link in iframe using Selenium Python

查看:91
本文介绍了SecurityError:拒绝访问属性“文档"的权限关于跨域对象错误,请使用Selenium Python单击iframe中的下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个自动化项目,并且正在尝试从网站下载pdf.该网站仅包含pdf,但网页的文件类型为HTML.使用PDF.js显示pdf,并且PDF.js查看器也位于iframe中.

I am working on an automation project and I am trying to download a pdf from a website. The website only contains the pdf but the file type of the webpage is HTML. The pdf is displayed using PDF.js and the PDF.js viewer is also in an iframe.

当我尝试使用浏览器javascript单击该元素时,返回与跨站点脚本相关的安全性错误.

When I tried to click the element using browser javascript, i was returned with a security error relating to cross site scripting.

SecurityError: Permission denied to access property "document" on cross-origin object

我想从我的脚本中使用selenium下载以python编写的pdf文件.当我尝试这个:

I would like to download the pdf from my script, written in python, using selenium. When I try this:

driver.find_element_by_id('download').click()

没有结果,即使我将焦点切换到硒中的iframe,下载按钮也没有被点击.

No results are produced, the download button doesn't get clicked even though I have switched focus to the iframe in selenium.

有人知道如何下载pdf的解决方案吗?

Does anybody know a solution how to download the pdf?

推荐答案

要单击该元素,您必须引入定位器策略:

To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following 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()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

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

    You can find a detailed discussion in:

    这篇关于SecurityError:拒绝访问属性“文档"的权限关于跨域对象错误,请使用Selenium Python单击iframe中的下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆