当前位置:网站首页>playwright控制本地谷歌浏览打开,并下载文件
playwright控制本地谷歌浏览打开,并下载文件
2022-04-23 13:12:00 【两个南瓜饼】
控制谷歌浏览器打开我就直接上大佬的源码了,我主要聊聊下载的一些问题
from getpass import getuser
import time
from playwright.sync_api import sync_playwright
import requests
# 不出意外这个可以获取到你google的本地缓存文件
__USER_DATE_DIR_PATH__ = f"C:\\Users\\{getuser()}\\AppData\Local\Google\Chrome\\User Data"
# chrome.exe 的地址
__EXECUTABLE_PATH__ = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
playwright = sync_playwright().start()
browser = playwright.chromium.launch_persistent_context(
# 指定本机用户缓存地址
user_data_dir=__USER_DATE_DIR_PATH__,
# 指定本机google客户端exe的路径
executable_path=__EXECUTABLE_PATH__,
# 要想通过这个下载文件这个必然要开 默认是False
accept_downloads=True,
# 设置不是无头模式
headless=False,
bypass_csp=True,
slow_mo=10,
#跳过检测
args=['--disable-blink-features=AutomationControlled']
)
page = browser.new_page()
'''下载'''
# 打开下载器
with page.expect_download() as download_info:
#找到你要下载的东西,找到点击的元素,点击
downloadx = '//*[@id="_view_1545184311000"]/div[2]/div[4]/a[3]'
page.click(downloadx)
# 下载的东西
download = download_info.value
# 下载的路径
print(download.path())
# 下载的文件名
print(download.suggested_filename)
几个下载的问题
首先说明 download 存在三个可调用的方法,分别是
download.path()
download.save_as()
download.suggested_filename
一三就是文件名和路径,第二个可以设置存储路径,但是这个方法的使用是在下载后才可以使用,而且对D,E盘无访问权限,可以说没啥用。所以还是要知道playwright 的默认存储设置。
默认情况下,每次使用with as 打开下载会在C:\Users\ADMINI~1\AppData\Local\Temp\路径下创建一个 以 playwright-artifacts-cLLZs3 命名的文件夹,cLLZs3 是每次打开随机生成的。下载的文件的文件名也是随机的 GUID。例如 3c5b244f-4a05-44d7-bb03-0e9fc863abe2 20220422162554220422BPA2ZFNR40.zip
版权声明
本文为[两个南瓜饼]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_59938810/article/details/124350045
边栏推荐
- Important knowledge of transport layer (interview, retest, final)
- GIS practical tips (III) - how to add legend in CASS?
- 8086 of x86 architecture
- 9419 page analysis of the latest first-line Internet Android interview questions
- 安装nngraph
- LeetCode_ DFS_ Medium_ 695. Maximum area of the island
- Translation of attention in natural language processing
- These vscode extensions are my favorite
- C语言之字符串与字符数组的区别
- 2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
猜你喜欢

2021年6月程序员工资统计,平均15052元,你拖后腿了吗?

Recovering data with MySQL binlog

GIS practical tips (III) - how to add legend in CASS?

100 lectures on practical application cases of Excel (VIII) - report connection function of Excel

Common interview questions and detailed analysis of the latest Android developers in 2020

Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus

Vscode tips

缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章

@优秀的你!CSDN高校俱乐部主席招募!

Imx6ull QEMU bare metal tutorial 2: usdhc SD card
随机推荐
SPI NAND flash summary
Install nngraph
nodeJs + websocket 循环小案例
[walking notes]
Recovering data with MySQL binlog
Start mqbroker CMD failure resolution
SQL exercise question 1
8086 of x86 architecture
100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
Go language: passing slices between functions
Mui close other pages and keep only the first page
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
nodejs + mysql 实现简单注册功能(小demo)
MySQL -- 16. Data structure of index
Hanlp word splitter (via spark)
mui 微信支付 排坑
Async void caused the program to crash
HQL statement tuning
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
2020年最新字节跳动Android开发者常见面试题及详细解析