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做例子分析 css

<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']")#相對徑+屬性值來定位

測試: html

#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

總結: python

在網頁結構簡單的狀況下,能夠經過name,tag等屬性定位,但要精肯定位更多的仍是用cssSelector, xpath來定位,經過增長屬性值來區分,若是實在太懶只有經過Firefox firebug插件直接定位,copy/paste不亦樂乎,而不用本身去學習css,xpath web

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

username_css:html body form#loginForm input
顯然這不是最優的,由於這是絕對路徑,只要網頁稍一改動,腳本就做廢,爲了更加靈活,仍是須要相對路徑來描述 測試

右擊input能夠選擇copy xpath或者copy css path this

相關文章
相關標籤/搜索