Selenium学习之==>18种定位方式的使用
Selenium的定位方式一共有18种,单数8种,复数8种,最后两种是前面这16种的底层封装
单数形式8种
1 # 1、id定位 2 al = driver.find_element_by_id('i1') 3 al.send_keys('id定位') 4 5 # 2、class定位 6 bl = driver.find_element_by_class_name('classname') 7 bl.send_keys('class定位') 8 9 # 3、css selector定位 10 cl = driver.find_element_by_css_selector('#i1') 11 cl.send_keys('css selector定位') 12 13 # 4、name定位:按照标签中的name属性定位 14 dl = driver.find_element_by_name('name') 15 dl.send_keys('name定位') 16 17 # 5、link_text 文本内容定位 18 import time 19 time.sleep(1) 20 driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面 21 time.sleep(1) 22 el = driver.find_element_by_link_text('跳转大师兄博客地址') 23 el.click() 24 25 # 6、partial_link_text 模糊文本内容匹配 26 import time 27 time.sleep(1) 28 driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面 29 time.sleep(1) 30 fl = driver.find_element_by_partial_link_text('跳转') 31 fl.click() 32 33 # 7、Xpath 定位 34 gl = driver.find_element_by_xpath('//input[@placeholder="请通过XPATH定位元素"]') 35 gl.send_keys('xpath 定位') 36 37 # 8、tag_name 标签名定位 38 hl = driver.find_element_by_tag_name('input') 39 hl.send_keys('tag_name 标签名定位')
复数形式也是8种,是以上8种的复数形式,返回的是一个列表
1 # 1、id定位 2 als = driver.find_elements_by_id('i1') 3 als[0].send_keys('id定位') 4 5 # 2、class定位 6 bls = driver.find_elements_by_class_name('classname') 7 bls[0].send_keys('class定位') 8 9 # 3、css selector定位 10 cls = driver.find_elements_by_css_selector('input') 11 cls[2].send_keys('定位到第三个input输入框') 12 13 # 4、name定位:按照标签中的name属性定位 14 dls = driver.find_elements_by_name('name') 15 dls[0].send_keys('name定位') 16 17 # 5、link_text 文本内容定位 18 import time 19 time.sleep(1) 20 driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面 21 time.sleep(1) 22 els = driver.find_elements_by_link_text('跳转大师兄博客地址') 23 els[0].click() 24 25 # 6、partial_link_text 模糊文本内容匹配 26 import time 27 time.sleep(1) 28 driver.execute_script('window.scrollTo(0,0)') # 跳转到网页的最上面 29 time.sleep(1) 30 fls = driver.find_elements_by_partial_link_text('示例') 31 fls[0].click() 32 33 # 7、Xpath 定位 34 gls = driver.find_elements_by_xpath('//input') 35 gls[4].send_keys('xpath 定位') 36 37 # 8、tag_name 标签名定位 38 hls = driver.find_elements_by_tag_name('input') 39 hls[5].send_keys('tag_name 标签名定位')
最后两种是前面16种的底层封装
1 driver.find_element('id','i1') 2 driver.find_elements('xpath','//input')
以上两种定位方式,第一个参数填入下面8个字符串,分别对应上面的16种定位方式,单数和复数分别对应
1 "id" 2 "xpath" 3 "link text" 4 "partial link text" 5 "name" 6 "tag name" 7 "class name" 8 "css selector"
下面举例说明:
1 ali = driver.find_element('id','i1') 2 ali.send_keys('xxxx') 3 # 相当于: 4 al = driver.find_element_by_id('i1') 5 al.send_keys('xxxx') 6 7 bls = driver.find_elements('css selector','input') 8 bls[1].send_keys('css selector') 9 # 相当于: 10 bl = driver.find_elements_by_css_selector('input') 11 bl[1].send_keys('css selector')
总结:
我们工作当中应该优先使用Css Selector定位,如果定位不到再用Xpath定位。因为查看源代码发现:使用id、name、tag name、class name定位,都会转换成css selector定位,具体源代码如下:
1 def find_element(self, by=By.ID, value=None): 2 if self.w3c: 3 if by == By.ID: 4 by = By.CSS_SELECTOR 5 value = '[id="%s"]' % value 6 elif by == By.TAG_NAME: 7 by = By.CSS_SELECTOR 8 elif by == By.CLASS_NAME: 9 by = By.CSS_SELECTOR 10 value = ".%s" % value 11 elif by == By.NAME: 12 by = By.CSS_SELECTOR 13 value = '[name="%s"]' % value 14 return self.execute(Command.FIND_ELEMENT, { 15 'using': by, 16 'value': value})['value'] 17 18 def find_elements(self, by=By.ID, value=None): 19 if self.w3c: 20 if by == By.ID: 21 by = By.CSS_SELECTOR 22 value = '[id="%s"]' % value 23 elif by == By.TAG_NAME: 24 by = By.CSS_SELECTOR 25 elif by == By.CLASS_NAME: 26 by = By.CSS_SELECTOR 27 value = ".%s" % value 28 elif by == By.NAME: 29 by = By.CSS_SELECTOR 30 value = '[name="%s"]' % value 31 return self.execute(Command.FIND_ELEMENTS, { 32 'using': by, 33 'value': value})['value'] or []