import json from time import sleep from selenium import webdriver import chardet from selenium.webdriver import ActionChains #初始化瀏覽器 # driver = webdriver.Chrome(executable_path = "e:\\chromedriver") driver = webdriver.Firefox(executable_path = "e:\\geckodriver") # driver = webdriver.Ie(executable_path = "e:\\IEDriverServer") #定義全局遍變量url url = "https://www.jd.com" def login_cookie(): #打開瀏覽器 driver.get(url) # 瀏覽器最大化 driver.maximize_window() #定位登錄button driver.find_element_by_xpath('//a[@class = "link-login"]').click() #定位賬戶登錄 driver.find_element_by_xpath('//a[text()="賬戶登錄"]').click() #定位賬號框,並輸入賬號 driver.find_element_by_xpath('//input[@name="loginname"]').send_keys("18501057627") #定位密碼框,並輸入密碼 driver.find_element_by_xpath('//input[@type="password"]').send_keys("GaoXXX") #點擊登錄button driver.find_element_by_xpath('//a[@id="loginsubmit"]').click() sleep(10) #需要手動滑動圖片,通過校驗 #獲取cookie my_cookie = driver.get_cookies() print(my_cookie) data_cookie = json.dumps(my_cookie) with open("jd_coolies","w") as fp: fp.write(data_cookie) #使用cookies def get_url_with_cookies(): # 訪問網站,清空舊cookies信息 driver.get(url) driver.delete_all_cookies() #獲取cookies文件 with open("jd_coolies","r") as fp: jd_cookies = fp.read() #加載cookies信息 jd_cookies_dict = json.loads(jd_cookies) for cookie in jd_cookies_dict: driver.add_cookie(cookie) #驗證是否登錄成功 driver.get(url) assert '退出' in driver.page_source print(url) # 添加購物車 def shopping(): driver.get('https://www.jd.com/') # 定位搜索框,並輸入:Python自動化 driver.find_element_by_xpath("//input[@clstag='h|keycount|head|search_c']").send_keys('Python自動化') # 定位“搜索”button,並點擊 driver.find_element_by_xpath('//button[@clstag="h|keycount|head|search_a"]/i').click() # 獲取當前窗口句柄 now_handle = driver.current_window_handle # 打印當前窗口句柄 print("添加購物車窗口") print(now_handle) #判斷 不是 當前窗口句柄 # 獲取所有窗口句柄 all_handles = driver.window_handles # 循環遍歷所有新打開的窗口句柄,也就是說不包括主窗口 for handle in all_handles: if handle != now_handle: # 切換窗口 driver.switch_to.window(handle) sleep(5) # 點擊加入購物車 driver.find_element_by_xpath("//div[@class='itemInfo-wrap']/div/div/a[contains(@onclick,'加入購物車')]").click() # 調用driver的page_source屬性獲取頁面源碼 pageSource = driver.page_source # 斷言頁面源碼中是否包含“商品已成功加入購物車”關鍵字,以此判斷頁面內容是否正確 assert "商品已成功加入購物車" in pageSource print("商品已成功加入購物車") def payOrder(): # # 獲取當前窗口句柄 current_handle = driver.current_window_handle # 打印當前窗口句柄 print(current_handle) print("點擊購物車") # 點擊“我的購物車” driver.find_element_by_xpath("//a[text()='我的購物車']").click() sleep(2) all_handles = driver.window_handles # 循環遍歷所有新打開的窗口句柄,也就是說不包括主窗口 for handle in all_handles: if handle != current_handle: # 切換窗口 driver.switch_to.window(handle) sleep(5) # 點擊“去結算”button driver.find_element_by_xpath("//div[@id='cart-floatbar']/div/div/div/div[2]/div[4]/div[1]/div/div[1]").click() # driver.find_element_by_xpath("//a[contains(text(),'去結算')]").click() sleep(2) # 點擊“提交訂單”button driver.find_element_by_xpath("//button[@id='order-submit']").click() # 調用driver的page_source屬性獲取頁面源碼 pageSource = driver.page_source # 斷言頁面源碼中是否包含“商品已成功加入購物車”關鍵字,以此判斷頁面內容是否正確 assert "訂單提交成功,請盡快付款" in pageSource if __name__=="__main__": login_cookie() get_url_with_cookies() shopping() payOrder()