Class HtmlPage
- All Implemented Interfaces:
Serializable, Cloneable, Page, Document, Node
- Direct Known Subclasses:
XHtmlPage
This class provides different methods to access the page's content like
getForms(), getAnchors(), getElementById(String), ... as well as the
very powerful inherited methods DomNode.getByXPath(String) and DomNode.getFirstByXPath(String)
for fine grained user specific access to child nodes.
Child elements allowing user interaction provide methods for this purpose like DomElement.click(),
HtmlElement.type(String), HtmlOption.setSelected(boolean), ...
HtmlPage instances should not be instantiated directly. They will be returned by WebClient.getPage(String)
when the content type of the server's response is text/html (or one of its variations).
Example:
final HtmlPage page = webClient.
getPage("http://mywebsite/some/page.html");
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classCache computed styles when possible, because their calculation is very expensive.(package private) static classprivate classListens for changes anywhere in the document and evicts cached computed styles whenever something relevant changes.(package private) static enumVarious possible external JavaScript file loading results.Nested classes/interfaces inherited from class DomNode
DomNode.ChildIterator, DomNode.DescendantElementsIterator<T> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List<PostponedAction> private Collection<HtmlAttributeChangeListener> Definition of special cases for the smart DomHtmlAttributeChangeListenerImplprivate List<AutoCloseable> private HtmlBaseprivate URLprivate booleanprivate HtmlPage.ComputedStylesCacheprivate static final Comparator<DomElement> private HTMLParserDOMBuilderprivate ElementFromPointHandlerprivate DomElementprivate SortedSet<BaseFrameElement> private Map<String, SortedSet<DomElement>> private intprivate final Objectprivate static final org.apache.commons.logging.Logprivate Map<String, SortedSet<DomElement>> private Charsetprivate intprivate List<SimpleRange> private intFields inherited from class DomNode
PROPERTY_ELEMENT, READY_STATE_COMPLETE, READY_STATE_INTERACTIVE, READY_STATE_LOADED, READY_STATE_LOADING, READY_STATE_UNINITIALIZEDFields inherited from interface Node
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE -
Constructor Summary
ConstructorsConstructorDescriptionHtmlPage(WebResponse webResponse, WebWindow webWindow) Creates an instance of HtmlPage. -
Method Summary
Modifier and TypeMethodDescription(package private) voidaddAfterLoadAction(PostponedAction action) Adds an action that should be executed once the page has been loaded.voidaddAutoCloseable(AutoCloseable autoCloseable) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Adds anAutoCloseable, which would be closed during thecleanUp().private voidaddElement(Map<String, SortedSet<DomElement>> map, DomElement element, String attribute, boolean recurse) voidAdds an HtmlAttributeChangeListener to the listener list.(package private) voidaddMappedElement(DomElement element) Adds an element to the ID and name maps, if necessary.(package private) voidaddMappedElement(DomElement element, boolean recurse) Adds an element to the ID and name maps, if necessary.Not yet implemented.private voidprotected voidcheckChildHierarchy(Node newChild) Check for insertion errors for a new child node.private voidvoidcleanUp()Clean up this page.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles.voidclearComputedStyles(DomElement element) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles for a specificElement.voidclearComputedStylesUpToRoot(DomElement element) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles for a specificElementand all parent elements.protected HtmlPageclone()Creates a clone of this instance, and clears cached state to be not shared with the original.cloneNode(boolean deep) createAttributeNS(String namespaceURI, String qualifiedName) Not yet implemented.createElement(String tagName) createElementNS(String namespaceURI, String qualifiedName) Not yet implemented.createProcessingInstruction(String namespaceURI, String qualifiedName) Not yet implemented.private static Comparator<HtmlElement> voidDeregister frames that are no longer in use.private voidExecutes any deferred scripts, if necessary.private booleanexecuteEventHandlersIfNeeded(String eventType) Looks for and executes any appropriate event handlers.executeJavaScript(String sourceCode) Executes the specified JavaScript code within the page.executeJavaScript(String sourceCode, String sourceName, int startLine) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.executeJavaScriptFunction(Object function, Object thisObject, Object[] args, DomNode htmlElementScope) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Execute a Function in the given context.private ScriptResultexecuteJavaScriptFunction(org.htmlunit.corejs.javascript.Function function, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args, DomNode htmlElementScope) private voidIf a refresh has been specified either through a meta tag or an HTTP response header, then perform that refresh.(package private) voidNotifies all registered listeners for the given event to add an attribute.(package private) voidNotifies all registered listeners for the given event to remove an attribute.(package private) voidNotifies all registered listeners for the given event to replace an attribute.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.getAnchorByHref(String href) Returns theHtmlAnchorwith the specified href.getAnchorByName(String name) Returns theHtmlAnchorwith the specified name.getAnchorByText(String text) Returns the first anchor with the specified text.Returns a list of all anchors contained in this page.The base URL used to resolve relative URLs.getBody()Returns thebodyelement (orframesetelement), ornullif it does not yet exist.Returns the encoding.Returns the content type of this page.private HtmlPage.ComputedStylesCacheReturns the document element.Not yet implemented.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the current builder.Not yet implemented.getElementById(String elementId) <E extends DomElement>
EgetElementByName(String name) Returns the element with the specified name.getElementFromPoint(int x, int y) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the element for the specified x coordinate and the specified y coordinate.getElementsById(String elementId) Returns the elements with the specified ID.getElementsByIdAndOrName(String idAndOrName) Returns the elements with the specified string for their name or ID.getElementsByName(String name) Returns the elements with the specified name attribute.private static DomElementgetFirstChildElement(DomElement startElement, Class<?> clazz) Gets the first child of startElement that is an instance of the given class.private DomElementgetFirstChildElementRecursive(DomElement startElement, Class<?> clazz) Gets the first child of startElement or it's children that is an instance of the given class.Returns the element with the focus or null if no element has the focus.getFormByName(String name) Returns the first form that matches the specified name.getForms()Returns a list of all the forms in this page.getFrameByName(String name) Returns the first frame contained in this page with the specified name.Returns a list containing all the frames (from frame and iframe tags) in this page.getFullyQualifiedUrl(String relativeUrl) Given a relative URL (ie/foo), returns a fully-qualified URL based on the URL that was used to load this page.getHead()Returns the head element.getHtmlElementByAccessKey(char accessKey) Returns the HTML element that is assigned to the specified access key.<E extends HtmlElement>
EgetHtmlElementById(String elementId) Returns the HTML element with the specified ID.getHtmlElementsByAccessKey(char accessKey) Returns all the HTML elements that are assigned to the specified access key.Not yet implemented.Not yet implemented.getMetaTags(String httpEquiv) Gets the meta tag for a givenhttp-equivvalue.Returns all namespaces defined in the root element of this page.getPage()Returns the page that contains this node.private StringReturns an auto-refresh string if specified.getResolvedTarget(String elementTarget) Given a target attribute value, resolve the target using a base target for the page.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.booleanNot yet implemented.getStyleFromCache(DomElement element, String normalizedPseudo) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.Returns a list of ids (strings) that correspond to the tabbable elements in this page.Returns a list of all elements that are tabbable in the order that will be used for tabbing.private HtmlTitleGets the title element for this page.Returns the title of this page or an empty string if the title wasn't specified.booleanbooleanIndicates if the provided event can be applied to this node.booleanReturnstrueif this page has case-sensitive tag names,falseotherwise.importNode(Node importedNode, boolean deep) Not yet implemented.voidInitialize this page.booleanINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates if this node is currently attached to the page.booleanReturnstrueif an HTML parser is operating on this page, adding content to it.booleanReturns true if this page is an HtmlPage.(package private) static booleanisMappedElement(Document document, String attributeName) Indicates if the attribute name indicates that the owning element is mapped.booleanINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.private booleanisOnbeforeunloadAccepted(HtmlPage page, Event event) booleanINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returnstrueif an HTML parser is parsing a non-inline HTML snippet to add content to this page.booleanINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returnstrueif an HTML parser is parsing an inline HTML snippet to add content to this page.booleanReturns whether the current page mode is inquirks modeor instandards mode.(package private) HtmlPage.JavaScriptLoadResultloadExternalJavaScriptFile(String srcAttribute, Charset scriptCharset) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.(package private) voidLoads the content of the contained frames.private ObjectloadJavaScriptFromUrl(URL url, Charset scriptCharset) Loads JavaScript from the specified URL.(package private) voidnotifyNodeAdded(DomNode node) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.(package private) voidnotifyNodeRemoved(DomNode node) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.pressAccessKey(char accessKey) Simulate pressing an access key.voidputStyleIntoCache(DomElement element, String normalizedPseudo, ComputedCssStyleDeclaration style) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Caches a CSS2Properties object.private voidrefresh()Refreshes the page by sending the same parameters as previously sent to get this page.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing an inline HTML snippet.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing an inline HTML snippet.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing some content for this page.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing some content for this page.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing a non-inline HTML snippet.voidINTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing a non-inline HTML snippet.private voidremoveElement(Map<String, SortedSet<DomElement>> map, DomElement element, String attribute, boolean recurse) voidRemoves an HtmlAttributeChangeListener from the listener list.(package private) voidremoveMappedElement(DomElement element, boolean recurse, boolean descendant) Removes an element and optionally its children from the ID and name maps, if necessary.(package private) voidremoveMappedElement(HtmlElement element) Removes an element from the ID and name maps, if necessary.renameNode(Node newNode, String namespaceURI, String qualifiedName) Not yet implemented.private List<HtmlAttributeChangeListener> voidSaves the current page, with all images, to the specified location.voidsetDocumentType(DocumentType type) Sets the document type.voidsetDocumentURI(String documentURI) Not yet implemented.voidsetDOMBuilder(HTMLParserDOMBuilder htmlUnitDOMBuilder) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Sets the builder to allow page to send content from document.write(ln) calls.voidsetElementFromPointHandler(ElementFromPointHandler elementFromPointHandler) Sets theElementFromPointHandler.voidsetElementWithFocus(DomElement elementWithFocus) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.booleansetFocusedElement(DomElement newElement) Moves the focus to the specified element.booleansetFocusedElement(DomElement newElement, boolean windowActivated) Moves the focus to the specified element.voidsetNodeValue(String value) voidvoidsetSelectionRange(SimpleRange selectionRange) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.voidsetStrictErrorChecking(boolean strictErrorChecking) Not yet implemented.voidsetTitleText(String message) Sets the text for the title of this page.voidsetXmlStandalone(boolean xmlStandalone) Not yet implemented.voidsetXmlVersion(String xmlVersion) Not yet implemented.Move the focus to the next element in the tab order.Move the focus to the previous element in the tab order.toString()Gives a basic representation for debugging purposes.voidwriteInParsedStream(String string) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.private voidMethods inherited from class SgmlPage
asXml, createAttribute, createCDATASection, createComment, createDocumentFragment, createNodeIterator, createTextNode, getCanonicalXPath, getDoctype, getElementsByTagName, getElementsByTagNameNS, getEnclosingWindow, getNodeName, getNodeType, getUrl, getWebClient, getWebResponse, isPrinting, normalizeDocument, setEnclosingWindow, setPrintingMethods inherited from class DomNode
addCharacterDataChangeListener, addDomChangeListener, appendChild, asNormalizedText, basicRemove, closest, compareDocumentPosition, detach, fireCharacterDataChanged, fireNodeAdded, fireNodeDeleted, getAncestors, getAttributes, getBaseURI, getByXPath, getByXPath, getChildNodes, getChildren, getDescendants, getDomElementDescendants, getEndColumnNumber, getEndLineNumber, getFeature, getFirstByXPath, getFirstByXPath, getFirstChild, getHtmlElementDescendants, getHtmlPageOrNull, getIndex, getLastChild, getLocalName, getNamespaceURI, getNextElementSibling, getNextSibling, getNodeValue, getParentNode, getPrefix, getPreviousElementSibling, getPreviousSibling, getReadyState, getScriptableObject, getSelectorList, getStartColumnNumber, getStartLineNumber, getTextContent, getUserData, getVisibleText, hasAttributes, hasChildNodes, hasFeature, insertBefore, insertBefore, isAncestorOf, isAncestorOfAny, isDefaultNamespace, isDisplayed, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, mayBeDisplayed, normalize, notifyIncorrectness, onAddedToDocumentFragment, onAddedToPage, onAllChildrenAddedToPage, parseHtmlSnippet, printChildrenAsXml, printXml, processImportNode, querySelector, querySelectorAll, quietlyRemoveAndMoveChildrenTo, remove, removeAllChildren, removeCharacterDataChangeListener, removeChild, removeDomChangeListener, replace, replaceChild, setEndLocation, setParentNode, setReadyState, setScriptableObject, setStartLocation, setTextContent, setUserDataMethods inherited from class Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Node
appendChild, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeValue, getParentNode, getPrefix, getPreviousSibling, getTextContent, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setTextContent, setUserData
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
documentPositionComparator
-
domBuilder_
-
originalCharset_
-
lock_
-
idMap_
-
nameMap_
-
frameElements_
-
parserCount_
private int parserCount_ -
snippetParserCount_
private int snippetParserCount_ -
inlineSnippetParserCount_
private int inlineSnippetParserCount_ -
attributeListeners_
-
afterLoadActions_
-
cleaning_
private boolean cleaning_ -
base_
-
baseUrl_
-
autoCloseableList_
-
elementFromPointHandler_
-
elementWithFocus_
-
selectionRanges_
-
computedStylesCache_
-
TABBABLE_TAGS
-
ACCEPTABLE_TAG_NAMES
-
ATTRIBUTES_AFFECTING_PARENT
-
-
Constructor Details
-
HtmlPage
Creates an instance of HtmlPage. An HtmlPage instance is normally retrieved withWebClient.getPage(String).- Parameters:
webResponse- the web response that was used to create this pagewebWindow- the window that this page is being loaded into
-
-
Method Details
-
getPage
-
hasCaseSensitiveTagNames
public boolean hasCaseSensitiveTagNames()Returnstrueif this page has case-sensitive tag names,falseotherwise. In general, XML has case-sensitive tag names, and HTML doesn't. This is especially important during XPath matching.- Specified by:
hasCaseSensitiveTagNamesin classSgmlPage- Returns:
trueif this page has case-sensitive tag names,falseotherwise
-
initialize
Initialize this page.- Throws:
IOException- if an IO problem occursFailingHttpStatusCodeException- if the server returns a failing status code AND the propertyWebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)is set to true.
-
addAfterLoadAction
Adds an action that should be executed once the page has been loaded.- Parameters:
action- the action
-
cleanUp
-
getDocumentElement
Returns the document element.- Specified by:
getDocumentElementin interfaceDocument- Overrides:
getDocumentElementin classSgmlPage- Returns:
- the document element
-
getBody
Returns thebodyelement (orframesetelement), ornullif it does not yet exist.- Returns:
- the
bodyelement (orframesetelement), ornullif it does not yet exist
-
getHead
-
getOwnerDocument
- Specified by:
getOwnerDocumentin interfaceNode- Overrides:
getOwnerDocumentin classDomNode
-
importNode
-
getInputEncoding
Not yet implemented. -
getXmlEncoding
-
getXmlStandalone
public boolean getXmlStandalone() -
setXmlStandalone
Not yet implemented.- Throws:
DOMException
-
getXmlVersion
-
setXmlVersion
Not yet implemented.- Throws:
DOMException
-
getStrictErrorChecking
public boolean getStrictErrorChecking()Not yet implemented. -
setStrictErrorChecking
public void setStrictErrorChecking(boolean strictErrorChecking) Not yet implemented. -
getDocumentURI
Not yet implemented. -
setDocumentURI
Not yet implemented. -
adoptNode
-
getDomConfig
Not yet implemented. -
renameNode
Not yet implemented.- Throws:
DOMException
-
getCharset
Returns the encoding.- Specified by:
getCharsetin classSgmlPage- Returns:
- the encoding
-
getContentType
Returns the content type of this page.- Specified by:
getContentTypein classSgmlPage- Returns:
- the content type of this page
-
getImplementation
Not yet implemented. -
createElement
- Parameters:
tagName- the tag name, preferably in lowercase
-
createElementNS
-
createAttributeNS
-
createEntityReference
Not yet implemented. -
createProcessingInstruction
Not yet implemented. -
getElementById
-
getAnchorByName
Returns theHtmlAnchorwith the specified name.- Parameters:
name- the name to search by- Returns:
- the
HtmlAnchorwith the specified name - Throws:
ElementNotFoundException- if the anchor could not be found
-
getAnchorByHref
Returns theHtmlAnchorwith the specified href.- Parameters:
href- the string to search by- Returns:
- the HtmlAnchor
- Throws:
ElementNotFoundException- if the anchor could not be found
-
getAnchors
Returns a list of all anchors contained in this page.- Returns:
- the list of
HtmlAnchorin this page
-
getAnchorByText
Returns the first anchor with the specified text.- Parameters:
text- the text to search for- Returns:
- the first anchor that was found
- Throws:
ElementNotFoundException- if no anchors are found with the specified text
-
getFormByName
Returns the first form that matches the specified name.- Parameters:
name- the name to search for- Returns:
- the first form
- Throws:
ElementNotFoundException- If no forms match the specified result.
-
getForms
-
getFullyQualifiedUrl
Given a relative URL (ie/foo), returns a fully-qualified URL based on the URL that was used to load this page.- Parameters:
relativeUrl- the relative URL- Returns:
- the fully-qualified URL for the specified relative URL
- Throws:
MalformedURLException- if an error occurred when creating a URL object
-
getResolvedTarget
-
getTabbableElementIds
Returns a list of ids (strings) that correspond to the tabbable elements in this page. Return them in the same order specified ingetTabbableElements()- Returns:
- the list of id's
-
getTabbableElements
Returns a list of all elements that are tabbable in the order that will be used for tabbing.The rules for determining tab order are as follows:
- Those elements that support the tabindex attribute and assign a positive value to it are navigated first. Navigation proceeds from the element with the lowest tabindex value to the element with the highest value. Values need not be sequential nor must they begin with any particular value. Elements that have identical tabindex values should be navigated in the order they appear in the character stream.
- Those elements that do not support the tabindex attribute or support it and assign it a value of "0" are navigated next. These elements are navigated in the order they appear in the character stream.
- Elements that are disabled do not participate in the tabbing order.
The following elements support the
tabindexattribute: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA.- Returns:
- all the tabbable elements in proper tab order
-
createTabOrderComparator
-
getHtmlElementByAccessKey
Returns the HTML element that is assigned to the specified access key. An access key (aka mnemonic key) is used for keyboard navigation of the page.Only the following HTML elements may have
accesskeys defined: A, AREA, BUTTON, INPUT, LABEL, LEGEND, and TEXTAREA.- Parameters:
accessKey- the key to look for- Returns:
- the HTML element that is assigned to the specified key or null if no elements can be found that match the specified key.
-
getHtmlElementsByAccessKey
Returns all the HTML elements that are assigned to the specified access key. An access key (aka mnemonic key) is used for keyboard navigation of the page.The HTML specification seems to indicate that one accesskey cannot be used for multiple elements however Internet Explorer does seem to support this. It's worth noting that Firefox does not support multiple elements with one access key so you are making your HTML browser specific if you rely on this feature.
Only the following HTML elements may have
accesskeys defined: A, AREA, BUTTON, INPUT, LABEL, LEGEND, and TEXTAREA.- Parameters:
accessKey- the key to look for- Returns:
- the elements that are assigned to the specified accesskey
-
executeJavaScript
Executes the specified JavaScript code within the page. The usage would be similar to what can be achieved to execute JavaScript in the current page by entering "javascript:...some JS code..." in the URL field of a native browser.
Note: the provided code won't be executed if JavaScript has been disabled on the WebClient (see
WebClient.isJavaScriptEnabled().- Parameters:
sourceCode- the JavaScript code to execute- Returns:
- a ScriptResult which will contain both the current page (which may be different than the previous page) and a JavaScript result object
-
executeJavaScript
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Execute the specified JavaScript if a JavaScript engine was successfully instantiated. If this JavaScript causes the current page to be reloaded (through location="" or form.submit()) then return the new page. Otherwise return the current page.
Please note: Although this method is public, it is not intended for general execution of JavaScript. Users of HtmlUnit should interact with the pages as a user would by clicking on buttons or links and having the JavaScript event handlers execute as needed..
- Parameters:
sourceCode- the JavaScript code to executesourceName- the name for this chunk of code (will be displayed in error messages)startLine- the line at which the script source starts- Returns:
- a ScriptResult which will contain both the current page (which may be different than the previous page and a JavaScript result object.
-
loadExternalJavaScriptFile
HtmlPage.JavaScriptLoadResult loadExternalJavaScriptFile(String srcAttribute, Charset scriptCharset) throws FailingHttpStatusCodeException INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
srcAttribute- the source attribute from the script tagscriptCharset- the charset from the script tag- Returns:
- the result of loading the specified external JavaScript file
- Throws:
FailingHttpStatusCodeException- if the request's status code indicates a request failure and theWebClientwas configured to throw exceptions on failing HTTP status codes
-
loadJavaScriptFromUrl
private Object loadJavaScriptFromUrl(URL url, Charset scriptCharset) throws IOException, FailingHttpStatusCodeException Loads JavaScript from the specified URL. This method may returnnullif there is a problem loading the code from the specified URL.- Parameters:
url- the URL of the scriptscriptCharset- the charset from the script tag- Returns:
- the content of the file, or
nullif we ran into a compile error - Throws:
IOException- if there is a problem downloading the JavaScript fileFailingHttpStatusCodeException- if the request's status code indicates a request failure and theWebClientwas configured to throw exceptions on failing HTTP status codes
-
getTitleText
Returns the title of this page or an empty string if the title wasn't specified.- Returns:
- the title of this page or an empty string if the title wasn't specified
-
setTitleText
Sets the text for the title of this page. If there is not a title element on this page, then one has to be generated.- Parameters:
message- the new text
-
getFirstChildElement
Gets the first child of startElement that is an instance of the given class.- Parameters:
startElement- the parent elementclazz- the class to search for- Returns:
nullif no child found
-
getFirstChildElementRecursive
Gets the first child of startElement or it's children that is an instance of the given class.- Parameters:
startElement- the parent elementclazz- the class to search for- Returns:
nullif no child found
-
getTitleElement
Gets the title element for this page. Returns null if one is not found.- Returns:
- the title element for this page or null if this is not one
-
executeEventHandlersIfNeeded
Looks for and executes any appropriate event handlers. Looks for body and frame tags.- Parameters:
eventType- eitherEvent.TYPE_LOAD,Event.TYPE_UNLOAD, orEvent.TYPE_BEFORE_UNLOAD- Returns:
trueif user acceptedonbeforeunload(not relevant to other events)
-
isOnbeforeunloadAccepted
public boolean isOnbeforeunloadAccepted()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Returns:
- true if the OnbeforeunloadHandler has accepted to change the page
-
isOnbeforeunloadAccepted
-
executeRefreshIfNeeded
If a refresh has been specified either through a meta tag or an HTTP response header, then perform that refresh.- Throws:
IOException- if an IO problem occurs
-
checkRecursion
private void checkRecursion() -
getRefreshStringOrNull
Returns an auto-refresh string if specified. This will look in both the meta tags and inside the HTTP response headers.- Returns:
- the auto-refresh string
-
executeDeferredScriptsIfNeeded
private void executeDeferredScriptsIfNeeded()Executes any deferred scripts, if necessary. -
deregisterFramesIfNeeded
public void deregisterFramesIfNeeded()Deregister frames that are no longer in use. -
getFrames
Returns a list containing all the frames (from frame and iframe tags) in this page.- Returns:
- a list of
FrameWindow
-
getFrameByName
Returns the first frame contained in this page with the specified name.- Parameters:
name- the name to search for- Returns:
- the first frame found
- Throws:
ElementNotFoundException- If no frame exist in this page with the specified name.
-
pressAccessKey
Simulate pressing an access key. This may change the focus, may click buttons and may invoke JavaScript.- Parameters:
accessKey- the key that will be pressed- Returns:
- the element that has the focus after pressing this access key or null if no element has the focus.
- Throws:
IOException- if an IO error occurs during the processing of this access key (this would only happen if the access key triggered a button which in turn caused a page load)
-
tabToNextElement
Move the focus to the next element in the tab order. To determine the specified tab order, refer togetTabbableElements()- Returns:
- the element that has focus after calling this method
-
tabToPreviousElement
Move the focus to the previous element in the tab order. To determine the specified tab order, refer togetTabbableElements()- Returns:
- the element that has focus after calling this method
-
getHtmlElementById
public <E extends HtmlElement> E getHtmlElementById(String elementId) throws ElementNotFoundException Returns the HTML element with the specified ID. If more than one element has this ID (not allowed by the HTML spec), then this method returns the first one.- Type Parameters:
E- the element type- Parameters:
elementId- the ID value to search for- Returns:
- the HTML element with the specified ID
- Throws:
ElementNotFoundException- if no element was found matching the specified ID
-
getElementsById
Returns the elements with the specified ID. If there are no elements with the specified ID, this method returns an empty list. Please note that the lists returned by this method are immutable.- Parameters:
elementId- the ID value to search for- Returns:
- the elements with the specified name attribute
-
getElementByName
Returns the element with the specified name. If more than one element has this name, then this method returns the first one.- Type Parameters:
E- the element type- Parameters:
name- the name value to search for- Returns:
- the element with the specified name
- Throws:
ElementNotFoundException- if no element was found matching the specified name
-
getElementsByName
Returns the elements with the specified name attribute. If there are no elements with the specified name, this method returns an empty list. Please note that the lists returned by this method are immutable.- Parameters:
name- the name value to search for- Returns:
- the elements with the specified name attribute
-
getElementsByIdAndOrName
Returns the elements with the specified string for their name or ID. If there are no elements with the specified name or ID, this method returns an empty list.- Parameters:
idAndOrName- the value to search for- Returns:
- the elements with the specified string for their name or ID
-
notifyNodeAdded
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
node- the node that has just been added to the document
-
notifyNodeRemoved
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
node- the node that has just been removed from the tree
-
addMappedElement
Adds an element to the ID and name maps, if necessary.- Parameters:
element- the element to be added to the ID and name maps
-
addMappedElement
Adds an element to the ID and name maps, if necessary.- Parameters:
element- the element to be added to the ID and name mapsrecurse- indicates if children must be added too
-
addElement
private void addElement(Map<String, SortedSet<DomElement>> map, DomElement element, String attribute, boolean recurse) -
removeMappedElement
Removes an element from the ID and name maps, if necessary.- Parameters:
element- the element to be removed from the ID and name maps
-
removeMappedElement
Removes an element and optionally its children from the ID and name maps, if necessary.- Parameters:
element- the element to be removed from the ID and name mapsrecurse- indicates if children must be removed toodescendant- indicates of the element was descendant of this HtmlPage, but now its parent might be null
-
removeElement
private void removeElement(Map<String, SortedSet<DomElement>> map, DomElement element, String attribute, boolean recurse) -
isMappedElement
Indicates if the attribute name indicates that the owning element is mapped.- Parameters:
document- the owning documentattributeName- the name of the attribute to consider- Returns:
trueif the owning element should be mapped in its owning page
-
calculateBase
private void calculateBase() -
loadFrames
Loads the content of the contained frames. This is done after the page is completely loaded, to allow script contained in the frames to reference elements from the page located after the closing </frame> tag.- Throws:
FailingHttpStatusCodeException- if the server returns a failing status code AND the propertyWebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)is set totrue
-
toString
-
getMetaTags
-
clone
-
cloneNode
-
addHtmlAttributeChangeListener
Adds an HtmlAttributeChangeListener to the listener list. The listener is registered for all attributes of all HtmlElements contained in this page.- Parameters:
listener- the attribute change listener to be added- See Also:
-
removeHtmlAttributeChangeListener
Removes an HtmlAttributeChangeListener from the listener list. This method should be used to remove HtmlAttributeChangeListener that were registered for all attributes of all HtmlElements contained in this page.- Parameters:
listener- the attribute change listener to be removed- See Also:
-
fireHtmlAttributeAdded
Notifies all registered listeners for the given event to add an attribute.- Parameters:
event- the event to fire
-
fireHtmlAttributeReplaced
Notifies all registered listeners for the given event to replace an attribute.- Parameters:
event- the event to fire
-
fireHtmlAttributeRemoved
Notifies all registered listeners for the given event to remove an attribute.- Parameters:
event- the event to fire
-
safeGetAttributeListeners
-
checkChildHierarchy
Check for insertion errors for a new child node. This is overridden by derived classes to enforce which types of children are allowed.- Overrides:
checkChildHierarchyin classDomNode- Parameters:
newChild- the new child node that is being inserted below this node- Throws:
DOMException- HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors or this node itself, or if this node is of type Document and the DOM application attempts to insert a second DocumentType or Element node. WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
-
isBeingParsed
public boolean isBeingParsed()Returnstrueif an HTML parser is operating on this page, adding content to it.- Returns:
trueif an HTML parser is operating on this page, adding content to it
-
registerParsingStart
public void registerParsingStart()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing some content for this page. -
registerParsingEnd
public void registerParsingEnd()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing some content for this page. -
isParsingHtmlSnippet
public boolean isParsingHtmlSnippet()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returnstrueif an HTML parser is parsing a non-inline HTML snippet to add content to this page. Non-inline content is content that is parsed for the page, but not in the same stream as the page itself -- basically anything other thandocument.write()ordocument.writeln():innerHTML,outerHTML,document.createElement(), etc.- Returns:
trueif an HTML parser is parsing a non-inline HTML snippet to add content to this page
-
registerSnippetParsingStart
public void registerSnippetParsingStart()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing a non-inline HTML snippet. -
registerSnippetParsingEnd
public void registerSnippetParsingEnd()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing a non-inline HTML snippet. -
isParsingInlineHtmlSnippet
public boolean isParsingInlineHtmlSnippet()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returnstrueif an HTML parser is parsing an inline HTML snippet to add content to this page. Inline content is content inserted into the parser stream dynamically while the page is being parsed (i.e.document.write()ordocument.writeln()).- Returns:
trueif an HTML parser is parsing an inline HTML snippet to add content to this page
-
registerInlineSnippetParsingStart
public void registerInlineSnippetParsingStart()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has started parsing an inline HTML snippet. -
registerInlineSnippetParsingEnd
public void registerInlineSnippetParsingEnd()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Called by the HTML parser to let the page know that it has finished parsing an inline HTML snippet. -
refresh
Refreshes the page by sending the same parameters as previously sent to get this page.- Returns:
- the newly loaded page.
- Throws:
IOException- if an IO problem occurs
-
writeInParsedStream
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Parses the given string as would it belong to the content being parsed at the current parsing position
- Parameters:
string- the HTML code to write in place
-
setDOMBuilder
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Sets the builder to allow page to send content from document.write(ln) calls.- Parameters:
htmlUnitDOMBuilder- the builder
-
getDOMBuilder
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the current builder.- Returns:
- the current builder
-
getNamespaces
-
setDocumentType
Sets the document type.- Overrides:
setDocumentTypein classSgmlPage- Parameters:
type- the document type
-
save
Saves the current page, with all images, to the specified location. The default behavior removes all script elements.- Parameters:
file- file to write this page into- Throws:
IOException- If an error occurs
-
isQuirksMode
public boolean isQuirksMode()Returns whether the current page mode is inquirks modeor instandards mode.- Returns:
- true for
quirks mode, false forstandards mode
-
isAttachedToPage
public boolean isAttachedToPage()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates if this node is currently attached to the page.- Overrides:
isAttachedToPagein classDomNode- Returns:
trueif the page is one ancestor of the node.
-
isHtmlPage
public boolean isHtmlPage()Returns true if this page is an HtmlPage.- Specified by:
isHtmlPagein interfacePage- Overrides:
isHtmlPagein classSgmlPage- Returns:
- true or false
-
getBaseURL
-
addAutoCloseable
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Adds anAutoCloseable, which would be closed during thecleanUp().- Parameters:
autoCloseable- the autoclosable
-
handles
-
setElementFromPointHandler
Sets theElementFromPointHandler.- Parameters:
elementFromPointHandler- the handler
-
getElementFromPoint
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the element for the specified x coordinate and the specified y coordinate.- Parameters:
x- the x offset, in pixelsy- the y offset, in pixels- Returns:
- the element for the specified x coordinate and the specified y coordinate
-
setFocusedElement
Moves the focus to the specified element. This will trigger any relevant JavaScript event handlers.- Parameters:
newElement- the element that will receive the focus, usenullto remove focus from any element- Returns:
- true if the specified element now has the focus
- See Also:
-
setFocusedElement
Moves the focus to the specified element. This will trigger any relevant JavaScript event handlers.- Parameters:
newElement- the element that will receive the focus, usenullto remove focus from any elementwindowActivated- - whether the enclosing window got focus resulting in specified element getting focus- Returns:
- true if the specified element now has the focus
- See Also:
-
getFocusedElement
Returns the element with the focus or null if no element has the focus.- Returns:
- the element with focus or null
- See Also:
-
setElementWithFocus
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Sets the element with focus.- Parameters:
elementWithFocus- the element with focus
-
getActiveElement
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
- Returns:
- the element with focus or the body
-
getSelectionRanges
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Returns the page's current selection ranges.
- Returns:
- the page's current selection ranges
-
setSelectionRange
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Makes the specified selection range the *only* selection range on this page.
- Parameters:
selectionRange- the selection range
-
executeJavaScriptFunction
public ScriptResult executeJavaScriptFunction(Object function, Object thisObject, Object[] args, DomNode htmlElementScope) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Execute a Function in the given context.- Parameters:
function- the JavaScript Function to callthisObject- the "this" object to be used during invocationargs- the arguments to pass into the callhtmlElementScope- the HTML element for which this script is being executed This element will be the context during the JavaScript execution. If null, the context will default to the page.- Returns:
- a ScriptResult which will contain both the current page (which may be different than the previous page and a JavaScript result object.
-
executeJavaScriptFunction
private ScriptResult executeJavaScriptFunction(org.htmlunit.corejs.javascript.Function function, org.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args, DomNode htmlElementScope) -
writeObject
- Throws:
IOException
-
readObject
- Throws:
ClassNotFoundExceptionIOException
-
setNodeValue
-
setPrefix
-
clearComputedStyles
public void clearComputedStyles()INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles.- Overrides:
clearComputedStylesin classSgmlPage
-
clearComputedStyles
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles for a specificElement.- Overrides:
clearComputedStylesin classSgmlPage- Parameters:
element- the element to clear its cache
-
clearComputedStylesUpToRoot
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Clears the computed styles for a specificElementand all parent elements.- Overrides:
clearComputedStylesUpToRootin classSgmlPage- Parameters:
element- the element to clear its cache
-
getStyleFromCache
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Parameters:
element- the element to clear its cachenormalizedPseudo- the pseudo attribute- Returns:
- the cached CSS2Properties object or null
-
putStyleIntoCache
public void putStyleIntoCache(DomElement element, String normalizedPseudo, ComputedCssStyleDeclaration style) INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Caches a CSS2Properties object.- Parameters:
element- the element to clear its cachenormalizedPseudo- the pseudo attributestyle- the CSS2Properties to cache
-
getStyleSheets
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.- Returns:
- a list of all styles from this page (<style> and <link rel=stylesheet>). This returns an empty list if css support is disabled in the web client options.
-
getCssPropertiesCache
- Returns:
- the CSSPropertiesCache for this page
-