Selenium Webdriver (2)-- Locate Elements

Selenium提供了方法来查找定位元素
find_element_by_id("id")                            #通过id来查找元素
find_element_by_name("name")                        #通过name属性来查找元素
find_element_by_class_name("class_name")            #通过class属性来查找元素
find_element_by_css_selector("locator")             #通过css属性来查找元素
find_element_by_link_text("link")                        #通过链接名属性
find_element_by_partial_link_text("partial_link")        #通过模糊匹配链接名来查找
find_element_by_tag("tag")                          #通过tag属性来查找
find_element_by_xpath("xpath")                      #通过xpath来查找
------------------------------------------------
查找一组元素返回一个列表 
find_elements_by_name("name")
find_elements_by_class_name("class_name")
find_elements_by_css_selector("locator")
find_elements_by_link("link")
find_elements_by_partial_link("partial_link")
find_elements_by_tag("tag")

用login.html作例子分析

<html>
  <body>
    <h1>Welcome</h1>
    <p class="content">Site content goes here.</p>
    <p>Are you want to do this</p>
    <a href="continue.html">Continue</a>
      <form id="loginForm">
	<input name="username" type="text"/>
	<input name="password" type="password"/>
	<input name="continue" type="submit" value="Login" />
 	<input name="continue" type="button" value="Clear" />
    </form>
  </body>
</html>

通过find_element定位元素
login_form = driver.find_element_by_id('loginForm')       #定位form元素
username = driver.find_element_by_name('username')        #name定位username输入框
heading1 = driver.find_element_by_tag_name('h1')          #定位h1的Welcome
content = driver.find_element_by_class_name("content")    #定位Site content goes here.
link = driver.find_element_by_link_text("Continue")            #定位Continue超链接
partial_link = driver.find_element_by_partial_link_text("Con") #定位Continue超链接,是部分匹配
----------------------------------------------------------------------------------------------
xpath:
login_form = driver.find_element_by_xpath("/html/body/form[1]")                            #绝对路径
login_form = driver.find_element_by_xpath("//form[1]")                                     #相对路径第一个form
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")                       #相对路径@属性标识
username = driver.find_element_by_xpath("//form[input/@name='username']")                  #相对路径跟子节点加属性
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")                
username = driver.find_element_by_xpath("//input[@name='username']")
clear_button = driver.find_element_by_xpath("//form[1]/input[4]")
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")   #相对路径跟多个属性加以区分
-----------------------------------------------------------------------------------------------
css:
login_form = driver.find_element_by_css_selector("html>body>form")                         #绝对路径
login_form = driver.find_element_by_css_selector("form")                                   #相对路径
login_form = driver.find_element_by_css_selector("form#loginForm")                         #相对路径id选择器
content = driver.find_element_by_css_selector("p.content")                                 #标签+class值
clear_button = driver.find_element_by_css_selector("input[name='continue'][type='button']")#相对径+属性值来定位

测试:

#encoding:utf-8
#Locating Elements
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

class search_python_in_baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        
    def test_search_python(self):
        driver = self.driver
        driver.get("C:\work\python\selenium\html\login.html")
        login_form_by_id = driver.find_element_by_id('loginForm') #定位form元素
        username_by_name = driver.find_element_by_name('username')                                                          #name定位username输入框
        heading_by_tag_name = driver.find_element_by_tag_name('h1')                                                         #定位h1的Welcome
        content_by_class_name= driver.find_element_by_class_name("content")                                                 #定位Site content goes here.
        link = driver.find_element_by_link_text("Continue")                                                                 #定位Continue超链接
        partial_link = driver.find_element_by_partial_link_text("Con")                                                      #定位Continue超链接,是部分匹配
        time.sleep(10)
        #css
        heading_by_css = driver.find_element_by_css_selector("html>body>h1")                                                #绝对路径
        username_by_css = driver.find_element_by_css_selector("input")                                                      #相对路径
        login_form_by_css = driver.find_element_by_css_selector("form#loginForm")                                           #相对路径id选择器
        content_by_css = driver.find_element_by_css_selector("p.content")                                                   #标签+class值
        clear_button_by_css = driver.find_element_by_css_selector("input[name='continue'][type='button']")                  #相对径+属性值来定位      
       #xpath
        heading_by_xpath = driver.find_element_by_xpath("/html/body/h1[1]")                                                 #绝对路径
        username_by_xpath = driver.find_element_by_xpath("//input[1]")                                                      #相对路径第一个form
        login_form_by_xpath = driver.find_element_by_xpath("//form[@id='loginForm']")                                       #相对路径@属性标识
        content_by_xpath = driver.find_element_by_xpath("//p[@class='content']")                                            #相对路径跟子节点加属性
        username_by_xpath2 = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")                
        username_by_xpath3 = driver.find_element_by_xpath("//input[@name='username']")
        clear_button_by_xpath1 = driver.find_element_by_xpath("//form[1]/input[4]")
        clear_button_xpath = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")                     #相对路径跟多个属性加以区分        
        if login_form_by_id == login_form_by_css and  login_form_by_id == login_form_by_xpath:
            print "login_form is true"
        if username_by_name == username_by_css and username_by_name == username_by_xpath:
            print "user name is true"
        if content_by_class_name==content_by_css and content_by_class_name==content_by_xpath:
            print 'content is true'
        if heading_by_tag_name==heading_by_css and heading_by_tag_name == heading_by_xpath:
            print 'heading is true'
        if clear_button_by_css == clear_button_xpath:
            print 'clear_button is true'
    def tearDown(self):
        self.driver.close()
        
if __name__ == '__main__':
    unittest.main()
-------------------------------------------------------------------------------------------
login_form is true
user name is true
content is true
heading is true
clear_button is true
.
----------------------------------------------------------------------
Ran 1 test in 13.780s

OK

总结:

在网页结构简单的情况下,可以通过name,tag等属性定位,但要精确定位更多的还是用cssSelector, xpath来定位,通过增加属性值来区分,如果实在太懒只有通过Firefox firebug插件直接定位,copy/paste不亦乐乎,而不用自己去学习css,xpath

username_xpath:/html/body/form/input[1]

username_css:html body form#loginForm input
显然这不是最优的,因为这是绝对路径,只要网页稍一改动,脚本就作废,为了更加灵活,还是需要相对路径来描述

右击input可以选择copy xpath或者copy css path


转载于:https://my.oschina.net/hding/blog/599896

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值