1 登录京东: 2 3 from selenium import webdriver 4 5 import random 6 import time 7 8 def randomSleep(minS, maxS): 9 time.sleep((maxS - minS) * random.random() + minS) 10 browser = webdriver.Chrome(r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe") 11 # browser = webdriver.Chrome() 12 13 # Home 14 browser.get("https://passport.jd.com/new/login.aspx") 15 16 # Login by username and password 17 randomSleep(1, 2) 18 browser.find_element_by_xpath("//a[@clstag='pageclick|keycount|201607144|2']").click() 19 # browser.find_element_by_tag_name("pageclick|keycount|201607144|2").click() 20 21 # Username and password 22 randomSleep(1, 2) 23 browser.find_element_by_id("loginname").send_keys(“xxxxxxx") 24 print(browser.find_element_by_id("loginname").text) 25 26 randomSleep(1, 3) 27 browser.find_element_by_id("nloginpwd").send_keys(“xxxxxx") 28 29 # Submit, wait for a long time 30 randomSleep(5, 10) 31 browser.find_element_by_id("loginsubmit").click() 32 33 34 print(browser.get_cookies()) 35 randomSleep(3, 5) 36 37 38 browser.quit()
1 登录12306: 2 #!/usr/bin/env python 3 # -*- coding: utf-8 -*- 4 # @Time : 2018/1/20 18:33 5 # @Author : lingxiangxiang 6 # @File : demon9.py 7 8 from selenium import webdriver 9 10 import random 11 import time 12 13 def randomSleep(minS, maxS): 14 time.sleep((maxS - minS) * random.random() + minS) 15 browser = webdriver.Chrome(r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe") 16 17 url = "https://kyfw.12306.cn/otn/login/init" 18 19 browser.get(url=url) 20 print(browser.find_element_by_id("username").text) 21 browser.find_element_by_id("username").clear() 22 browser.find_element_by_id("username").send_keys("974644081@qq.com") 23 randomSleep(2, 5) 24 browser.find_element_by_id("password").send_keys(“xxxxxxxxx") 25 randomSleep(1, 4) 26 time.sleep(6) 27 browser.find_element_by_id("loginSub").click() 28 randomSleep(2, 5) 29 print(browser.get_cookies()) 30 browser.quit()
常用方法: browser.get_cookies() browser.title 头名字 browser.close() forward() 前进,browser.foeward() .back() 后退,browser.back() .refresh() 刷新,browser.refresh() .current_url 返回当前页面url,browser.current_url