您的位置:首页 > 其它

使用selenium进行自动化测试

2013-10-05 15:33 477 查看
selenium 支持多个客户端:ruby,Java,python。可以用来对网页进行全面测试,支持真实浏览器测试。

firefox

IE

chrome

safari

支持多操作系统:

Linux

windows

mac osx

安装:

sudo pip install selenium

测试:

#coding=utf-8
from selenium import webdriver
b = webdriver.Firefox()
b.get("http://www.baidu.com")
b.find_element_by_id("kw").send_keys("渗透")
b.find_element_by_id("su").click()


在新窗口里面打开一个链接

b= webdriver.Firefox()
a = b.find_element_by_tag_name('a')
b.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", a)


切换到新窗口

b.switch_to_window(b.window_handles[-1])    #切换到最后一个打开的窗口
b.close()    #关闭
b.switch_to_window(b.window_handles[0])    #回到第一个打开的窗口


获取a的源代码

a.get_attribute('outerHTML')


获取href

a.get_attribute("href")


弹出窗口的完整代码:

#coding:utf-8
from selenium import webdriver

b = webdriver.Firefox()
b.get("http://www.cpython.org")
links = b.find_elements_by_tag_name("a")

for l in links:
n = b.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", l)
href = n.get_attribute('href')
if href.find('mailto:') > -1:
continue
n.click()
b.switch_to_window(b.window_handles[-1])
b.close()
b.switch_to_window(b.window_handles[0])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: