Selenium webdriver 学习笔记(一) - UI Element定位 .

上一篇 / 下一篇  2012-11-27 11:42:13 / 个人分类:Selenium

51Testing软件测试网+D]I#n@ H6hW/x

1. 创建Firefoxweb driver实例51Testing软件测试网0`ifEgA[;p!]L

1vO&thF)PW2Y5PC0   WebDriverdriver=newFirefoxDriver();                    //FireFox driver

"~r OB#{0

tX3igyt&H0   WebDriverdriver=newInternetExplorerDriver();   //IE driver

r1P*yi/sw051Testing软件测试网y@&[r!@*^9bc Qj!O

2. 获取Web page51Testing软件测试网`B1V%B+qQ){

51Testing软件测试网 v5y*~ P+z5n4a"j

   driver.get("http://www.google.com");51Testing软件测试网P,GG0mKr h

HEyp#x'l:nN0   或者:

pH"_u-B+zJ051Testing软件测试网H:X#F/H/~[ u{R7b

    driver.navigate().to("http://www.google.com");51Testing软件测试网 utte:P'i5H$c/~@

51Testing软件测试网{jeO j-bH6K

    //navigate().to和get()其实作用是一样的,但是navigate还可以进行浏览器的前进后退操作:

!|!H.RX#P u u0

@9L8QH3gWXy8G&Q0   driver.navigate().forward();51Testing软件测试网I'c g _9U

t'L|}x a z0    driver.navigate.back();

SW CC-D3B6o051Testing软件测试网 ^Jd(}Y b*@^ b

3. 定位UI Element51Testing软件测试网O1_)L!nDTMe3z]

51Testing软件测试网3H ] zA c$g)QU

    先安装FireBug(http://getfirebug.com/), 然后用FireBug定位页面元素的值。 如下图所示,只要把firebug的箭头放到要定位的元素上,就可以得到该元素对应标签的值, 比如goole的textbox:<input id="lst-ib" class="lst lst-tbb" type="text" maxlength="2048" name="q" autocomplete="off" size="41" title="Google 搜索" value="">, 然后我们就可以通过name, id, class等属性来定位这个输入框。51Testing软件测试网 H] J`+M+[5\

B|-U0M6?\;cj6c0    51Testing软件测试网4kX c$qQ j4o2Iov

51Testing软件测试网 M.uMPP4h-RcA

 51Testing软件测试网qa$r5A j!eU T9S

HG6n8D'` G03.1. 通过id

)VWr \@-g051Testing软件测试网2Sal K3^r\

WebElementelement=driver.findElement(By.id("lst-ib"));

yA S"W{i0

hR b'^,}1v d03.2. 通过class name

NY$NT;W V&t7R051Testing软件测试网8FMDJ Z)k6~h

WebElementelement=driver.findElement(By.className("lst lst-tbb"));51Testing软件测试网:u ~Ne4l N w

5[T-u*}5e8d03.3. 通过Name51Testing软件测试网9]%QH;Mt4t] |

51Testing软件测试网T%A c&w5]'a,Z4qMs

WebElementelement=driver.findElement(By.name("q"));51Testing软件测试网6F8H^)eH(l M+D T

51Testing软件测试网I,w.B!w4p:{di&D

3.4. 通过 Tag Name

$^HbgY0

xXn5u*J|hA$a0WebElementframe=driver.findElement(By.tagName("iframe"));51Testing软件测试网I.p[3xK$D0M

51Testing软件测试网J5ic`K

3.5.通过Link Text

K#N3X*W$d,~ry051Testing软件测试网S/_lv#A2[(?/PY

WebElementcheese=driver.findElement(By.linkText("cheese"));

"Yn-F%?Ey9W(R051Testing软件测试网:R!w` HV_i

3.6.通过Partial Link Text51Testing软件测试网.\1t4Hq8p(N.|m7Z2aFM

X DV G,RKgg0WebElementcheese=driver.findElement(By.partialLinkText("cheese"));51Testing软件测试网%^5Pdl TY @am

51Testing软件测试网_^7wz L|

3.7. 通过CSS

/nAYeP051Testing软件测试网w%z-|.gw

WebElementcheese=driver.findElement(By.cssSelector("#food span.dairy.aged"));

}X*CB!l-y*N}0

K1v+LCA/dAjmp03.8. 通过XPATH [注意:XPATH在IE上速度会比较慢,所以推荐使用css selector]51Testing软件测试网Vp0t ] uH

$JBg}0WQ.?5f l0List<WebElement>inputs=driver.findElements(By.xpath("//input"));

k7`(` u kz;U051Testing软件测试网po'qx7f}BJ


;m VZj!s*@Z03.9. 通过JavaScript
51Testing软件测试网n*C.aH3nD Z8RS*D;t

51Testing软件测试网}ib1_:Y;t"K;I$\f

WebElementelement=(WebElement)((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");

+Q!d1cAr6C0

TAG:

 

评分:0

我来说两句

Open Toolbar