import time
from selenium import webdriver
from CookieRequests import BaseCookie


class Cookie(BaseCookie):
    def login(self, username, password):
        # driver = webdriver.PhantomJS('D:\Software\phantomjs-2.1.1-windows\\bin\phantomjs.exe')
        driver = webdriver.Chrome('D:\Software\chromedriver.exe')
        driver.get('https://login.sina.com.cn/signup/signin.php')
        username_ele = driver.find_element_by_id('username')
        username_ele.clear()
        username_ele.send_keys(username)
        pwd_ele = driver.find_element_by_id('password')
        pwd_ele.send_keys(password)

        sub_ele = driver.find_element_by_xpath('//div[@class="btn_mod"]/input[@type="submit"]')
        sub_ele.click()
        time.sleep(3)

        try:
            validation_code = driver.find_element_by_id('door')
            door = input('Please input validation code:')
            validation_code.send_keys(door)
            sub_ele.click()
            time.sleep(3)
            driver.get('http://weibo.com')
            time.sleep(5)
            cookie = dict()
            for c in driver.get_cookies():
                cookie[c['name']] = c['value']
            return cookie
        except:
            print('Get validation code error, maybe do not need')
            pass
        finally:
            driver.close()
