python27+selenium+Phantomjs登录京东

selenium处理验证码还不会搞,之后再更新有验证码的登录

# _*_coding:utf-8_*_
from selenium import webdriver
import time
import requests
s = requests.session()
from lxml import etree

start_url = "https://passport.jd.com/uc/login?ltype=logout"
#driver = webdriver.Firefox()
driver = webdriver.PhantomJS()
def get_Login_Cookie():
    driver.get(start_url)
    time.sleep(1)
    elem_name = driver.find_element_by_name("loginname")
    elem_password = driver.find_element_by_name("nloginpwd")
    elem_name.clear()
    elem_password.clear()
    elem_name.send_keys("账号")
    elem_password.send_keys("密码")
    elem_login = driver.find_element_by_id("loginsubmit")
    elem_login.click()
    time.sleep(1)
    print driver.current_url
    cookies = driver.get_cookies()
    #print cookies
    cookie = []
    for item in cookies:
        cookie.append(item['name'] + '='  + item['value'])
    c = ';'.join(cookie)
    return c



def islogin():
    home_url = "https://home.jd.com/"
    page = s.get(home_url).text
    #print page
    dom = etree.HTML(page)
    Me = dom.xpath('/html/body/div[4]/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/a/text()')
    if Me:
        print "Cookie登录成功!"
    #print Me

if __name__ == "__main__":
    c = get_Login_Cookie()
    s.headers.update({"cookie":c})
    islogin()








你可能感兴趣的:(python)