Selenium对浏览器的各种操作
作者:网络转载 发布时间:[ 2014/7/30 12:01:03 ] 推荐标签:软件测试 测试工具
(相关的XPATH教程具体可以参考W3C的教程进行学习)
Using JavaScript
这么这是两个例子,例子一需要提前加载jqury的支持
element=driver.execute_script("return $('.cheese')[0]")
labels=driver.find_elements_by_tag_name("label")inputs=driver.execute_script("var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){"+"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;",labels)
User Input - Filling In Forms
select=driver.find_element_by_tag_name("select")allOptions=select.find_elements_by_tag_name("option")foroptioninallOptions:print"Value is: "+option.get_attribute("value")option.click()
有些选择提取的元素是需要进行筛选的如
# available since 2.12fromselenium.webdriver.support.uiimportSelectselect=Select(driver.find_element_by_tag_name("select"))select.deselect_all()select.select_by_visible_text("Edam")
那么这些form进行如何提交呢?
driver.find_element_by_id("submit").click()
element.submit()
Moving Between Windows and Frames
<a href="somewhere.html"target="windowName">Click here to open a new window</a>
driver.switch_to_window("windowName")
driver.switch_to_frame("frameName")
driver.switch_to_frame("frameName.0.child")
切换一些弹出来的比如登录框等
Popup Dialogs
这类的有alerts, confirms, and prompts等
alert=driver.switch_to_alert()
返回的是一个弹窗的窗体对象
Cookies
# Go to the correct domaindriver.get("http://www.example.com")# Now set the cookie. Here's one for the entire domain# the cookie name here is 'key' and its value is 'value'driver.add_cookie({'name':'key','value':'value','path':'/'})# additional keys that can be passed in are:# 'domain' -> String,# 'secure' -> Boolean,# 'expiry' -> Milliseconds since the Epoch it should expire.# And now output all the available cookies for the current URLforcookieindriver.get_cookies():print"%s -> %s"%(cookie['name'],cookie['value'])# You can delete cookies in 2 ways# By namedriver.delete_cookie("CookieName")# Or all of themdriver.delete_all_cookies()
Changing the User Agent
profile=webdriver.FirefoxProfile()profile.set_preference("general.useragent.override","some UA string")driver=webdriver.Firefox(profile)
Drag And Drop
fromselenium.webdriver.common.action_chainsimportActionChainselement=driver.find_element_by_name("source")target=driver.find_element_by_name("target")ActionChains(driver).drag_and_drop(element,target).perform()
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
两种自动化测试工具AutoRunner与Selenium的对比几大市面主流性能测试工具测评自动化测试工具中的脚本回放报对象不存在如何解决?自动化测试工具中录制到不想要的对象该怎么办?QTP和AR两大自动化测试工具的对比软件测试中的虚拟化技术指的是什么?如何应用于测试工具中?常见的5个自动化测试工具对比分析如何借助性能测试工具PR进行性能瓶颈分析?浅谈自动化测试工具AutoRunner中对象库的重要性自动化测试工具AutoRunner常见问题及解决方法汇总app自动化测试工具MR的脚本调用自定义函数自动化测试工具AR对象库功能有什么意义?如何进行大数据量性能测试?大数据性能测试工具有哪些?软件测试常用的工具都有哪些?国内外软件测试工具都在这里了如何快速安装试用性能测试工具PR?移动端测试工具该怎么选择?移动端测试及使用工具详解

sales@spasvo.com