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

Selenium之元素定位

时间:2019-01-16 21:46:41      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:inf   元素   web   com   相对   webdriver   nts   [1]   span   

1.查看页面元素:ID、class、type、name等。

技术分享图片

2.通过webdriver的方法定位:

技术分享图片

find_element_by_name() 
find_element_by_id() 
find_element_by_class_name() 

技术分享图片

 

browser.find_element_by_xpath();

绝对路径:find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input")

相对路径:

find_element_by_xpath("//input[@id=‘kw‘]")

find_element_by_xpath("//*[@name=‘wd‘]")

find_element_by_css_selector() 

一般class是用.标记,id是用#标记,标签名直接写具体标签名就好了

技术分享图片

find_element_by_link_text()
find_element_by_partial_link_text()

技术分享图片

3.通过By定位:

find_element(By.ID,"kw")
find_element(By.NAME,"wd")
find_element(By.CLASS_NAME,"s_ipt")
find_element(By.TAG_NAME,"input")
find_element(By.LINK_TEXT,u"新闻")
find_element(By.PARTIAL_LINK_TEXT,u"新")
find_element(By.XPATH,"//*[@class=‘bg s_btn‘]")
find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su") 
前提是导入:from selenium.webdriver.common.by import By 

 

4.JS定位:

id定位:document.getElementById()
name定位:document.getElementsByName()
tag定位:document.getElementsByTagName()
class定位:document.getElementsByClassName()
css定位:document.querySelectorAll()

 

Selenium之元素定位

标签:inf   元素   web   com   相对   webdriver   nts   [1]   span   

原文地址:https://www.cnblogs.com/lianstyle/p/10279261.html

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