首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用selenium下载单击事件上的文件?

如何使用selenium下载单击事件上的文件?
EN

Stack Overflow用户
提问于 2013-08-26 08:32:13
回答 4查看 121K关注 0票数 58

我正在研究蟒蛇和硒。我希望使用selenium从单击事件中下载文件。我写了以下代码。

代码语言:javascript
运行
复制
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get("http://www.drugcite.com/?q=ACTIMMUNE")

browser.close()

我想从名为“导出数据”的链接中下载两个文件。如何实现它,因为它只适用于单击事件?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-08-26 09:06:28

使用find_element(s)_by_*查找链接,然后调用click方法。

代码语言:javascript
运行
复制
from selenium import webdriver

# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/tmp')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

browser = webdriver.Firefox(profile)
browser.get("http://www.drugcite.com/?q=ACTIMMUNE")

browser.find_element_by_id('exportpt').click()
browser.find_element_by_id('exporthlgt').click()

添加了配置文件操作代码,以防止下载对话框。

票数 77
EN

Stack Overflow用户

发布于 2016-04-14 03:50:00

我承认这个解决方案比Firefox的替代方案更“麻烦”一些,但它同时适用于Chrome和Firefox,并且不依赖于任何时候都可能改变的浏览器特性。如果没有别的,也许这会给一些人一个关于如何解决未来挑战的不同观点。

Prerequisites:确保您安装了selenium和pyvirtualdisplay .

  • Python2:sudo pip install selenium pyvirtualdisplay
  • Python3:sudo pip3 install selenium pyvirtualdisplay

,魔术,

代码语言:javascript
运行
复制
import pyvirtualdisplay
import selenium
import selenium.webdriver
import time
import base64
import json

root_url = 'https://www.google.com'
download_url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'

print('Opening virtual display')
display = pyvirtualdisplay.Display(visible=0, size=(1280, 1024,))
display.start()
print('\tDone')

print('Opening web browser')
driver = selenium.webdriver.Firefox()
#driver = selenium.webdriver.Chrome() # Alternately, give Chrome a try
print('\tDone')

print('Retrieving initial web page')
driver.get(root_url)
print('\tDone')

print('Injecting retrieval code into web page')
driver.execute_script("""
    window.file_contents = null;
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function() {
        var reader  = new FileReader();
        reader.onloadend = function() {
            window.file_contents = reader.result;
        };
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', %(download_url)s);
    xhr.send();
""".replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ') % {
    'download_url': json.dumps(download_url),
})

print('Looping until file is retrieved')
downloaded_file = None
while downloaded_file is None:
    # Returns the file retrieved base64 encoded (perfect for downloading binary)
    downloaded_file = driver.execute_script('return (window.file_contents !== null ? window.file_contents.split(\',\')[1] : null);')
    print(downloaded_file)
    if not downloaded_file:
        print('\tNot downloaded, waiting...')
        time.sleep(0.5)
print('\tDone')

print('Writing file to disk')
fp = open('google-logo.png', 'wb')
fp.write(base64.b64decode(downloaded_file))
fp.close()
print('\tDone')
driver.close() # close web browser, or it'll persist after python exits.
display.popen.kill() # close virtual display, or it'll persist after python exits.

Explaination

我们首先在我们要针对的域加载一个URL,从这个域下载一个文件。这允许我们在该域中执行AJAX请求,而不会遇到跨站点脚本问题。

接下来,我们向DOM中注入一些javascript,从而触发AJAX请求。AJAX请求返回响应后,我们获取响应并将其加载到FileReader对象中。在这里,我们可以通过调用base64 ()来提取文件的readAsDataUrl编码内容。然后,我们获取base64编码的内容,并将其附加到window,这是一个易于访问的变量。

最后,由于AJAX请求是异步的,所以我们输入一个Python循环,等待附加到窗口的内容。附加后,我们解码从窗口检索到的base64内容,并将其保存到文件中。

此解决方案应适用于Selenium所支持的所有现代浏览器,无论是文本还是二进制,以及所有mime类型。

交替法

虽然我还没有对此进行测试,Selenium确实为您提供了等待元素出现在DOM中的能力。与其循环直到填充了一个全局访问变量,不如在DOM中创建一个具有特定ID的元素,并使用该元素的绑定作为触发器来检索下载的文件。

票数 18
EN

Stack Overflow用户

发布于 2017-05-18 22:15:00

在chrome中,我通过单击链接下载文件,然后打开chrome://downloads页面,然后从影子DOM中检索下载的文件列表,如下所示:

代码语言:javascript
运行
复制
docs = document
  .querySelector('downloads-manager')
  .shadowRoot.querySelector('#downloads-list')
  .getElementsByTagName('downloads-item')

此解决方案被限制为chrome,数据还包含文件路径和下载日期等信息。(注意这段代码来自JS,可能不是正确的python语法)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18439851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档