当前位置: 首页 > 工具软件 > Splinter > 使用案例 >

splinter API 页面元素查找API

李俊雅
2023-12-01

这里整理一些有关查找页面元的API,这些api的返回值是一个列表,

如果要对返回结果操作,比如点击,填值等,则要通过[x]下标。

 

1,find_by_id(id)

通过id在当前页面中查找元素

 

例子:查找id为 task_name的元素,并填入 测试二

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

bs = Browser('chrome')

bs.visit('http://www.sterson.com.cn/test')

time.sleep(1)

bs.find_by_id('task_name')[0].fill(u'测试二')

 

 

2,find_by_name(name)

通过name在当前页面中查找元素

 

例子:查找name为 task_name 的元素,并填入 测试三

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

 

bs = Browser('chrome')

bs.visit('http://www.sterson.com.cn/test')

time.sleep(1)

bs.find_by_name('task_name')[0].fill(u'测试三')

 

 

3,find_by_text(text)

通过text(文本)查找当前页面元素

 

例子:查找页面中是否有 新增任务 的文本

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

 

bs = Browser('chrome')

bs.visit('http://www.sterson.com.cn/test')

time.sleep(1)

if bs.find_by_text('新增任务'):

    print 'ok'

else:

    print 'no'

 

4,find_by_xpath(xpath, original_find=None, original_query=None)

通过xpath查找页面元素

 

//*:匹配所有元素:

find_by_xpath (“//*[contains(., ‘你好’)]”)

find_by_xpath("//*[starts-with(@id, 'JGTextBox1')]")  ID值以JGTextBox1开头的元素

 

//a:匹配超级连接:

find_by_xpath('//a[text()="%s"]' % text)    

find_by_xpath ('//a[@href="%s"]' % href)

 

//button:匹配按钮: 

find_by_xpath("//button[text()='登录']”)  文本为“登录”的按钮

 

//option:匹配单选按钮:

find_by_xpath('//option[@value="%s"]' % value)

 

//select:匹配下拉选择

find_by_xpath('//select[@name="%s"]/option[@value="%s"]' % (name, value))

 

例子:

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

bs = Browser('chrome')

bs.visit('http://sterson.qicp.vip/test')

time.sleep(1)

bs.find_by_xpath('//*[@id="task_describe"]')[0].fill(u'这是一个测试任务')

 

 

5,find_link_by_text(text)

通过text文本查找页上的链接

 

例子:

# -*- coding: utf-8 -*-

from splinter.browser import Browser

import time

 

bs = Browser('chrome')

bs.visit('http://www.sterson.com.cn/test')

time.sleep(1)

bs.find_link_by_text('李老道自学网')[0].click()

更多自动化测试资料欢迎浏览李老道自学网:http://www.sterson.com.cn/

 类似资料: