2.33 定位的坑:class属性有空格
51Testing软件测试网Y!b@:QP4x前言
~9nSig2Y1R)t
L0有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted
k2l O9_q/d0这个报错意思是说定位语法错了。
n9Kz7k6]sX{w)c0
4b:pQ3pr/{@-L
n7Q0一、定位带空格的class属性
#Dbd&OT`S01.以126邮箱为例:http://mail.126.com/,定位账号输入框51Testing软件测试网 j
qtph
51Testing软件测试网|,W:S-ZYt#GdmDi
:[{-^&T5qM]B*R3AP02.如果直接复制过来用class属性定位是会报错的
!A'\8[
v
P]0k9aL'CM'Pn(N T0
(c7@
@ @
O]$S0
9YP
S9x,R-_.JB)sm0
{/a IC a-\IA051Testing软件测试网U%uQo!G4?(}F+z二、class属性科普51Testing软件测试网ctT1i;~e)W/k
1.class属性中间的空格并不是空字符串,那是间隔符号,表示的是一个元素有多个class的属性名称,在整个HTML文档,使用CSS中的同一个class类可能是一个或多个!51Testing软件测试网$Mr9m x.m a:e
(class属性是比较特殊的一个,除了这个有多个属性外,其它的像name,id是没多个属性的)
bKE:m4V
eD|0m0j9`zp?$pR@1r]4y02.想补习html基础知识的可以参考菜鸟教程:http://www.runoob.com/html/html-attributes.html
iAZ^4I*UO051Testing软件测试网 g6T&B6ip:{ lI
c#K2T5o.r K ~]0三、class定位51Testing软件测试网&q]D$qB)v?g:O1[
1.既然知道class属性有空格是多个属性了,那定位的时候取其中的一个就行(并且要唯一),也就是说class="j-inputtext dlemail",取j-inputtext 和dlemail都是可以的,这样这个class属性在页面上唯一就行
2U$psG\051Testing软件测试网-y-k@dE'\2.那么问题来了:如何才知道这个元素的某个属性是不是在页面上是唯一的呢?
s"DWL)ZM(]0;sU/?gM&Zc0 51Testing软件测试网7ssT_%UC6XT
四、判断元素唯一性51Testing软件测试网v?"AC0M9f
1.F12切换到HTML界面,在搜索框输入关键字搜索,如:j-inputtext,然后按回车搜索,看页面上有几个class属性中有j-inputtext这个属性的,就知道是不是唯一的了。
L4Z
k4S"{-Pl051Testing软件测试网2O-yM
]1LE(PA51Testing软件测试网1t&Jz2R;l8Y:x#yDr*V
51Testing软件测试网,J7V~4z8[:lFO/O
51Testing软件测试网R\+sR9qynZIa五、class属性不唯一怎么办
d{`bi&x7GgW
wI01.如果这个class的多个属性都不是唯一的咋办呢,元素不唯一也不用怕,可以用复数定位,把所有的相同元素定位出来,按下标取第几个就行。
l;@1o7w-B]Z051Testing软件测试网)V*@!J*w(O
51Testing软件测试网\J"_0_\x六、css定位
A0D:x;j1[m|01.css来定位class属性的元素前面加个点(.)就行,然后空格变成点(.)就能定位了51Testing软件测试网4C e.@8a o+O
2.当然css也可以取class属性的其中一个属性(页面上唯一的)来定位,定位方法是灵活多变的
]$O]~
?fSk051Testing软件测试网(P7Zw#fjmB7~?*J
s"P2^r7X/m051Testing软件测试网:_&XzY![jB'@
51Testing软件测试网F2K`\-|V u ya七、参考代码51Testing软件测试网|3I:k
wL`
# coding:utf-8
v;{ koF$E0j1eKm0fromseleniumimportwebdriver
*M1gR:J
r\r;C6Z0
B:a!X*V+s
Qk5W"C0driver = webdriver.Firefox()51Testing软件测试网q5@'W)Ots
driver.get("http://mail.126.com/")51Testing软件测试网"WaVm)zm2i#f}
driver.switch_to.frame("x-URS-iframe")51Testing软件测试网*Le8}l\R3w
w~#c!c
# 方法一:取单个class属性51Testing软件测试网*R8Tb~3i
driver.find_element_by_class_name("dlemail").send_keys("yoyo")
o*ONfU L
Z6z N+nLz0driver.find_element_by_class_name("dlpwd").send_keys("12333")51Testing软件测试网6lv9g;C+_t4k?%j/Ks
# 方法二:定位一组取下标定位(乃下策)51Testing软件测试网 wU1rA;CRm2c
w
# driver.find_elements_by_class_name("j-inputtext")[0].send_keys("yoyo")
%u'Y6fY:qF6Q,}:|0# driver.find_elements_by_class_name("j-inputtext")[1].send_keys("12333")51Testing软件测试网#E*U'nc5E)G8c
# 方法三:css定位
`'[#T,bqq)zZA0# driver.find_element_by_css_selector(".j-inputtext.dlemail").send_keys("yoyo")
'K4KcVY(|;X{0# driver.find_element_by_css_selector(".j-inputtext.dlpwd").send_keys("123")51Testing软件测试网X3@1F?xhmbC
# 方法四:取单个class属性也是可以的
6X-e'ZC/C1D9]0# driver.find_element_by_css_selector(".dlemail").send_keys("yoyo")
9a%~ H M%LB7w0# driver.find_element_by_css_selector(".dlpwd").send_keys("123")