1. 程式人生 > >selenium采用xpath方法識別頁面元素

selenium采用xpath方法識別頁面元素

元素 技術分享 試用 chrome http 搜索 方法 sub com

  有些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方法識別頁面元素