问题遇到的现象和发生背景
因故,需要将1000多人的某种信息在某平台上进行查询,然后打印出来。
想法是用python + selenium+Chrome, 另存为PDF。在非headless状态下勉强运作(涉及另外两个问题放后面),
主要问题:headless模式下,save as PDF好像根本不工作,没有生成PDF文件。
次要问题:1、在检查网页元素是否生存的情况 下,还会时不时因为元素没生成 而出错。
2、采用先保存PDF后重命名的 方式批量保存,经常因找不到下载好的PDF文件无法rename。但文件事实上已经下载好了。原因判断不来。
问题相关代码,请勿粘贴截图
import 省略
#warnings.filterwarnings('ignore')
wb = load_workbook('C:/Users/Administrator/Desktop/2#.xlsx') #要查询的列表
ws1 = wb['Sheet1']
ws2 = wb['Sheet2']
sn=ws1['A'] #序号
name=ws1['B'] #姓名
pid=ws1['E'] #编号之类的
i=0
while i < ws1.max_row :
print(sn[i].value,name[i].value,pid[i].value)#看进度
filepatch='C:/Users/Administrator/Desktop/查询结果/' #保存PDF文件的地方
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account":"",
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
profile = {
'printing.print_preview_sticky_settings.appState': json.dumps(appState),
'savefile.default_directory': filepatch
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')
chrome_options.add_argument('--enable-print-browser')
# chrome_options.add_argument('--headless') #目前headless模式下不会存PDF
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
newname=str(sn[i].value) +str(name[i].value) + '.pdf'
browser = webdriver.Chrome("chromedriver.exe",options=chrome_options)
url = 'https://*****inquire.html' #隐私保护,在这里*掉,请见谅。
browser.get(url)
browser.maximize_window()
browser.implicitly_wait(5)
browser.find_element_by_id('personName').send_keys(name[i].value)
browser.find_element_by_id('personIdcard').send_keys(pid[i].value)
browser.find_element_by_id('submitBtn').click()
button = browser.find_element_by_xpath('//*[@id="verify-wrap"]/span[1]')
action = ActionChains(browser)
#滑动验证后点一个“查看”按钮,显示更多结果。
action.click_and_hold(button).perform()
action.move_by_offset(browser.find_element_by_xpath('//*[@id="verify-wrap"]/span[2]').size.get('width'), 0).perform()
action.release().perform()
#下面两行不是很理解 ,估计是第一行逻辑有问题,导致“查看”结果时,时不时出错。
WebDriverWait(browser,10,0.5).until(expected_conditions.element_to_be_clickable(browser.find_element_by_xpath('//*[@id="lookBtn"]')))
WebDriverWait(browser,10,0.5).until(expected_conditions.element_to_be_clickable((By.XPATH,'//*[@id="lookBtn"]')))
browser.find_element_by_xpath('//*[@id="lookBtn"]').click()
temp_title = browser.title
newname=str(sn[i].value) +str(name[i].value) + '.pdf' #filepatch+
browser.execute_script('document.title='+'"'+ newname + '"' +';')
browser.execute_script('return window.print();')
#time.sleep(5)
#采用先保存PDF后改名的方式,时不时提示“找不到文件”,猜测OS.renames的时候,PDF文件还没存好,难道是异步执行的原因??但查询相关资料,browser.execute_script却是同步的,不理解相关机制,不好判断。
#os.renames(filepatch + temp_title + '.pdf', filepatch+str(sn[i].value) +str(name[i].value) + '.pdf')
#os.system("pause")
browser.close()
i=i+1
运行结果及报错内容
主要问题:headless模式下 根本没有生成PDF文件。显式模式下,会在指定的目录下生成PDF文件。
我的解答思路和尝试过的方法
网外有讨论过类似问题,大意是:Chrome headless模式下会忽略save as pdf参数块设置的保存位置,而默认保存在Chrome 的程序目录,而高版本Chrome 的默认目录又禁止headless模式下载文件。
我想要达到的结果
headless 模式下 正常地 将网页另存为PDF文件。