测试必须在时间、质量和成本之间获取一个平衡点,这是测试策略和测试设计的价值体现。

2.10 select下拉框

上一篇 / 下一篇  2018-06-12 10:48:45 / 个人分类:Python+Selenium2 WebDriver API

2.10 select下拉框

51Testing软件测试网fX,{K tXND

本篇以百度设置下拉选项框为案例,详细介绍select下拉框相关的操作方法。

*K)}5mvl6Y:{ g^R1D051Testing软件测试网 Y6\0FFaiY*n/]

一、认识select51Testing软件测试网[_ ?i*L
    1.打开百度-设置-搜索设置界面,如下图所示51Testing软件测试网 H[Q.a;z8U,j

O*r*CheE7H*i0

8d;R&Z4|5b#C)G0

w:]/jf-Mys0 51Testing软件测试网3@g9F$Oz{Gg5l

;Q;q%mL { es"r M0 

x.X4t@${a~q0N]4@0

U#yx%r!k'^x2]'oLY0    2.箭头所指位置,就是select选项框,打开页面元素定位,下方红色框框区域,可以看到select标签属性:                   51Testing软件测试网%nK%XB7tXKs

<selectid="nr"name="NR">

;w} o0qW&R k;D0    3.选项有三个。

} p\;h*e.C/W3|nq0
<optionselected=""value="10">每页显示10条</option><optionvalue="20">每页显示20条</option><optionvalue="50">每页显示50条</option>

m-U2h2MA!C!P/Qe0二、二次定位
}$F6BZk1k0    1.定位select里的选项有多种方式,这里先介绍一种简单的方法:二次定位51Testing软件测试网S/[ i5wG
    2.基本思路,先定位select框,再定位select里的选项            
w/_U5J:e'^v?0    3.代码如下:51Testing软件测试网-PP O3Hb)g"AXs

51Testing软件测试网J3\XI#a6T'`|

6a%es#s*t[ e_0

;~xY)a6k*OdF0 51Testing软件测试网cxg2I,y5M m

51Testing软件测试网k.I0S3Mx$s9I

   4.还有另外一种写法也是可以的,把最下面两步合并成为一步:   

;Jv2Nl:UJV0
driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

1A1B"`^7k0三、直接定位51Testing软件测试网1afrm A-u+c+~`
    1.有很多小伙伴说firebug只能定位到select框,不能定位到里面的选项,其实是工具掌握的不太熟练。小编接下来教大家如何定位里面的选项。51Testing软件测试网!_lm;w7_Cb&~2ki$S
    2.用firebug定位到select后,下方查看元素属性地方,点select标签前面的+号,就可以展开里面的选项内容了。51Testing软件测试网J5uy2rI9S*Ys

EHe#lv2u#e051Testing软件测试网!\W)V@m+U8Y5wTb2D

6u%P r?0ohp&K0 3.然后自己写xpath定位或者css,一次性直接定位到option上的内容。(不会自己手写的,回头看前面的元素定位内容)51Testing软件测试网7b#T+lO!R~&W

51Testing软件测试网aF]/T1F|/}/X\

)RK%w.c jj0

P-[2z Ph[.I0 51Testing软件测试网'v7ft(K&K.^ST#e

51Testing软件测试网r ^Eiw

四、Select模块(index)

&}:js3` ?'NN0

{0S fmQD-hWu0    1.除了上面介绍的两种简单的方法定位到select选项,selenium还提供了更高级的玩法,导入Select模块。直接根据属性或索引定位。51Testing软件测试网't*A1oBeC [ O:D
    2.先要导入select方法:
x^xP'U _F0l1Y0from selenium.webdriver.support.select import Select       
;R8s$wT3`1M)A0    3.然后通过select选项的索引来定位选择对应选项(从0开始计数),如选择第三个选项:select_by_index(2)51Testing软件测试网e[H;@;@A r

51Testing软件测试网k1o }bG Y*B

T,Y@/rV O051Testing软件测试网z G7o(kh

五、Select模块(value)

%Y7WH.Uw051Testing软件测试网3C-}qL:e#r u$E2[%X

    1.Select模块里面除了index的方法,还有一个方法,通过选项的value值来定位。每个选项,都有对应的value值,如51Testing软件测试网6?EK8~:j/_

<selectid="nr"name="NR"><optionselected=""value="10">每页显示10条</option><optionvalue="20">每页显示20条</option><optionvalue="50">每页显示50条</option></select>

1_]O&k8b3lY0   2.第二个选项对应的value值就是"20":select_by_value("20")

Dg N1U tuSw8e0

)wBj*Jn Mya0

K:Z_"]s*j.z+JNG'w$Ub051Testing软件测试网^H*o&sS

 

Q(T0hpN.F(s051Testing软件测试网.gYr_%yz/U `c

六、Select模块(text)
h*a^:D1dO!Y0    1.Select模块里面还有一个更加高级的功能,可以直接通过选项的文本内容来定位。
2F9Y0B$P*`+u-|*Jg0    2.定位“每页显示50条”:select_by_visible_text("每页显示50条")

"cq%q3q {G tFw$i051Testing软件测试网%\R3q/gc?1^;N

jZp/sTRmfB0

zS/g to xy'\0 

1t/v;`&qu#@l P051Testing软件测试网0y'h9w8Z9\

七、Select模块其它方法51Testing软件测试网[*r+n|;FV$oe
    1.select里面方法除了上面介绍的三种,还有更多的功能如下:51Testing软件测试网lA*jrt8Gk

N9f}6VPj;f0

G2y@WQ/f4S|051Testing软件测试网(].X$Od6w N

select_by_index()  :通过索引定位
%l&J i&Pa b0select_by_value()  :通过value值定位
5V(_7F,qU6WB+G,B0select_by_visible_text() :通过文本值定位
%L;Yg#P `?0deselect_all()          :取消所有选项
&Nn;?xM U/v4y0deselect_by_index()     :取消对应index选项
0`*CW4q,^ vBH5L0deselect_by_value()      :取消对应value选项51Testing软件测试网 j3o.t~*HG
deselect_by_visible_text() :取消对应文本选项51Testing软件测试网IS5l7?l.D

To$\X)P&@gI0first_selected_option()  :返回第一个选项51Testing软件测试网 LW#y2E&~q Vv"?a
all_selected_options()   :返回所有的选项51Testing软件测试网7IVcCMxM

51Testing软件测试网|(ev*j$T~

 

XI@`1v*N&w%h H0

?7\T4u.C*Y {NI0八、整理代码如下:51Testing软件测试网.na$htP] l?1H

复制代码
#coding:utf-8fromseleniumimportwebdriverfromselenium.webdriver.common.action_chainsimportActionChainsfromselenium.webdriver.support.selectimportSelect
driver=webdriver.Firefox()
url="https://www.baidu.com"driver.get(url)
driver.implicitly_wait(20)#鼠标移动到“设置”按钮mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()#通过text:select_by_visible_text()s = driver.find_element_by_id("nr")
Select(s).select_by_visible_text("每页显示50条")## 分两步:先定位下拉框,再点击选项s = driver.find_element_by_id("nr")s.find_element_by_xpath("//option[@value='50']").click()## 另外一种写法driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()## 直接通过xpath定位driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()## 通过索引:select_by_index()s = driver.find_element_by_id("nr")
Select(s).select_by_index(2)## 通过value:select_by_value()s = driver.find_element_by_id("nr")
Select(s).select_by_value("20")
复制代码

TAG:

 

评分:0

我来说两句

Open Toolbar