首先 从 pip 安装 selenium
pip install selenium
Demo1 : 使用不同Driver 来抓取页面 title
# pip install selenium
__author__ = 'wangshaoxing'
from selenium import webdriver
driverArray=[
webdriver.Ie()
];
def openBroswer(driver):
driver.set_window_size(1300,600)
driver.get("http://www.jd.com")
print driver.name+": "+driver.title
driver.close()
pass
for d in driverArray:
openBroswer(d)
pass
Demo2: 跳转一次页面 ,并且获得页面内容
__author__ = 'wangshaoxing'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("http://www.jd.com/")
driver.set_window_size(1300,600)
elem = driver.find_element_by_id("key111")
elem.send_keys("iphone")
elem.send_keys(Keys.RETURN)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "J-global-toolbar"))
)
fs=open("jd.txt","w")
fs.write(driver.page_source.encode("utf-8"))
fs.close()
driver.close()
Demo3: 与javascript进行交互
__author__ = 'wangshaoxing'
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1300,600)
driver.get("http://www.jd.com/")
#js_result=driver.execute_script(""" return $("#logo-2014").offset() """)
js_result=driver.execute_script(""" return 1 """)
print js_result
driver.close()
Demo4: 截取屏幕
__author__ = 'wangshaoxing'
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.set_window_size(1300,600)
driver.get("http://www.jd.com/")
driver.execute_script("""
setInterval(function(){
window.scrollBy(0,100)
},100)
""")
time.sleep(8)
js_result=driver.execute_script("""
return $("body").height();
""")
print js_result
driver.save_screenshot("screen.png")
driver.close()
Demo5:截取屏幕并裁切适当区域
__author__ = 'wangshaoxing'
from selenium import webdriver
import sys
from PIL import Image
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.set_window_size(1300,600)
driver.get("http://www.jd.com/")
driver.save_screenshot("screen1.png")
js_result = driver.execute_script("""
var position=$("#logo-2014").offset();
var width = $("#logo-2014").width();
var height= $("#logo-2014").parent().height();
var result={
position:position,
width:width,
height:height
}
return result;
""")
# {u'position': {u'top': 30, u'left': 0}, u'height': 60, u'width': 362}
driver.save_screenshot("screen1.png")
im = Image.open("screen1.png")
box = im.copy()
box = (int(js_result["position"]["left"]),int(js_result["position"]["top"]),int(js_result["width"]+js_result["position"]["left"]),int(js_result["height"]+js_result["position"]["top"]))
region = im.crop(box)
region.save("small1.png")
driver.close()
Demo6:完成一次完整的模拟登陆
#coding=utf-8
__author__ = u'wangshaoxing'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os
driver = webdriver.PhantomJS()
driver.set_window_size(1300,600)
driver.get(u"https://passport.jd.com/uc/login")
# elem = driver.find_element_by_id("key")
# # elem = driver.find_element_by_id("key1")
# elem.send_keys("iphone")
# elem.send_keys(Keys.RETURN)
element_user = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, u"loginname"))
)
element_user.clear()
element_user.send_keys(u"wshxbqq1")
element_pwd = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, u"nloginpwd"))
)
element_pwd.clear()
element_pwd.send_keys(u"13031446049w")
element_btn = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, u"loginsubmit"))
)
element_btn.click()
time.sleep(2)
driver.save_screenshot("vr.png")
vr=""
while not os.path.exists("vr.txt"):
print "waitting for code"
time.sleep(2)
pass
f=open("vr.txt","r")
vr=f.read()
f.close()
element_vr = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, u"authcode"))
)
element_vr.send_keys(vr)
element_btn.click()
time.sleep(2)
driver.get(u"http://cart.jd.com/cart")
# element_test = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.ID, "service-2014"))
# )
# driver.get("http://cart.jd.com/cart")
#
# element_test = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.ID, "service-2014"))
# )
# fs=open("jd.txt","w")
# fs.write(driver.page_source.encode("utf-8"))
# fs.close()
#driver.close()