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

2.10 select下拉框

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

2.10 select下拉框

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

p0co~^(U;i f051Testing软件测试网a-P2Xk'Mk.|

一、认识select51Testing软件测试网v%C6m'^/S2M,ZE0[%R
    1.打开百度-设置-搜索设置界面,如下图所示

g@ t:L5b7Z c3[B051Testing软件测试网/^ pQ3RJ

51Testing软件测试网tCkhvm

(ATJ|K/\[0 

4UQ0[jHL| ft'a0

5g `bL"H^0 51Testing软件测试网'E;t/qF7U

&J"}]}"n)FrcU0    2.箭头所指位置,就是select选项框,打开页面元素定位,下方红色框框区域,可以看到select标签属性:                   

W;M*~m&ha#M-^0
<selectid="nr"name="NR">
51Testing软件测试网 B WJiM2XX

    3.选项有三个。51Testing软件测试网-]D*S:l8udM!`

<optionselected=""value="10">每页显示10条</option><optionvalue="20">每页显示20条</option><optionvalue="50">每页显示50条</option>
51Testing软件测试网&Z(T[ \'?*_x M9g:b

二、二次定位51Testing软件测试网3zHs7c&L
    1.定位select里的选项有多种方式,这里先介绍一种简单的方法:二次定位51Testing软件测试网j~nQ Cc&? OU
    2.基本思路,先定位select框,再定位select里的选项            51Testing软件测试网 Z&U:P9gi)Z
    3.代码如下:51Testing软件测试网;dq+E7Y{M@4`@'r+w

51Testing软件测试网 ax(B4I+Y%g

xZ-m}A051Testing软件测试网 t0O2?;z;R8Y8al

 51Testing软件测试网^m#wB,O"m&X

51Testing软件测试网 ?n3Sm M}

   4.还有另外一种写法也是可以的,把最下面两步合并成为一步:   51Testing软件测试网IJ`/b*l

driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()
51Testing软件测试网XnnVX~d;@6~

三、直接定位
,J/Y} D}0    1.有很多小伙伴说firebug只能定位到select框,不能定位到里面的选项,其实是工具掌握的不太熟练。小编接下来教大家如何定位里面的选项。
J~0q+y%lK~X}0    2.用firebug定位到select后,下方查看元素属性地方,点select标签前面的+号,就可以展开里面的选项内容了。

G3XWlT/G]e5p3LOB0

3yO9iM)O"C051Testing软件测试网$OsQxt&c2L8T

R,EZNN S1Mb;fk0 3.然后自己写xpath定位或者css,一次性直接定位到option上的内容。(不会自己手写的,回头看前面的元素定位内容)

6p)Z1]M U0w0

c,S0Q$^ACzr'hk:~0

])N'E hcT0

?6F:hYvV*m0 51Testing软件测试网t i)@P`9E8VX

51Testing软件测试网 j%dG!v0|

四、Select模块(index)51Testing软件测试网({1oSPr4m^

\(Z+v9e `!y~"r8p(~0    1.除了上面介绍的两种简单的方法定位到select选项,selenium还提供了更高级的玩法,导入Select模块。直接根据属性或索引定位。51Testing软件测试网!gKt]9LZ d!AI
    2.先要导入select方法:
UC^A4tg0from selenium.webdriver.support.select import Select       51Testing软件测试网B/W:_^9N
    3.然后通过select选项的索引来定位选择对应选项(从0开始计数),如选择第三个选项:select_by_index(2)51Testing软件测试网 I;R\+b RD.\Q o

.N6QVI3?'^-^X J051Testing软件测试网 Y)K3uJn8Qx$YFU

)DEN}+`*h/N0五、Select模块(value)

xZ"W ekdY`s051Testing软件测试网X-Y6W*@ Z/Hl

    1.Select模块里面除了index的方法,还有一个方法,通过选项的value值来定位。每个选项,都有对应的value值,如51Testing软件测试网Y` p!tl\iXT

<selectid="nr"name="NR"><optionselected=""value="10">每页显示10条</option><optionvalue="20">每页显示20条</option><optionvalue="50">每页显示50条</option></select>
51Testing软件测试网$m\/J8r(iHP!k!\g

   2.第二个选项对应的value值就是"20":select_by_value("20")51Testing软件测试网X7I${O9S

51Testing软件测试网V-i+hy5e2~ y

0L%O+e;O h.s5w0

/R(sS!`l"c m7N0 

H Tf0d?i3J"[:ODj051Testing软件测试网)q!SN}!\D

六、Select模块(text)
6M#k7{]f7C0    1.Select模块里面还有一个更加高级的功能,可以直接通过选项的文本内容来定位。
{4`"}$K!KE|)C\0    2.定位“每页显示50条”:select_by_visible_text("每页显示50条")

iPAj t$\0

Anf-OLo{r{$v0

|%_O`Y9lbfU051Testing软件测试网k e:S#x!K K7u8h@

 

h7r-ct!Z0

au sg'p5v0七、Select模块其它方法
;`I5Reb"{5v^c0    1.select里面方法除了上面介绍的三种,还有更多的功能如下:

Ji&{ vL051Testing软件测试网J-G4KX F \

)JHa(\sX*UK0

$Tb@^}s!K Lp0select_by_index()  :通过索引定位51Testing软件测试网$DNU*\Z8\R h u
select_by_value()  :通过value值定位
hI_8~7d*J#M n5T0select_by_visible_text() :通过文本值定位
}3T(v0^V X-w0deselect_all()          :取消所有选项
E G9T F|E'w0deselect_by_index()     :取消对应index选项51Testing软件测试网%?;`U:wd'P%L;Vy+mt
deselect_by_value()      :取消对应value选项51Testing软件测试网9T[4M%vk'U3S
deselect_by_visible_text() :取消对应文本选项51Testing软件测试网:V6W[T$Q~6L8M:f

51Testing软件测试网S)K:oOZ

first_selected_option()  :返回第一个选项
Y(Iln:U F+X8?N0all_selected_options()   :返回所有的选项51Testing软件测试网T$K;McveB

t ]q&wOC5z~3J0 51Testing软件测试网&a;DD$~6c ~

51Testing软件测试网 q!d4yu+x-LhWq{iD

八、整理代码如下:51Testing软件测试网Yr$eo ~j

复制代码
#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