1 from selenium import webdriver
 2 from selenium.common.exceptions import TimeoutException
 3 from selenium.webdriver.common.by import By
 4 from selenium.webdriver.support.ui import WebDriverWait
 5 from selenium.webdriver.support import expected_conditions as EC
 6 
 7 def get_cookies(self):
 8     browser = webdriver.Chrome()
 9     wait = WebDriverWait(browser, 10)
10     try:
11         browser.get('https://mail.126.com')
12         iframe = wait.until(
13             EC.presence_of_element_located((By.CSS_SELECTOR, "#x-URS-iframe"))
14             )
15         # browser.switch_to_frame('x-URS-iframe')#Firefox
16         browser.switch_to_frame(0)#Chrome
17         userName = browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div/div[2]/input').send_keys('username')
18         password = browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[3]/div[2]/input[@class="j-inputtext dlpwd"]').send_keys('password')
19         dologin = browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[8]/a')
20         dologin.click()
21         dologinAgain =wait.until(
22             EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[3]/div[1]/div[2]/div[2]/a[1]'))
23             )
24         dologinAgain.click()
25         time.sleep(5)
26         browser.switch_to_default_content()
27         browser.get('http://study.163.com')
28         cookie_dict = {}
29         for item in browser.get_cookies():
30             cookie_dict[item['name']] = item['value']
31         browser.close()
32         return cookie_dict
33     except TimeoutException:
34         self.get_cookies()

 

posted on 2018-04-10 16:52  math98  阅读(102)  评论(0编辑  收藏  举报