webdriver定位页面元素的8种方式

webdriver定位页面元素的8种方式

find_element_by_id()
find_element_by_name()
find_element_by_class_name()

find_element_by_tag_name() #标签
find_element_by_link_text() #文本链接
find_element_by_partial_link_text()#部分文本链接
'''以下是重点并且是难点'''
find_element_by_xpath() #应用很多
find_element_by_css_selector() 

用法以百度为例,以下全为定位页面元素8种方式的用法:

<span class="bg s_ipt_wr quickdelete-wrap">
	<span class="soutu-btn"></span>
	<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
	<a href="javascript:;" id="quickdelete" title="清空" class="quickdelete" style="top: 0px; right: 0px; display: none;"></a>
</span>

find_element_by_id(“kw”)
find_element_by_name(“wd”)
find_element_by_class_name(“s_ipt”)
find_element_by_tag_name(“input”)

以百度右上角文本链接为例

<div id="u1">
	<a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
	<a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
	<a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a>
	<a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a>
	<a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">贴吧</a>
	<a href="http://xueshu.baidu.com" name="tj_trxueshu" class="mnav">学术</a>
	<a href="https://passport.baidu.com/v2/?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" οnclick="return false;">登录</a>
	<a href="http://www.baidu.com/gaoji/preferences.html" name="tj_settingicon" class="pf">设置</a><a href="http://www.baidu.com/more/" name="tj_briicon" class="bri" style="display: block;">更多产品</a>
	<a href="http://www.suoha.com">超级长的一个文本链接啦啦啦啦</a>
</div>

find_element_by_link_text(“新闻”)
find_element_by_partial_link_text(“长的一个”)

<span class="bg s_ipt_wr quickdelete-wrap">
	<span class="soutu-btn"></span>
	<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
	<a href="javascript:;" id="quickdelete" title="清空" class="quickdelete" style="top: 0px; right: 0px; display: none;"></a>
</span>

find_element_by_xpath(“//[@id=‘kw’]”)
find_element_by_xpath(’//
[@id=“kw”]’)
注:以上两个xpath的用法在Python中外面用了双引号,里面的字符串就要用单引号,外面用了单引号,里面就要用双引号

find_element_by_xpath(“//*[@name=‘wd’]”)
find_element_by_xpath(“//input[@maxlength=‘255’]”)
find_element_by_xpath(“//form[@id=‘form’]/span/input”)
find_element_by_xpath(“/html/body/div[3]/div/div/div/div/form/span/input”)

<input id="kw" name="wd"> #需要找到这个元素
<input id="kw" name="ll">
<input id="hh" name="wd">

find_element_by_xpath(“//input[@id=‘kw’ and @name=‘wd’]”)

<span class="bg s_ipt_wr quickdelete-wrap">
	<span class="soutu-btn"></span>
	<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
	<a href="javascript:;" id="quickdelete" title="清空" class="quickdelete" style="top: 0px; right: 0px; display: none;"></a>
</span>

find_element_by_css_selector("#kw") 在CSS种#代表id
find_element_by_css_selector("[name=‘wd’]")
find_element_by_css_selector("[name=wd]")
find_element_by_css_selector(".s_ipt") 在CSS中.代表class
以下写路径:
find_element_by_css_selector(".bg > input")
find_element_by_css_selector(“span.bg > input#kw”)
find_element_by_css_selector(“form.form > span.bg > input#kw”)

在火狐浏览器中使用firebug中的FirePath可以直接生成CSS的元素语法
高版本的火狐貌似右键复制就可以生成元素语法
在谷歌中,直接右键复制xpath或者右键复制CSS也可以直接生成元素语法

发布了24 篇原创文章 · 获赞 0 · 访问量 564

猜你喜欢

转载自blog.csdn.net/MoLi_D/article/details/104124814
今日推荐