参考【http://blog.csdn.net/henni_719/article/details/51096531】大神写的笔记,多谢大神共享。
哈哈,今天又找到一位大神写的Selenium2+Python自动化的好文了,收藏收藏
http://www.cnblogs.com/fnng/p/3160606.html
1、Webdriver API
1)定位元素:Python中定位元素的方法如下:
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
在使用By时需要将By类导入:from selenium.webdriver.common.by import By
2)控制浏览器:
a、Webdriver提供set_windows_size()方法设置浏览器大小;
#coding=utf-8 from selenium import webdriver driver=webdriver.Chorme() driver.get(“http://m.mail.10086.cn”) print u“设置浏览器的宽480,高800显示” #u:代表里面字符串是Unicode编码的driver.set_window_size(480,800) driver.quit()
b、Maximize_window():使浏览器最大化
c、back()和forward()方法来模拟后退和前进
3)常用的几个元素属性
size:返回元素大小
text:返回元素的文本值
get_attribute(name):获取元素某个属性的值
is_displayed():判断元素是否是显示,True:显示,False:隐藏
size = driver.find_element_by_id('kw').sizeprint type(size)print "kw element size is %r" % repr(size) text = driver.find_element_by_id('kw').textprint "kw element size is %s" % text attribute = driver.find_element_by_id("kw").get_attribute("type")print "kw element attribute is %s" % attribute re = driver.find_element_by_id("kw").is_displayed()print "kw element is displayed? %r" % re输出:kw element size is "{'width': 500, 'height': 22}"kw element size is kw element attribute is textkw element is displayed? True
4)鼠标事件:webdriver中关于鼠标操作是由ActionChains类提供。
鼠标类的包:from selenium.webdriver.common.action_chains import ActionChains
常用的鼠标操作的方法:
perform():执行ActionChains类存储的方法
context_click():右击
double_click():双击
drag_and_drop():拖动
drag_and_drop(source,target):从原元素上按下左键拖拽元素到目标元素后释放
move_to_element():鼠标移动到某元素
click_and_hold():按下鼠标左键停在某个元素上
1 from selenium import webdriver 2 from selenium.webdriver.common.action_chains import ActionChains 3 import time 4 import os 5 if __name__ == "__main__": 6 chromeDriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver_x64.exe" 7 os.environ["webdriver.chrome.driver"] = chromeDriver 8 driver = webdriver.Chrome(chromeDriver) 9 driver.get("http://yun.baidu.com")10 11 time.sleep(3)12 driver.find_element_by_id("TANGRAM__PSP_4__userName").clear()13 driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("username") 14 driver.find_element_by_id("TANGRAM__PSP_4__password").clear() 15 driver.find_element_by_id("TANGRAM__PSP_4__password").send_keys("password") 16 driver.find_element_by_id("TANGRAM__PSP_4__submit").click() 17 18 disk_NET=driver.find_element_by_class_name("pulldown-nav") 19 ActionChains(driver).move_to_element(disk_NET).perform() 20 time.sleep(3)21 DC=driver.find_element_by_link_text("网盘")22 #感觉下面写的xpath语句也没错,但是执行了没有点击效果???/(ㄒoㄒ)/~~23 #DC = driver.find_element_by_xpath("//div[@class='navs']/div[1]/div[1]/div[@class='content']/a[1]/span[1]")24 ActionChains(driver).double_click(DC).perform() 25 26 time.sleep(10)27 driver.quit()
5)键盘事件:Keys类提供操作键盘上几乎所有键的方法,send_keys()模拟键盘输入,还可以模拟一些组合键,比如:ctrl+c、Ctrl+v等
键盘类的包:from selenium.webdriver.commom.keys import Keys
常用的键盘操作:
send_keys(Keys.BACK_SPACE):删除键(BackSpace)
send_keys(Keys.SPACE):空格键(Space)
send_keys(Keys.TAB):制表键(Tab)
send_keys(Keys.ESCAPE):回退键(Esc)
send_keys(Keys.ENTER):回车键(Enter)
send_keys(Keys.CONTROL,'a'):全选(Ctrl+A)
send_keys(Keys.CONTROL,'c'):复制(Ctrl+C)
send_keys(Keys.CONTROL,'x'):剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v'):粘贴(Ctrl+V)
send_keys(Keys.F1):键盘F1
1 from selenium.webdriver.common.keys import Keys 2 import time 3 def keyboard(driver): 4 time.sleep(2) 5 driver.find_element_by_id("kw").send_keys("vameii") 6 driver.find_element_by_id("kw").submit() 7 #由于下面每步都执行的很快,所以加个time.sleep()执行效果更明显 8 #删除多余的i 9 driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)10 time.sleep(3)11 #Ctrl+a全选输入框内容 12 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')13 time.sleep(3) 14 #Ctrl+x全选输入框内容 15 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')16 time.sleep(3) 17 #Ctrl+v全选输入框内容 18 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')19 time.sleep(3) 20 driver.find_element_by_id("su").send_keys(Keys.ENTER) 21 time.sleep(3)22 23 driver.quit()