在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 用selenium登陸后,獲取登陸后頁面的鏈接后的網(wǎng)頁,然后保存為PDF,報錯了

用selenium登陸后,獲取登陸后頁面的鏈接后的網(wǎng)頁,然后保存為PDF,報錯了。。

用selenium登陸后,獲取登陸后頁面的鏈接后的網(wǎng)頁,然后保存為PDF,我用代碼是:

all = driver.find_elements_by_css_selector('.title')
for i in range(1):
    print i
    print all[i].text
    all[i].click()
    time.sleep(4)
    html = driver.page_source
    print type(html)
    path_wkthmltopdf = r'F:\Python\Anaconda\Lib\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    pdfkit.from_string(html, "F:\Confirm Data\{}.pdf".format(all[i].text), configuration=config)

clipboard.png

謝謝。

回答
編輯回答
小眼睛

提示錯誤就是所引用的元素已不再依附于當(dāng)前頁面了,原因大致是頁面跳轉(zhuǎn)或者刷新導(dǎo)致的頁面內(nèi)容變更。
估計你是想遍歷文章標(biāo)題,點擊標(biāo)題進入正文,然后抓取保存成pdf。

那么看代碼:

pdfkit.from_string(html, "F:\Confirm Data\{}.pdf".format(all[i].text), configuration=config)

在 click() 事件后,頁面都刷新或者跳轉(zhuǎn)了,從新頁面的源碼中引用 all[i].text 導(dǎo)致報錯。

2018年8月19日 12:26