实例1.登录网易邮箱
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driverr=webdriver.Firefox() driverr.get("www.mail.163.com") driverr.find_element_by_id("idInput").send_keys("*********")
driverr.find_element_by_id("pwdInput").send_keys("*********")
driverr.find_element_by_id("pwdInput").send_keys(Keys.ENTER) time.sleep(2) #打印标题 title=driverr.title print(title)
driverr.quit()
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例2.多窗口切换搜索
#coding=utf-8
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
driver=webdriver.Firefox() driver.get("http://www.baidu.com")
#保存当前窗口
nowhandle=driver.current_window_handle
#按firefox的Ctrl+N新建一个浏览器窗口
time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘n‘)
driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘n‘) #获取所有窗口句柄
allhandles=driver.window_handles sogouhandle=driver.window_handles youdaohandle=driver.window_handles
#循环窗口
for handle in allhandles: if handle!=nowhandle: time.sleep(1) sogouhandle=handle for handle in allhandles: if handle !=sogouhandle :
youdaohandle=handle
#切换到另外一个窗口并打开www.sogou.com
driver.switch_to_window(sogouhandle) driver.get("http://www.sogou.com") time.sleep(2)
#切换回百度的窗口,并输入hello搜索
driver.switch_to_window(nowhandle) time.sleep(1) driver.find_element_by_id("kw").send_keys("hello") time.sleep(1) driver.find_element_by_id("kw").submit() time.sleep(2)
#切换到搜狗窗口,并输入yes搜索
driver.switch_to_window(sogouhandle) time.sleep(1) driver.find_element_by_id("query").send_keys("yes") time.sleep(1) driver.find_element_by_id("query").submit() time.sleep(2)
driver.switch_to_window(youdaohandle) time.sleep(1) driver.get("http://www.youdao.com") driver.find_element_by_id("query").send_keys("whoami") time.sleep(1) driver.find_element_by_id("query").submit()
#测试完成,退出浏览器
time.sleep(2) driver.quit()
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例3.百度上传图片搜索
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
#打开网址 driver=webdriver.Firefox() driver.get("http://image.baidu.com/") time.sleep(2)
driver.find_element_by_id(‘sttb‘).click() #time.sleep(2) driver.find_element_by_id("stfile").send_keys("C:\\Users\\Administrator\\Desktop\\cat.jpg")
#关闭浏览器 time.sleep(5) driver.quit()
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例4,通过关键字的id判断页面是否正常打开
#cocding=utf-8 from selenium import webdriver
import time driver=webdriver.Firefox() driver.get("http://www.baidu.com") time.sleep(2) title=driver.title print title time.sleep(5) oth=driver.find_element_by_name("tj_trnews").text print oth
if oth ==‘新闻‘: print"ok" else: print "not ok" driver.quit()
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例5:鼠标的拖动操作
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time
driver=webdriver.Firefox() driver.get("http://yun.baidu.com/") time.sleep(1)
driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("u**[email protected]") time.sleep(1) driver.find_element_by_id("TANGRAM__PSP_4__password").send_keys("useuue") time.sleep(1) driver.find_element_by_id("TANGRAM__PSP_4__submit").click() time.sleep(2) #driver.find_element_by_id("TANGRAM__PSP_18__closeBtn").click()
driver.get("http://pan.baidu.com/disk/home") #driver.find_element_by_xpath(‘//*[@id="bd"]/div[2]/div/ul[1]/a/span‘).click()
time.sleep(1) driver.find_element_by_id("_disk_id_12").click()
time.sleep(2)
#切换到缩略图模式 #driver.find_element_by_link_text(u"按缩略图模式").click() driver.find_element_by_xpath(‘//*[@id="yao-main"]/div/div[6]/ul[2]/a‘).click()
#指定要拖动的元素 element=driver.find_element_by_xpath(‘//*[@id="yao-main"]/div/div[4]/div[1]/div[2]/div[2]/div[1]/div[1]/span/span‘) #指定要拖动到的地方 target=driver.find_element_by_xpath(‘//*[@id="yao-main"]/div/div[4]/div[1]/div[2]/div[1]/div[1]/div[1]‘)
time.sleep(1) ActionChains(driver).drag_and_drop(element,target).perform()
time.sleep(2)
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例6:浏览器最大化操作
#coding=utf-8 import time from selenium import webdriver
webdriver = webdriver.Firefox() webdriver.get ("http://www.baidu.com")
print(u"浏览器最大化") webdriver.maximize_window() time.sleep(3)
webdriver.quit() print(u"退出浏览器成功")
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例7:浏览器设置尺寸
#coding=utf-8 import time from selenium import webdriver
browser = webdriver.Firefox() browser.get(‘http://m.mail.10086.cn‘) print("设置浏览器宽480、高800显示") browser.set_window_size(480,800) time.sleep(3) browser.quit()
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例8: 浏览器前进后退
#coding=utf-8 from selenium import webdriver import time
driver = webdriver.Firefox() first_url=‘http://www.baidu.com‘ print"now access %s" % (first_url) driver.get(first_url) second_url= ‘http://news.baidu.com‘
print("back to %s")%(first_url) driver.get (second_url) print("back to %s")% (first_url) driver.back()
print("forward to %s")%(second_url) driver.back() driver.quit()
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例9: 网页中键盘操作
#coding=utf-8
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver = webdriver.Firefox() driver.get("http://www.baidu.com") driver.find_element_by_id("kw").send_keys(u"广泛覆盖") time.sleep(2) driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE) time.sleep(3) driver.find_element_by_id("kw").send_keys(Keys.SPACE) driver.find_element_by_id("kw").send_keys(u"是个好人") time.sleep(2) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘a‘) time.sleep(3) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘x‘)
time.sleep(2) driver.find_element_by_id("kw").send_keys(Keys.CONTROL,‘v‘) time.sleep(3) driver.find_element_by_id("su").send_keys(Keys.ENTER) time.sleep(4) driver.quit()
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例 10 :小米官网鼠标悬停
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time
driver=webdriver.Firefox() driver.get("http://www.mi.com/") time.sleep(2)
above=driver.find_element_by_xpath(‘/html/body/div[2]/div[1]/div[2]/ul/li[2]/a/span[1]‘) #鼠标移动到above ActionChains(driver).move_to_element(above).perform() time.sleep(2) honhmi=driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/ul/li[3]/a/span[1]") ActionChains(driver).move_to_element(honhmi).perform() time.sleep(2) tv=driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/ul/li[5]/a/span[1]") ActionChains(driver).move_to_element(tv).perform() time.sleep(2) luyouqi=driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/ul/li[6]/a/span[1]") ActionChains(driver).move_to_element(luyouqi).perform() time.sleep(2) zhinengyingjian=driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/ul/li[7]/a/span[1]") ActionChains(driver).move_to_element(zhinengyingjian).perform() time.sleep(2) serever=driver.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/ul/li[8]/a/span") ActionChains(driver).move_to_element(serever).perform() time.sleep(3) driver.quit()
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例11:网站注册关掉提示框,并返回输入信息是否存在
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys
import time driver=webdriver.Firefox() driver.get("http://my.kuyiso.com/reg/") driver.find_element_by_xpath(‘/html/body/form/div[2]/div[1]/span/div/dl/dd[7]/input‘).click() driver.find_element_by_xpath(‘/html/body/form/div[2]/div[1]/span/div/dl/dd[8]/input‘).click() alert = driver.switch_to_alert()
alert.dismiss() driver.find_element_by_xpath(‘/html/body/form/div[2]/div[1]/span/div/dl/dd[1]/input‘).send_keys(‘good‘) driver.find_element_by_xpath(‘//*[@id="password"]‘).click() time.sleep(2) if(driver.find_element_by_id(‘showname‘).is_displayed()): print driver.find_element_by_id(‘showname‘).text time.sleep(2) driver.quit()
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
实例12:谷歌浏览器操作实例
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver) driver.get("http://www.baidu.com") driver.find_element_by_id(‘kw‘).send_keys(‘hello‘)
time.sleep(2) driver.quit()