selenium的定位方式
2020-06-30 本文已影响0人
Lydia1991
selenium有18种定位方式,8种单数,8种复数,2种父类,最后两种是前面这16种的底层封装。
8种单数定位方式
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get('https://www.xxxxx.com/')
# 最大化浏览器
driver.maximize_window()
# 定位到页面顶部
js='window.scrollTo(0,0)'
# 1、id定位
al = driver.find_element_by_id('i1')
al.send_keys('id定位')
# 2、class定位
bl = driver.find_element_by_class_name('classname')
bl.send_keys('class定位')
# 3、css selector定位
cl = driver.find_element_by_css_selector('#i1')
cl.send_keys('css selector定位')
# 4、name定位:按照标签中的name属性定位
dl = driver.find_element_by_name('name')
dl.send_keys('name定位')
# 5、link_text 文本内容定位
import time
time.sleep(1)
driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面
time.sleep(1)
el = driver.find_element_by_link_text('跳转大师兄博客地址')
el.click()
# 6、partial_link_text 模糊文本内容匹配
import time
time.sleep(1)
driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面
time.sleep(1)
fl = driver.find_element_by_partial_link_text('跳转')
fl.click()
# 7、Xpath 定位
gl = driver.find_element_by_xpath('//input[@placeholder="请通过XPATH定位元素"]')
gl.send_keys('xpath 定位')
# 8、tag_name 标签名定位
hl = driver.find_element_by_tag_name('input')
hl.send_keys('tag_name 标签名定位')
8种复数定位方式
# 1、id定位
als = driver.find_elements_by_id('i1')
als[0].send_keys('id定位')
# 2、class定位
bls = driver.find_elements_by_class_name('classname')
bls[0].send_keys('class定位')
# 3、css selector定位
cls = driver.find_elements_by_css_selector('input')
cls[2].send_keys('定位到第三个input输入框')
# 4、name定位:按照标签中的name属性定位
dls = driver.find_elements_by_name('name')
dls[0].send_keys('name定位')
# 5、link_text 文本内容定位
import time
time.sleep(1)
driver.execute_script('window.scrollTo(0,0);') # 跳转到网页的最上面
time.sleep(1)
els = driver.find_elements_by_link_text('跳转大师兄博客地址')
els[0].click()
# 6、partial_link_text 模糊文本内容匹配
import time
time.sleep(1)
driver.execute_script('window.scrollTo(0,0)') # 跳转到网页的最上面
time.sleep(1)
fls = driver.find_elements_by_partial_link_text('示例')
fls[0].click()
# 7、Xpath 定位
gls = driver.find_elements_by_xpath('//input')
gls[4].send_keys('xpath 定位')
# 8、tag_name 标签名定位
hls = driver.find_elements_by_tag_name('input')
hls[5].send_keys('tag_name 标签名定位')
最后两种是前面16种的封装:
1 driver.find_element('id','i1')
2 driver.find_elements('xpath','//input')
以上两种定位方式,第一个参数填入下面8个字符串,分别对应上面的16种定位方式,单数和复数分别对应
"id"
"xpath"
"link text"
"partial link text"
"name"
"tag name"
"class name"
"css selector"
下面举例说明:
ali = driver.find_element('id','i1')
ali.send_keys('xxxx')
# 相当于:
al = driver.find_element_by_id('i1')
al.send_keys('xxxx')
bls = driver.find_elements('css selector','input')
bls[1].send_keys('css selector')
# 相当于:
bl = driver.find_elements_by_css_selector('input')
bl[1].send_keys('css selector')
总结:
我们工作当中应该优先使用Css Selector定位,如果定位不到再用Xpath定位。因为查看源代码发现:使用id、name、tag name、class name定位,都会转换成css selector定位,具体源代码如下:
def find_element(self, by=By.ID, value=None):
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENT, {
'using': by,
'value': value})['value']
def find_elements(self, by=By.ID, value=None):
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENTS, {
'using': by,
'value': value})['value'] or []