selenium - CSS selector定位方法

不管用什么方式查找元素,都要保证在页面上查找到唯一元素。除非想要查找一批元素。
选择器有三种:id, 标签名, class。 在CSS里# 代表id, .(点)代表class.

  1. Id定位:
<input name="fm-login-id" type="text" class="fm-text" id="fm-login-id" tabindex="1" aria-label="邮箱/会员名/8位ID" placeholder="邮箱/会员名/8位ID" autocapitalize="off" data-spm-anchor-id="a212t0.3047821.0.i0.1e2479f4VaAq1G">

有如上元素。
a. 该元素可定义成:
input[id=‘fm-login-id’],也可简写成#fm-login-id。
b. 如果再加上标签名如:input#fm-login-id,则搜索时将只搜索此类标签,可以使搜索速度更快

  1. class定位:
<i class="wind-rc-product-icon glyph-actiontrail"></i>

a. 当class的值有多个时,可以取其中任何与其他元素的class值不同的那个(唯一的那个),可以定义成 .wind-rc-product-icon.glyph-actiontrail
b. 也可以加上标签名字,定义成 i.glyph-actiontrail
c. 也可以加上所有class的值, 即:i.glyph-actiontrail.wind-rc-product-icon

  1. 通配符定位:^, *,$
 <span class="sc-Axmtr hvJMgY" data-spm-anchor-id="5176.12818093.nav-right.i0.65fe16d0BaXZ3I">访问控制</span>

a. span[class^=‘sc-Axm’], 查找以这个文本开头的元素。
b. span[class$=‘Axmtr’], 查找以这个文本结尾的元素。
c. span[class*=‘Axm’], 查找包含这个文本的元素

  1. 定位子节点
    a.
    在这里插入图片描述
    div>a.sc-AxhUy 通过上层父元素div和class定位元素。

b.
在这里插入图片描述
div>input[name=‘up-input’], 通过上层父元素div和内层元素属性定位元素。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<TABLE border="0" cellSpacing="0" cellPadding="0" width="950" height="36"> <TBODY> <TR> <TD> <IMG alt="" src="http://img02.taobaocdn.com/imgextra/i2/792030784/T22YadXhpaXXXXXXXX_!!792030784.jpg" width="111" height="36" /> </TD> <TD> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG alt="" src="http://img01.taobaocdn.com/imgextra/i1/792030784/T24badXhpaXXXXXXXX_!!792030784.jpg" width="64" height="36" /></A> </TD> <TD> <DIV 85px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG border="0" alt="" src="http://img01.taobaocdn.com/imgextra/i1/792030784/T28badXhhaXXXXXXXX_!!792030784.jpg" width="85" height="72" /></A> </DIV> </TD> <TD> <DIV 115px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img03.taobaocdn.com/imgextra/i3/792030784/T2abedXhXaXXXXXXXX_!!792030784.jpg" width="115" height="72" /></A> </DIV> </TD> <TD> <DIV 86px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img03.taobaocdn.com/imgextra/i3/792030784/T22radXhtaXXXXXXXX_!!792030784.jpg" width="86" height="72" /></A> </DIV> </TD> <TD> <DIV 58px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img02.taobaocdn.com/imgextra/i2/792030784/T27badXhhaXXXXXXXX_!!792030784.jpg" width="58" height="72" /></A> </DIV> </TD> <TD> <DIV 64px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img01.taobaocdn.com/imgextra/i1/792030784/T28YadXhhaXXXXXXXX_!!792030784.jpg" width="64" height="72" /></A> </DIV> </TD> <TD> <DIV 59px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img01.taobaocdn.com/imgextra/i1/792030784/T29radXhdaXXXXXXXX_!!792030784.jpg" width="59" height="72" /></A> </DIV> </TD> <TD> <DIV 93px; HEIGHT: 36px; OVERFLOW: hidden" class="dtcd"> <A href="http://aituaihua.taobao.com/" target="_blank" data-spm-anchor-id="11049LRU.2-aS228.3-5aPtV9"><IMG class="selected" border="0" alt="" src="http://img04.taobaocdn.com/imgextra/i4/792030784/T2gredXg4aXXXXXXXX_!!792030784.jpg" width="93" height="72" /></A> </DIV> </TD> <TD height="36" background="http://img03.taobaocdn.com/imgextra/i3/792030784/T2DrudXapaXXXXXXXX_!!792030784.jpg" width="215"> <DIV left; PADDING-LEFT: 20px; COLOR: #686f46">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值