Selenium + python + Google Drive

问题描述 投票:0回答:1

我正在使用带有硒的python进行网络刮擦以及无头模式下的镀铬驱动程序(没有ui)。问题是我点击一个href按钮,该动作不下载我想要的PDF文件。但事情是它可能下载但我不知道在哪里。它没有做任何事情,没有错误,没有。

同样的动作与gui模式完美配合。

有什么建议?。

提前致谢!。

python selenium selenium-chromedriver
1个回答
0
投票

初始化chromedriver时,可以设置下载文件的默认目录:

import os
from selenium import webdriver
# Set the target subfolder based on the current working directory
dl_folder = '/downloads'
dl_location = os.path.join(os.getcwd(), dl_folder)

# Add the headless argument
chrome_options.add_argument('headless')

# Prepare a dict with additional preferences. This is where the magic happens:
prefs = {"download.default_directory": dl_location}
chrome_options.add_experimental_option("prefs", prefs)

# launch the driver
driver = webdriver.Chrome(executable_path= '/path/to/executable', chrome_options= chrome_options)

之后,单击初始化下载的内容

exportbtn = driver.find_element_by_id('exporter-csv')
exportbtn.click()

然后从文件夹中检索最新添加的文件。例如,如果是csv将其加载到数据帧:

import glob
import pandas as pd
list_of_files = glob.glob(dl_location+ '/*')
latest_file = max(list_of_files, key=os.path.getctime)
df = pd.read_csv(latest_file)

我不熟悉直接获取文件名或文件的方法。如果有任何我很好奇听到它。

© www.soinside.com 2019 - 2024. All rights reserved.