@king
2015-10-16T15:09:07.000000Z
字数 3796
阅读 4214
Python
selenium webdriver (python)第三版:http://down.51cto.com/data/1985688
selenium IDE:
http://www.cnblogs.com/fnng/archive/2011/10/23/2222157.html
http://www.cnblogs.com/fnng/p/3300788.html
1.安装selenium: pip install selenium
2.安装ChromeDriver
官网:https://sites.google.com/a/chromium.org/chromedriver/
下载:http://chromedriver.storage.googleapis.com/index.html
若使用其他浏览器测试须另行下载对应的Driver。
下载后放到Chrome安装目录下,并把该目录添加到path环境变量
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains #ActionChains鼠标操作类
from selenium.webdriver.common.keys import Keys #keys类操作
# 打开浏览器:
browser = webdriver.Chrome()
# # 若未配置环境变量:
# path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
# browser = webdriver.Chrome(executable_path = path)
# 在指定时间范围等待:
browser.implicitly_wait(30)
# 设置超时
browser.set_page_load_timeout(30)
browser.set_script_timeout(30)
# 打开url:
browser.get(r"http://www.你的网站.com")
# 前进/后退
browser.forward()
browser.back()
# 刷新
browser.refresh()
# 将浏览器最大化
browser.maximize_window()
# 将设置浏览器为宽480,高800显示'
browser.set_window_size(480, 800)
# 关闭浏览器
browser.quit()
# 关闭标签
browser.close()
# 获取浏览器窗口相关信息
browser.title # 当前页面标题
browser.name # 浏览器名
browser.current_url # 返回当前页面url
browser.window_handles # 返回当前浏览器的所有窗口
browser.current_window_handle # 返回当前浏览器的窗口句柄
# 选择窗口
browser.switch_to_window('window_name')
# 截取当前显示的页面并保存
browser.get_screenshot_as_file(r'd:\backup\140591\桌面\首页图片\test.png')
# 定位元素
# 需要先分析源码
text_username = browser.find_element_by_name('username')
text_password = browser.find_element_by_name('password')
form_login = browser.find_element_by_name('formlogin')
# 也可以使用其他方法如xpath,注意转义符
text_username = browser.find_element_by_xpath('//input[@name = \'username\']')
'''
所有相关方法:
find_element(by='id', value=None)
find_element_by_class_name(name)
find_element_by_css_selector(css_selector)
find_element_by_id(id)
find_element_by_link_text(link_text)
find_element_by_name(name)
find_element_by_partial_link_text(link_text)
find_element_by_tag_name(name)
find_element_by_xpath(xpath)
'''
# 输入值
# send_keys也可以用于上传文件
text_username.send_keys("你的用户名_错误")
text_password.send_keys("你的密码")
'''
相关方法
send_keys(Keys.BACK_SPACE) # 返回键
send_keys(Keys.CONTROL,'a') # Ctrl + A
'''
# 清空输入框,换成正确的用户名
text_username.clear()
text_username.send_keys("你的用户名_正确")
# ----------------鼠标操作--------------------
# 右键单击
ActionChains(browser).context_click(text_username).perform()
# 双击
ActionChains(browser).double_click(text_username).perform()
#执行元素的移动操作
ActionChains(browser).drag_and_drop(text_username, text_password).perform()
# 提交表单
form_login.submit()
# -----------执行JavaScript脚本 --------------
js="var q=document.getElementById(\"user_name\");q.style.border=\"1px solid red\";"
#调用js
browser.execute_script(js)
# eg:
# 将页面滚动条拖到底部
js="var q=document.documentElement.scrollTop=10000"
browser.execute_script(js)
按键补充:
module:selenium.webdriver.common.keys
• class Keys()
– NULL = u’ue000’
– CANCEL = u’ue001’ # ^break
– HELP = u’ue002’
– BACK_SPACE = u’ue003’
– TAB = u’ue004’
– CLEAR = u’ue005’
– RETURN = u’ue006’
– ENTER = u’ue007’
– SHIFT = u’ue008’
– LEFT_SHIFT = u’ue008’ # alias
– CONTROL = u’ue009’
– LEFT_CONTROL = u’ue009’ # alias
– ALT = u’ue00a’
– LEFT_ALT = u’ue00a’ # alias
– PAUSE = u’ue00b’
– ESCAPE = u’ue00c’
– SPACE = u’ue00d’
– PAGE_UP = u’ue00e’
– PAGE_DOWN = u’ue00f’
– END = u’ue010’
– HOME = u’ue011’
– LEFT = u’ue012’
– ARROW_LEFT = u’ue012’ # alias
– UP = u’ue013’
– ARROW_UP = u’ue013’ # alias
– RIGHT = u’ue014’
– ARROW_RIGHT = u’ue014’ # alias
– DOWN = u’ue015’
– ARROW_DOWN = u’ue015’ # alias
– INSERT = u’ue016’
– DELETE = u’ue017’
– SEMICOLON = u’ue018’
– EQUALS = u’ue019’
– NUMPAD0 = u’ue01a’ # numbe pad keys
– NUMPAD1 = u’ue01b’
– NUMPAD2 = u’ue01c’
– NUMPAD3 = u’ue01d’
– NUMPAD4 = u’ue01e’
– NUMPAD5 = u’ue01f’
– NUMPAD6 = u’ue020’
– NUMPAD7 = u’ue021’
– NUMPAD8 = u’ue022’
– NUMPAD9 = u’ue023’
– MULTIPLY = u’ue024’
– ADD = u’ue025’
– SEPARATOR = u’ue026’
– SUBTRACT = u’ue027’
– DECIMAL = u’ue028’
– DIVIDE = u’ue029’
– F1 = u’ue031’ # function keys
– F2 = u’ue032’
– F3 = u’ue033’
– F4 = u’ue034’
– F5 = u’ue035’
– F6 = u’ue036’
– F7 = u’ue037’
– F8 = u’ue038’
– F9 = u’ue039’
– F10 = u’ue03a’
– F11 = u’ue03b’
– F12 = u’ue03c’
– META = u’ue03d’
– COMMAND = u’ue03d’