public interface Frame
Each web page loaded in the browser has a main (top-level) frame. The frame itself may have
child frames. When a web page is unloaded, its frame and all child frames are closed
automatically. Any attempt to work with an already closed frame will lead to the IllegalStateException
error.
Modifier and Type | Method and Description |
---|---|
Browser |
browser()
Returns the
Browser instance of this frame. |
java.util.List<Frame> |
children()
Returns the list of child frames or an empty list if this frame does not have any children.
|
java.util.Optional<Document> |
document()
Returns an
Optional that contains the DOM Document instance of the frame or
an empty Optional if the frame does not have a document. |
boolean |
execute(EditorCommand command)
Executes the given
command in the frame. |
<T> T |
executeJavaScript(java.lang.String javaScript)
Executes the given
javaScript code in the frame and returns
the result of the execution. |
void |
executeJavaScript(java.lang.String javaScript,
java.util.function.Consumer<?> callback)
Executes the given
javaScript code in the frame and returns the result of the
execution through the given callback . |
boolean |
hasSelection()
Returns
true if some content in this frame is selected. |
java.lang.String |
html()
Returns a string that represents content of the frame in the HTML format or an empty string
if the frame does not have a content or its content is empty.
|
PointInspection |
inspect(int x,
int y)
Inspects the given location on the web page containing this frame and returns the result of
the inspection.
|
PointInspection |
inspect(Point location)
Inspects the given
location on the web page containing this frame and returns the
result of the inspection. |
boolean |
isCommandEnabled(EditorCommand.Name commandName)
Returns
true if the command with the given commandName can be executed in the
frame. |
boolean |
isMain()
Returns
true if the frame is a main (top-level) frame in the browser. |
Json |
json()
Returns the
Json instance for this frame . |
void |
loadHtml(java.lang.String html)
Navigates the frame to a data URL assembled from the given HTML.
|
void |
loadUrl(java.lang.String url)
Navigates the frame to a resource identified by the given
url . |
WebStorage |
localStorage()
Returns the
localStorage instance of this frame. |
java.lang.String |
name()
Returns a string that represent the name of the frame or an empty string if the frame does
not have a name.
|
java.util.Optional<Frame> |
parent()
Returns an
Optional that contains the parent Frame instance or an empty
Optional if the current frame is a main (top-level) frame and it does not have a
parent. |
void |
print()
Requests printing of the currently loaded web page in this frame.
|
RenderProcess |
renderProcess()
Returns a
RenderProcess associated with the current Frame instance. |
java.lang.String |
selectionAsHtml()
Returns a string that contains HTML of the selected content in the frame or an empty string
if there is no selection.
|
java.lang.String |
selectionAsText()
Returns a text representation of the selected content in the frame or an empty string if
there is no selection.
|
WebStorage |
sessionStorage()
Returns the
sessionStorage instance of this frame. |
java.lang.String |
text()
Returns the content of the frame and its sub-frames as plain text or an empty string if the
frame does not have content or its content is empty.
|
void |
viewSource()
Creates a new popup where the
view-source:<frame-url> URL is loaded to display HTML
source of the frame. |
Browser browser()
Browser
instance of this frame.boolean isMain()
true
if the frame is a main (top-level) frame in the browser.java.util.Optional<Frame> parent()
Optional
that contains the parent Frame
instance or an empty
Optional
if the current frame is a main (top-level) frame and it does not have a
parent.ObjectClosedException
- when the frame is closedjava.util.List<Frame> children()
ObjectClosedException
- when this frame is closedjava.lang.String selectionAsText()
ObjectClosedException
- when the frame is closedjava.lang.String selectionAsHtml()
ObjectClosedException
- when the frame is closedboolean hasSelection()
true
if some content in this frame is selected.ObjectClosedException
- when the frame is closedjava.util.Optional<Document> document()
Optional
that contains the DOM Document
instance of the frame or
an empty Optional
if the frame does not have a document.ObjectClosedException
- when the frame is closedjava.lang.String name()
ObjectClosedException
- when the frame is closedjava.lang.String html()
ObjectClosedException
- when the frame is closedjava.lang.String text()
Limitations:
display:none
, unexpanded divs, etc.
Please note that this functionality is resource-intensive, consuming significant memory and CPU during a text capture.
ObjectClosedException
- when the frame is closedvoid viewSource()
view-source:<frame-url>
URL is loaded to display HTML
source of the frame.ObjectClosedException
- when the frame is closedvoid loadUrl(java.lang.String url)
url
.
This method tells the frame to start asynchronous loading and returns immediately.
url
- URL of the resource to loadjava.lang.IllegalArgumentException
- when url
is empty or blankObjectClosedException
- when the frame is closedvoid loadHtml(java.lang.String html)
This method tells the frame to start asynchronous loading and returns immediately.
But does nothing if the resulting data URL is longer than 2 * 1024 * 1024
characters.
Important: pages loaded through data URLs are not allowed to access resources from the file system.
html
- an HTML to load into the frameObjectClosedException
- when the frame is closed@Nullable <T> T executeJavaScript(java.lang.String javaScript)
javaScript
code in the frame and returns
the result of the execution.
The type mapping rules are the following:
| JavaScript | Java | |--------------------|----------------| | Number | Double | | String | String | | Boolean | Boolean | | null and undefined | null | | Node | JsObject, Node | | ArrayBuffer | JsArrayBuffer | | Array | JsArray | | Set | JsSet | | Map | JsMap | | Object | JsObject | | Proxy Object | Object |
Proxy objects are mapped to the corresponding injected Java object.
Important: This method blocks the current thread execution and waits until the passed JavaScript code has been executed completely. The time required to complete the execution can be different depending on the passed JavaScript code. So, it is strongly recommended that the method is not invoked in the application UI thread.
T
- the result type according to the type mapping rulesjavaScript
- a string that represents JavaScript code to executenull
if the result of the execution on
JavaScript is null
or undefined
ObjectClosedException
- when the frame is closedjava.lang.IllegalArgumentException
- when javaScript
is emptyvoid executeJavaScript(java.lang.String javaScript, java.util.function.Consumer<?> callback)
javaScript
code in the frame and returns the result of the
execution through the given callback
.
The type mapping rules are the following:
| JavaScript | Java | |--------------------|----------------| | Number | Double | | String | String | | Boolean | Boolean | | null and undefined | null | | Node | JsObject, Node | | ArrayBuffer | JsArrayBuffer | | Array | JsArray | | Set | JsSet | | Map | JsMap | | Object | JsObject | | Proxy Object | Object |
Proxy objects are mapped to the corresponding injected Java object.
This method does not block the current thread execution and executes the given JavaScript code asynchronously.
javaScript
- a string that represents JavaScript code to executecallback
- a callback you can use to get the result of the execution. The result type is
determined according to the type mapping rulesObjectClosedException
- when the frame is closedjava.lang.IllegalArgumentException
- when javaScript
is emptyboolean execute(EditorCommand command)
command
in the frame.
Before executing the command it is recommended to check whether it can be executed or not
using the isCommandEnabled(EditorCommand.Name)
method.
command
- the command to executetrue
if the command has been executed successfullyObjectClosedException
- when the frame is closedisCommandEnabled(EditorCommand.Name)
boolean isCommandEnabled(EditorCommand.Name commandName)
true
if the command with the given commandName
can be executed in the
frame.
Some commands can be executed only under certain conditions. For example, the EditorCommand.Name.INSERT_TEXT
command can be executed only if there is a focused text field
in the frame.
commandName
- the name of the command to checkObjectClosedException
- when the frame is closedPointInspection inspect(Point location)
location
on the web page containing this frame and returns the
result of the inspection.location
- a point on the web pageObjectClosedException
- when the frame is closedPointInspection inspect(int x, int y)
x
- a horizontal coordinate on the web pagey
- a vertical coordinate on the web pageObjectClosedException
- when the frame is closedWebStorage localStorage()
localStorage
instance of this frame.ObjectClosedException
- when the frame is closedWebStorage sessionStorage()
sessionStorage
instance of this frame.ObjectClosedException
- when the frame is closedvoid print()
ObjectClosedException
- when the frame is closedJson json()
Json
instance for this frame
.ObjectClosedException
- when the frame is closedRenderProcess renderProcess()
RenderProcess
associated with the current Frame instance. When Chromium
loads a new web page, it creates a new render process where DOM and JavaScript are running.
This render process can be terminated by Chromium engine when a new web page with different
domain is loaded. So, this method can return different render process information.