selenium切換標簽頁driver.close()無法運行

CSDN問答 2022-01-07 12:52:21 阅读数:212

selenium driver.close driver close
  
import seleniumfrom selenium import webdriverimport osimport requestsfrom PyPDF2 import PdfFileReaderimport randomimport timefrom selenium.webdriver.common.keys import Keysimport pandas as pddef makedirs_D(): file_path = 'D:'+"\\公司年報\\" if not os.path.exists(file_path): os.makedirs(file_path) print("New file_path:",file_path)# 解析網址def get_html_content(url): header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'} res = requests.get(url, headers=header) if res.status_code == 200: res.encoding="UTF-8" # print(res.content) # print("Sucessfully Got urlText!") return res.content else: print("Failed to get urlText!")# 保存pdfdef report_save(url, pdf_name): report = get_html_content(url) path = "D:\\公司年報\\" + pdf_name + ".pdf" with open(path, 'wb') as f: f.write(report) # print("Sucessfully Saved PDFreport!")# 獲取年報頁數def get_num_pages(pdf_name): path = "D:\\公司年報\\" + pdf_name + ".pdf" reader = PdfFileReader(path) if reader.isEncrypted: reader.decrypt('') page_num = reader.getNumPages() return page_numdriver = webdriver.Chrome(r'D:\softs\python3.8\Scripts\chromedriver.exe')driver.implicitly_wait(5)url = 'http://www.cninfo.com.cn/new/commonUrl/pageOfSearch?url=disclosure/list/search&checkedCategory=category_ndbg_szsh#sse'driver.get(url)# # 修改財報公布日期期間# driver.find_element_by_xpath('//tbody/tr[4]/td[7]/div/span').click()# driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/button[7]').send_keys(Keys.ENTER)def main(): makedirs_D() items = [] count = 1 while count <= 1: count += 1 time.sleep(2) all_tr = driver.find_element_by_xpath('//*[@id="main"]/div[2]/div[1]/div[1]/div[2]/div/div[3]/table/tbody').find_elements_by_xpath('.//tr') for tr in all_tr: item = {} a = random.random() * 3 time.sleep(a) # 設置隨機休息 number = tr.find_elements_by_xpath('./td')[0].find_element_by_xpath('.//span').text item['公司代碼'] = number name_pre = tr.find_elements_by_xpath('./td')[1].find_element_by_xpath('.//span').text name = name_pre.replace('*', '~') # windows命名不能包含*,去除* item['公司名稱'] = name title = tr.find_elements_by_xpath('./td')[2].find_element_by_xpath('.//a').text item['公告標題'] = title date = tr.find_elements_by_xpath('./td')[3].find_element_by_xpath('.//span').text item['公告發布日期'] = date year = str(int(date[0:4]) - 1) # 次年發布的頭年年度報告 item['年報對應年份'] = year pdf_name = number + "-" + name + "-" + year + "-" + title # 設置pdf命名 # print(pdf_name) handle_main = driver.current_window_handle # 句柄 tr.find_elements_by_xpath('./td')[2].find_element_by_xpath('.//a').send_keys(Keys.ENTER) # 點擊進入網頁 if len(driver.window_handles) > 1: # 注意 for handle in driver.window_handles: if handle != handle_main: driver.switch_to.window(handle) # 切換到新句柄 pdf_href = driver.find_element_by_xpath( '//*[@id="noticeDetail"]/div/div[2]/div[1]/a').get_attribute('href') # 獲取pdf網址 print(pdf_href) report_save(pdf_href, pdf_name) driver.close() driver.switch_to.window(handle_main) # 切換回主句柄 driver.quit()......if __name__=="__main__": main()
  

Q:注釋掉driver.close()則程序無法持續運行; 不注釋掉則driver中存多個標簽頁且標簽頁切換時從右往左逐頁過渡。請問如何調整!先致謝!




采納答案:

driver.close() 注釋掉則程序無法持續運行,不注釋掉則driver標簽頁越來越多且切換到主句柄時是依次從標簽頁從右向左。請求幫助!


版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071252210005.html