码迷,mamicode.com
首页 > 其他好文 > 详细

selenium采用xpath方法识别页面元素

时间:2018-01-20 22:42:29      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:post   htm   技术   val   ima   elf   inpu   browser   浏览器   

  有些HTML页面中的元素中属性较少,经常有找不到id、class、name等常用属性的时候,这个时候xpath、css就能很好的识别到我们的元素。

  Firefox和chrome浏览器中均有xpath、css插件工具。

  以下为通过xpath方法写的测试用例:

 1     def test_xpath(self):
 2         u‘‘‘采用xpath识别元素‘‘‘
 3         self.browser.find_element_by_xpath(".//*[@id=‘kw‘]").send_keys("xpath test")        #采用id,.//input[@id=‘kw‘]
 4         self.browser.find_element_by_xpath(".//*[@id=‘su‘]").submit()           #采用id
 5         log.info("采用xpath识别页面中的属性,[id]")
 6         time.sleep(1)
 7         self.browser.find_element_by_xpath(".//*[@name=‘wd‘]").clear()  # 清空原关键字        #采用name,.//input[@name=‘wd‘]
 8         self.browser.find_element_by_xpath(".//*[@class=‘s_ipt‘]").send_keys("selenium auto test")        #采用class,.//input[@class=‘s_ipt‘]
 9         #self.browser.find_element_by_xpath(".//*[@type=‘submit‘]").submit()     #采用type,.//input[@type=‘submit‘]
10         self.browser.find_element_by_xpath("//form[@id=‘form‘]/span/input[@value=‘百度一下‘]").submit()    #提交搜索
11         log.info("采用xpath识别页面中的属性,[class、type]")
12         ‘‘‘
13             .//*[@id=‘kw‘]
14             .//*[@name=‘wd‘]
15             .//*[@class=‘s_ipt‘]
16             .//*[@autocomplete=‘off‘]
17             .//*[@type=‘submit‘]
18             .//input[@autocomplete=‘off‘]
19             .//input[ @ type = ‘submit‘]
20             //form[@id=‘form‘]/span/input[@value=‘百度一下‘]
21         ‘‘‘

 

技术分享图片

 

 

 技术分享图片

 

 技术分享图片

 

  

selenium采用xpath方法识别页面元素

标签:post   htm   技术   val   ima   elf   inpu   browser   浏览器   

原文地址:https://www.cnblogs.com/zhuque/p/8321999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!