public abstract class ElementHandle extends JSHandle
| 限定符和类型 | 字段和说明 |
|---|---|
protected JSHandle |
handle |
protected ElementHandle |
isolatedHandle |
protected static org.slf4j.Logger |
LOGGER |
| 构造器和说明 |
|---|
ElementHandle(JSHandle handle) |
| 限定符和类型 | 方法和说明 |
|---|---|
ElementHandle |
$(String selector)
查询当前元素内与给定选择器匹配的第一个元素。
|
List<ElementHandle> |
$$(String selector)
查询当前元素内与给定选择器匹配的所有元素。
|
Object |
$$eval(String selector,
String pptrFunction)
对当前元素中找到所有与给定选择器匹配的元素,并运行给定的 JS 函数。
|
Object |
$$eval(String selector,
String pptrFunction,
List<Object> args)
对当前元素中找到所有与给定选择器匹配的元素,并运行给定的 JS 函数。
|
Object |
$eval(String selector,
String pptrFunction)
在当前元素中找到指定选择器的第一个元素,并运行给定的 JS 函数。
|
Object |
$eval(String selector,
String pptrFunction,
List<Object> args)
在当前元素中找到指定选择器的第一个元素,并运行给定的 JS 函数。
|
protected ElementHandle |
adoptIsolatedHandle()
获取隔离的ElementHandle,确保返回的ElementHandle对应PUPPETEER_WORLD类型的IsolatedWorld。
|
<T> T |
adoptResult(T isolatedResult)
转换由隔离的isolatedHandle执行的结果,转为当前elementHandle的结果
|
ElementHandle |
asElement()
将 JSHandle 转成 ElementHandle 的方法
如果对象是 JSHandle 返回 null,如果对象是 ElementHandle,返回当前对象
|
protected void |
assertConnectedElement() |
abstract void |
autofill(AutofillData data)
如果该元素是表单输入,则可以使用 ElementHandle.autofill() 来测试表单是否与浏览器的自动填充实现兼容。
|
abstract int |
backendNodeId()
When connected using Chrome DevTools Protocol, it returns a
DOM.BackendNodeId for the element.
|
BoundingBox |
boundingBox()
此方法返回元素的边界框(相对于主框架),如果元素是 不是布局的一部分,则返回 null(例如:display: none).
|
BoxModel |
boxModel()
此方法返回元素的框,如果元素是 不是布局的一部分,则返回 null(例如:display: none)。
|
void |
click()
如果需要,此方法将元素滚动到视图中,然后使用 Page.mouse 单击元素的中心。
|
void |
click(ClickOptions options)
如果需要,此方法将元素滚动到视图中,然后使用 Page.mouse 单击元素的中心。
|
Point |
clickablePoint()
返回元素内的中点,除非提供了特定的偏移量。
|
Point |
clickablePoint(Offset offset)
返回元素内的中点,除非提供了特定的偏移量。
|
abstract Frame |
contentFrame()
解析与元素关联的框架(如果有)。
|
void |
dispose()
释放当前对象
当页面被重新导航或者关闭,ElementHandles 会被自动处理。
|
boolean |
disposed()
当前对象是否已经释放
|
DragData |
drag(ElementHandle target)
将当前元素拖动到目标元素上
|
DragData |
drag(Point point)
将当前元素拖动到指定位置
|
Object |
evaluate(String pptrFunction)
使用当前对象作为第一个参数来执行给定的 JS 函数。
|
Object |
evaluate(String pptrFunction,
List<Object> args)
使用当前对象作为第一个参数来执行给定的 JS 函数。
|
JSHandle |
evaluateHandle(String pptrFunction)
使用当前对象作为第一个参数来执行给定的 JS 函数。
|
JSHandle |
evaluateHandle(String pptrFunction,
List<Object> args)
使用当前对象作为第一个参数来执行给定的 JS 函数。
|
void |
focus()
对元素调用 focus。
|
abstract Frame |
frame()
与当前对象对应的 Frame
|
Map<String,JSHandle> |
getProperties()
获取表示当前对象属性的句柄映射。
|
JSHandle |
getProperty(String propertyName)
获取表示当前对象属性的句柄映射。
|
void |
hover()
如果需要,此方法将元素滚动到视图中,然后使用 Page.mouse 将鼠标悬停在元素的中心上。
|
String |
id()
返回 当前对象的 remoteObject 对象的id
|
boolean |
isHidden()
当前元素是否隐藏
|
boolean |
isIntersectingViewport()
如果元素在当前视口中可见,则解析为 true。
|
boolean |
isIntersectingViewport(int threshold)
如果元素在当前视口中可见,则解析为 true。
|
boolean |
isVisible()
当前元素是否可见
|
Object |
jsonValue()
表示引用对象的可序列化部分的普通对象
|
void |
press(String key)
聚焦元素,然后使用 Keyboard.down() 和 Keyboard.up()。
|
void |
press(String key,
KeyPressOptions options)
聚焦元素,然后使用 Keyboard.down() 和 Keyboard.up()。
|
abstract List<ElementHandle> |
queryAXTree(String name,
String role) |
RemoteObject |
remoteObject()
提供对支持当前对象的 Protocol.Runtime.RemoteObject 的访问。
|
String |
screenshot(ElementScreenshotOptions options)
如果需要,此方法将元素滚动到视图中,然后使用 Page.screenshot() 截取元素的屏幕截图。
|
String |
screenshot(String path) |
void |
scrollIntoView()
将当前元素滚动到视图中。
|
protected void |
scrollIntoViewIfNeeded() |
List<String> |
select(List<String> values)
根据给定的值列表来选择元素,并返回选中的值列表
该方法通过在浏览器环境中执行一段JavaScript代码来实现对HTML选择框的操作
包括单选和多选情况的处理,并确保触发相应的输入和变化事件
|
void |
tap()
点击元素,如果元素不可见,则滚动到可见。
|
ElementHandle |
toElement(String tagName)
将当前句柄转换为给定的元素类型。
|
String |
toString()
返回 JSHandle 的字符串表示形式。
|
void |
touchEnd()
结束触摸元素
|
void |
touchMove()
此方法在需要时将元素滚动到视图中,然后将触摸移动到元素的中心。
|
void |
touchStart()
此方法在需要时将元素滚动到视图中,然后在元素的中心启动触摸。
|
void |
type(String text)
聚焦元素,然后为文本中的每个字符发送 keydown、keypress/input 和 keyup 事件。
|
void |
type(String text,
long delay)
聚焦元素,然后为文本中的每个字符发送 keydown、keypress/input 和 keyup 事件。
|
abstract void |
uploadFile(List<String> filePaths)
上传文件方法
该方法负责将提供的文件路径列表上传到特定的输入元素它首先确定输入元素是否支持多文件上传,
然后验证文件路径列表的大小是否符合上传条件接着检查每个文件路径是否可读,如果不可读,则抛出异常
当文件路径列表为空时,该方法通过评估一段脚本来更新输入元素的files属性以模拟用户操作,当列表不为空时,
它会通过DOM API设置文件输入元素的文件列表
|
ElementHandle |
waitForSelector(String selector,
WaitForSelectorOptions options)
等待直到指定的选择器匹配的元素满足某些条件(可见、隐藏或存在).
|
protected static final org.slf4j.Logger LOGGER
protected volatile ElementHandle isolatedHandle
protected final JSHandle handle
public ElementHandle(JSHandle handle)
protected ElementHandle adoptIsolatedHandle() throws com.fasterxml.jackson.core.JsonProcessingException
当当前句柄在其frame的隔离realm中时,直接返回自身 否则,如果尚未创建隔离句柄,则在同步块中创建隔离句柄 这确保了在多线程环境下,isolatedHandle的创建是线程安全的
com.fasterxml.jackson.core.JsonProcessingException - 如果转换过程出现错误public <T> T adoptResult(T isolatedResult)
throws com.fasterxml.jackson.core.JsonProcessingException
isolatedResult - isolatedHandle执行的结果com.fasterxml.jackson.core.JsonProcessingException - 序列化错误public JSHandle getProperty(String propertyName) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
getProperty 在类中 JSHandlecom.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public Map<String,JSHandle> getProperties() throws com.fasterxml.jackson.core.JsonProcessingException
getProperties 在类中 JSHandlecom.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public Object evaluate(String pptrFunction) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
evaluate 在类中 JSHandlepptrFunction - 给定函数com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public Object evaluate(String pptrFunction, List<Object> args) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
evaluate 在类中 JSHandlepptrFunction - 给定函数args - 给定函数的参数,第一个参数是当前对象com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public JSHandle evaluateHandle(String pptrFunction) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
evaluateHandle 在类中 JSHandlepptrFunction - 给定函数com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public JSHandle evaluateHandle(String pptrFunction, List<Object> args) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
evaluateHandle 在类中 JSHandlepptrFunction - 给定函数args - 给定函数的参数,第一个参数是当前对象com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - 给定函数执行的异常public Object jsonValue() throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
jsonValue 在类中 JSHandlecom.fasterxml.jackson.core.JsonProcessingException - 如果对象由于循环而无法序列化,则抛出该异常。EvaluateException - 给定函数执行的异常public String toString()
public RemoteObject remoteObject()
remoteObject 在类中 JSHandlepublic ElementHandle asElement()
如果对象是 JSHandle 返回 null,如果对象是 ElementHandle,返回当前对象
public abstract Frame frame()
public ElementHandle $(String selector) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
selector - 选择器com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - JS函数执行异常public List<ElementHandle> $$(String selector) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
selector - 选择器com.fasterxml.jackson.core.JsonProcessingException - 序列化异常EvaluateException - JS函数执行异常public Object $eval(String selector, String pptrFunction) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
可以用来获取元素内的属性,修改元素属性等
selector - 在当前元素内查找元素的选择器pptrFunction - 给定的函数com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public Object $eval(String selector, String pptrFunction, List<Object> args) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
可以用来获取元素内的属性,修改元素属性等
selector - 在当前元素内查找元素的选择器pptrFunction - 给定的 JS 函数args - pptrFunction 的参数,如果有的话com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public Object $$eval(String selector, String pptrFunction) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
selector - 在当前元素内查找元素的选择器pptrFunction - 给定的 JS 函数com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public Object $$eval(String selector, String pptrFunction, List<Object> args) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
与 $eval 函数的区别:可以同时对匹配的所有元素进行操作
selector - 在当前元素内查找元素的选择器pptrFunction - 给定的 JS 函数args - pptrFunction 的参数,如果有的话com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public boolean isVisible()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public boolean isHidden()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public ElementHandle toElement(String tagName) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
如果句柄不匹配,则会出现错误。句柄不会被自动处置。
tagName - 给定的元素类型com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public abstract Frame contentFrame() throws com.fasterxml.jackson.core.JsonProcessingException
com.fasterxml.jackson.core.JsonProcessingExceptionpublic Point clickablePoint() throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public Point clickablePoint(Offset offset) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
offset - 特定的偏移量。com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public void hover()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public void click()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public void click(ClickOptions options) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
options - 可选的点击参数com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public DragData drag(ElementHandle target) throws com.fasterxml.jackson.core.JsonProcessingException
target - 目标元素com.fasterxml.jackson.core.JsonProcessingException - 抛出异常public DragData drag(Point point) throws com.fasterxml.jackson.core.JsonProcessingException
point - 指定位置com.fasterxml.jackson.core.JsonProcessingException - 抛出异常public List<String> select(List<String> values) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
values - 要选中的值的列表com.fasterxml.jackson.core.JsonProcessingException - 如果JavaScript代码序列化失败EvaluateException - 如果在浏览器环境中执行JavaScript代码失败public abstract void uploadFile(List<String> filePaths) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
该方法负责将提供的文件路径列表上传到特定的输入元素它首先确定输入元素是否支持多文件上传, 然后验证文件路径列表的大小是否符合上传条件接着检查每个文件路径是否可读,如果不可读,则抛出异常
当文件路径列表为空时,该方法通过评估一段脚本来更新输入元素的files属性以模拟用户操作,当列表不为空时, 它会通过DOM API设置文件输入元素的文件列表
filePaths - 文件路径列表com.fasterxml.jackson.core.JsonProcessingException - 当JSON处理失败时抛出EvaluateException - 当JavaScript评估失败时抛出public abstract List<ElementHandle> queryAXTree(String name, String role) throws com.fasterxml.jackson.core.JsonProcessingException
com.fasterxml.jackson.core.JsonProcessingExceptionprotected void scrollIntoViewIfNeeded()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingExceptionEvaluateExceptionpublic void tap()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 如果序列化失败EvaluateException - 如果执行失败public void touchStart()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 如果序列化失败EvaluateException - 如果执行失败public void touchMove()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 如果序列化失败EvaluateException - 如果执行失败public void touchEnd()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 如果序列化失败EvaluateException - 如果 JS 函数执行失败public void focus()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - Json解析异常EvaluateException - 如果 JS 函数执行失败public void type(String text) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
要按特殊键,例如 Control 或 ArrowDown,请使用 ElementHandle.press()。
text - 发送的文本com.fasterxml.jackson.core.JsonProcessingException - Json解析异常EvaluateException - 如果 JS 函数执行失败public void type(String text, long delay) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
要按特殊键,例如 Control 或 ArrowDown,请使用 ElementHandle.press()。
text - 发送的文本delay - 间隔 delay 毫秒数发送文本的字符数com.fasterxml.jackson.core.JsonProcessingException - Json解析异常EvaluateException - 如果 JS 函数执行失败public void press(String key) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
如果 key 是单个字符,并且除了 Shift 之外没有按下修饰键,则还将生成 keypress/input 事件。可以指定 text 选项来强制生成输入事件。
注意修饰键确实会影响 elementHandle.press。按住 Shift 将以大写形式键入文本。
key - 要按下的键com.fasterxml.jackson.core.JsonProcessingExceptionEvaluateExceptionpublic void press(String key, KeyPressOptions options) throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
key - 要按下的键options - 选项com.fasterxml.jackson.core.JsonProcessingExceptionEvaluateExceptionpublic void scrollIntoView()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON数据时发生错误。EvaluateException - 当执行脚本评估时发生错误。protected void assertConnectedElement()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingExceptionEvaluateExceptionpublic boolean isIntersectingViewport(int threshold)
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
threshold - 0(无交叉)和 1(完全交叉)之间交叉的阈值。默认为 1。com.fasterxml.jackson.core.JsonProcessingException - Json解析异常EvaluateException - 评估异常public boolean isIntersectingViewport()
throws com.fasterxml.jackson.core.JsonProcessingException,
EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - Json解析异常EvaluateException - 评估异常public abstract void autofill(AutofillData data)
目前,仅支持自动填充信用卡信息,并且在 Chrome 中仅支持新的 headless 和 headful 模式。
data - 自动填写表单数据public BoundingBox boundingBox() throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public BoxModel boxModel() throws com.fasterxml.jackson.core.JsonProcessingException, EvaluateException
盒子被表示为点数组;每个点都是一个对象 {x, y}。箱点按顺时针顺序排序。
com.fasterxml.jackson.core.JsonProcessingException - 当处理JSON时发生错误EvaluateException - 当页面函数执行失败时抛出异常public String screenshot(String path) throws IOException, EvaluateException, ExecutionException, InterruptedException
public String screenshot(ElementScreenshotOptions options) throws IOException, EvaluateException, ExecutionException, InterruptedException
options - 截图配置IOException - IO异常EvaluateException - 截图异常ExecutionExceptionInterruptedExceptionpublic ElementHandle waitForSelector(String selector, WaitForSelectorOptions options) throws com.fasterxml.jackson.core.JsonProcessingException
selector - 选择器字符串,用于选择目标元素.options - 包含等待条件的选项,如元素可见或隐藏.com.fasterxml.jackson.core.JsonProcessingExceptionpublic abstract int backendNodeId()
Copyright © 2020–2025. All rights reserved.