如何使用Selenium和ChromeDriver下载文件(How to download a file with Selenium and ChromeDriver)

编程入门 行业动态 更新时间:2024-10-25 08:21:44
如何使用Selenium和ChromeDriver下载文件(How to download a file with Selenium and ChromeDriver)

我有一个网站可以使用硒和ChromeDriver(在Windows上)进行测试,我喜欢测试导出数据并再次导入数据的功能。

导出将创建一个xml文件,并将其下载到一台计算机上。 当使用webdriver运行此操作时,Chrome会询问我是保留该文件还是丢弃该文件,因为这可能是潜在的威胁。

如何在测试中关闭此行为? 有没有我可以使用的chrome设置,以便无论下载什么文件都可以?

谢谢

I have a website to test with selenium and ChromeDriver (on windows), where I like to test the functionality to export data and import it again.

The export creates a xml file that is downloaded on ones computer. When running this with webdriver, Chrome asks me whether to keep the file or discard it, as it might be a potential threat.

How can I switch off this behavior inside my test ? Is there a chrome setting I can use, so that a file is no matter what downloaded ?

Thanks

最满意答案

尝试这个。 在Windows上执行

( 如何在Chrome中使用Selenium Python绑定来控制文件的下载 )

from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_experimental_option("prefs", { "download.default_directory": r"C:\Users\xxx\downloads\Test", "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True })

Try this. Executed on windows

(How to control the download of files with Selenium Python bindings in Chrome)

from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_experimental_option("prefs", { "download.default_directory": r"C:\Users\xxx\downloads\Test", "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True })

更多推荐

本文发布于:2023-07-24 10:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   Selenium   ChromeDriver   download

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!