浏览器操控
ssf.Browser
模块提供了一系列浏览器自动化操作的功能,如点击、滚动、获取页面元素等。
ssf.Browser
注意此功能需要预先安装插件 插件地址为(需要翻墙)https://chromewebstore.google.com/detail/ssf-%E8%87%AA%E5%8A%A8%E5%8C%96%E6%8F%92%E4%BB%B6/nlpkochpdcicaiogdlbionoheneabnom
标签页信息
定义了标签页的详细信息。
属性名 | 类型 | 描述 |
---|---|---|
active | boolean | 是否为活动标签页 |
audible | boolean | 是否有声音 |
autoDiscardable | boolean | 是否可以自动丢弃 |
discarded | boolean | 是否已丢弃 |
favIconUrl | string | 标签页的图标 URL |
groupId | number | 标签页所属的组 ID |
height | number | 标签页的高度 |
highlighted | boolean | 是否高亮 |
id | number | 标签页的唯一标识符 |
incognito | boolean | 是否为隐私模式 |
index | number | 标签页在窗口中的索引 |
lastAccessed | number | 上次访问的时间戳 |
mutedInfo | { muted: boolean } | 是否静音 |
pinned | boolean | 是否固定 |
selected | boolean | 是否选中 |
status | string | 标签页的状态 |
title | string | 标签页的标题 |
url | string | 标签页的 URL |
width | number | 标签页的宽度 |
windowId | number | 所属窗口的 ID |
ssf.Browser 模块
listen
方法
监听浏览器事件。
ssf.Browser.listen(): void;
示例
// 监听浏览器事件
ssf.Browser.listen();
click
方法
在指定标签页中点击元素。
ssf.Browser.click(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.click(1, '//button[@id="submit"]', 5000);
dblclick
方法
在指定标签页中双击元素。
ssf.Browser.dblclick(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.dblclick(1, '//input[@id="username"]', 5000);
mousedown
方法
在指定标签页中按下鼠标按钮。
ssf.Browser.mousedown(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.mousedown(1, '//div[@class="container"]', 5000);
mouseup
方法
在指定标签页中释放鼠标按钮。
ssf.Browser.mouseup(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.mouseup(1, '//div[@class="container"]', 5000);
contextmenu
方法
在指定标签页中右键点击元素。
ssf.Browser.contextmenu(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.contextmenu(1, '//a[@href="#"]', 5000);
mousemove
方法
在指定标签页中移动鼠标到元素。
ssf.Browser.mousemove(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.mousemove(1, '//span[@class="icon"]', 5000);
scrollVertically
方法
在指定标签页中垂直滚动页面。
ssf.Browser.scrollVertically(tabId: number, distance: number, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
distance | number | 滚动距离(正值向下滚动,负值向上滚动) |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.scrollVertically(1, 100, 5000);
scrollHorizontally
方法
在指定标签页中水平滚动页面。
ssf.Browser.scrollHorizontally(tabId: number, distance: number, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
distance | number | 滚动距离(正值向右滚动,负值向左滚动) |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.scrollHorizontally(1, -100, 5000);
scrollToElement
方法
在指定标签页中滚动到元素。
ssf.Browser.scrollToElement(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.scrollToElement(1, '//div[@id="content"]', 5000);
imageToBase64
方法
在指定标签页中将图片转换为 Base64 编码。
ssf.Browser.imageToBase64(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 图片元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.imageToBase64(1, '//img[@src="image.jpg"]', 5000);
getHTML
方法
在指定标签页中获取元素的 HTML 内容。
ssf.Browser.getHTML(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getHTML(1, '//div[@id="content"]', 5000);
getText
方法
在指定标签页中获取元素的文本内容。
ssf.Browser.getText(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getText(1, '//p[@class="description"]', 5000);
setText
方法
在指定标签页中设置元素的文本内容。
ssf.Browser.setText(tabId: number, xpath: string, text: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
text | string | 要设置的文本内容 |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.setText(1, '//input[@id="name"]', 'John Doe', 5000);
setHTML
方法
在指定标签页中设置元素的 HTML 内容。
ssf.Browser.setHTML(tabId: number, xpath: string, html: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
html | string | 要设置的 HTML 内容 |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.setHTML(1, '//div[@id="content"]', '<p>Hello, World!</p>', 5000);
getUrl
方法
在指定标签页中获取元素的 URL。
ssf.Browser.getUrl(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getUrl(1, '//a[@id="link"]', 5000);
getPosition
方法
在指定标签页中获取元素的位置。
ssf.Browser.getPosition(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getPosition(1, '//div[@id="content"]', 5000);
uploadFile
方法
在指定标签页中上传文件。
ssf.Browser.uploadFile(tabId: number, xpath: string, path: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 文件输入框的 XPath |
path | string | 要上传的文件路径 |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.uploadFile(1, '//input[@type="file"]', '/path/to/file.txt', 5000);
getCookie
方法
在指定标签页中获取 Cookie。
ssf.Browser.getCookie(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getCookie(1, '//div[@id="content"]', 5000);
setCookie
方法
在指定标签页中设置 Cookie。
ssf.Browser.setCookie(tabId: number, xpath: string, cookie: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
cookie | string | 要设置的 Cookie 字符串 |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.setCookie(1, '//div[@id="content"]', 'name=value; domain=.example.com', 5000);
reload
方法
在指定标签页中重新加载页面。
ssf.Browser.reload(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.reload(1, '//div[@id="content"]', 5000);
goForward
方法
在指定标签页中前进一页。
ssf.Browser.goForward(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.goForward(1, '//div[@id="content"]', 5000);
goBack
方法
在指定标签页中后退一页。
ssf.Browser.goBack(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.goBack(1, '//div[@id="content"]', 5000);
getTitle
方法
在指定标签页中获取页面标题。
ssf.Browser.getTitle(tabId: number, xpath: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
xpath | string | 元素的 XPath |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.getTitle(1, '//div[@id="content"]', 5000);
create_tab
方法
创建一个新的标签页。
ssf.Browser.create_tab(url: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
url | string | 新标签页的 URL |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.create_tab('https://example.com', 5000);
update_tab
方法
更新指定标签页的 URL。
ssf.Browser.update_tab(tabId: number, url: string, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
url | string | 新的 URL |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.update_tab(1, 'https://example.com/new-page', 5000);
remove_tab
方法
关闭指定的标签页。
ssf.Browser.remove_tab(tabId: number, timeout: number): any;
参数
参数名 | 类型 | 描述 |
---|---|---|
tabId | number | 标签页的 ID |
timeout | number | 操作超时时间(单位为毫秒) |
示例
ssf.Browser.remove_tab(1, 5000);
current_tab
方法
获取当前活动的标签页信息。
ssf.Browser.current_tab(timeout: number): TabInfo;
参数
参数名 | 类型 | 描述 |
---|---|---|
timeout | number | 操作超时时间(单位为毫秒) |
返回值
TabInfo
- 当前活动标签页的信息。
示例
const currentTab = ssf.Browser.current_tab(5000);
console.log('当前活动标签页信息:', currentTab);
get_tab_list
方法
获取所有标签页的信息列表。
ssf.Browser.get_tab_list(timeout: number): TabInfo[];
参数
参数名 | 类型 | 描述 |
---|---|---|
timeout | number | 操作超时时间(单位为毫秒) |
返回值
TabInfo[]
- 所有标签页的信息列表。
示例
const tabList = ssf.Browser.get_tab_list(5000);
console.log('所有标签页信息:', tabList);