分享

Selenium-webdriver系列教程(五)————如何定位frame中的元素

 看见就非常 2015-09-18


selenium webdriver处理frame比较简单,这点比某些测试工具要先进一些,令人身心愉悦。

以下面的html代码为例,我们看一下如何定位frame上的元素。

frame.html

<html>

    <head>

        <title>Frame</title>

        <style>

            #f_1 {width: 10em; height: 10em; border: 1px solid #ccc; }

            #f_2 {display: none}

        </style>

    </head>

    <body>

        <p id = "p">Outside frame</p>

        <iframe id = "f_1" src = "part1.htm"></iframe>

        <iframe id = "f_2" src = "part2.htm"></iframe>

    </body>

</html>

part1.htm

<html>

    <head><title>Part1</title></head>

    <body>

        <p id = "f_p">This is part 1</p>

        <input id = "btn" type = "button" value = "click me" onclick = "alert('hello')" />

    </body>

</html>


switch_to方法会new1个TargetLocator对象,使用该对象的frame方法可以将当前识别的”主体”移动到需要定位的frame上去。

require 'selenium-webdriver'
dr = Selenium::WebDriver.for :chrome
frame_file = 'file:///'+File.expand_path(File.join(File.dirname(__FILE__),'frame.html'))

dr.navigate.to frame_file
#定位default content 上的p元素

dr.find_element(:id=>'p')

#将当前识别主体移动到id为f_1的frame上去
dr.switch_to.frame('f_1')

#点击frame上的button
button = dr.find_element(:id=>'btn')
button.click  # -->a alert will popup
alert = dr.switch_to.alert
alert.accept

#此时再去定位frame外的p 元素将出现错误
dr.find_element(:id=>'p') #--> error

#将识别的主体切换出frame
dr.switch_to.default_content

dr.find_element(:id=>'p') #--> ok


PS:如果用的是Selenium::WebDriver.for :ie
会遇到

这样的IE提示窗口,因此页面上的元素不能被找到
可以这样处理:
  工具-Internet选项-高级-允许活动内容在我的计算机上的文件中运行  勾上 就OK了

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多