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>
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