Refine search
DomNodeList<DomElement> domNodeList = page.getElementsByTagName("a"); if (domElement.getTextContent().equals("Surge")) { System.out.println(domElement.getTextContent()); return true; try { webClient.waitForBackgroundJavaScript(500); tempPage = domElement.click(); Thread.sleep(500); DomElement surge_url = tempPage.getElementById("surge_url"); if (surge_url != null) { String href = surge_url.getAttribute("href"); System.out.println(href);
/** * Returns the tag name of this element. * @return the tag name of this element */ @Override public final String getTagName() { return getNodeName(); }
public String getAttribute(String name) { if ("value".equals(name) && element instanceof HtmlOption) { // for options, we want text if no value was specified return ((HtmlOption) element).getValueAttribute(); } else { if (!element.hasAttribute(name)) return null; return element.getAttribute(name); } }
/** * This method implements the control state update part of the click action. * * <p>The default implementation only calls doClickStateUpdate on parent's DomElement (if any). * Subclasses requiring different behavior (like {@link HtmlSubmitInput}) will override this method.</p> * @param shiftKey {@code true} if SHIFT is pressed * @param ctrlKey {@code true} if CTRL is pressed * * @return true if doClickFireEvent method has to be called later on (to signal, * that the value was changed) * @throws IOException if an IO error occurs */ protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException { if (propagateClickStateUpdateToParent()) { // needed for instance to perform link doClickAction when a nested element is clicked // it should probably be changed to do this at the event level but currently // this wouldn't work with JS disabled as events are propagated in the host object tree. final DomNode parent = getParentNode(); if (parent instanceof DomElement) { return ((DomElement) parent).doClickStateUpdate(false, false); } } return false; }
/** * Sets the focus on this element. */ public void focus() { final HtmlPage page = (HtmlPage) getPage(); page.setFocusedElement(this); final Object o = getScriptableObject(); if (o instanceof HTMLElement) { ((HTMLElement) o).setActive(); } }
/** * Recursively checks whether "xml:space" attribute is set to "default". * @param node node to start checking from * @return {@link Boolean#TRUE} if "default" is set, {@link Boolean#FALSE} for other value, * or null if nothing is set. */ private static Boolean isXMLSpaceDefault(DomNode node) { for ( ; node instanceof DomElement; node = node.getParentNode()) { final String value = ((DomElement) node).getAttribute("xml:space"); if (!value.isEmpty()) { return "default".equals(value); } } return null; }
final String currentNodeName = currentNode.getNodeName(); final String newNodeName = newElement.getNodeName(); final String parentNodeName = parent.getNodeName(); parent.appendChild(newElement);
/** * This is overridden instead of {@link #computeElements()} in order to prevent caching at all. * * {@inheritDoc} */ @Override public List<DomNode> getElements() { final List<DomNode> response = new ArrayList<>(); final DomElement domElement = (DomElement) getDomNodeOrDie(); for (DomNode parent = domElement.getParentNode(); parent != null; parent = parent.getParentNode()) { if (parent instanceof HtmlLabel) { response.add(parent); } } final String id = domElement.getId(); if (id != DomElement.ATTRIBUTE_NOT_DEFINED) { for (final DomElement label : domElement.getHtmlPageOrNull().getElementsByTagName("label")) { if (id.equals(label.getAttributeDirect("for"))) { response.add(label); } } } return response; } }
if (element.getOwnerDocument() instanceof XmlPage) { return; LOG.debug("Script node added: " + element.asXml()); final PostponedAction action = new PostponedAction(element.getPage(), "Execution of script " + element) { @Override public void execute() { final AbstractJavaScriptEngine<?> engine = element.getPage().getWebClient().getJavaScriptEngine(); if (element.hasAttribute("async") && !engine.isScriptRunning()) { final HtmlPage owningPage = element.getHtmlPageOrNull(); owningPage.addAfterLoadAction(action); else if (element.hasAttribute("async") || postponed && StringUtils.isBlank(element.getTextContent())) { engine.addPostponedAction(action);
public void checkComponentSource(URL pageName, String xmlunitPage, By pageElementToTest) throws IOException, SAXException { WebClient client = new WebClient(); client.getOptions().setJavaScriptEnabled(false); HtmlPage page = client.getPage(pageName); DomElement element; String locator = pageElementToTest.toString(); locator = locator.substring(locator.indexOf(':') + 1).trim(); if (pageElementToTest instanceof ById) { element = page.getElementById(locator); } else if (pageElementToTest instanceof ByTagName) { element = page.getElementsByTagName(locator).get(0); } else { throw new IllegalArgumentException("Only id and name are supported"); } String pageCode = element.asXml(); checkXmlStructure(xmlunitPage, pageCode); }
/** * Search for the namespace URI of the given prefix, starting from the specified element. * @param element the element to start searching from * @param prefix the namespace prefix * @return the namespace URI bound to the prefix; or null if there is no such namespace */ public static String lookupNamespaceURI(final DomElement element, final String prefix) { String uri = element.getAttribute("xmlns:" + prefix); if (uri == DomElement.ATTRIBUTE_NOT_DEFINED) { final DomNode parentNode = element.getParentNode(); if (parentNode instanceof DomElement) { uri = lookupNamespaceURI((DomElement) parentNode, prefix); } } return uri; }
/** * Detaches this attribute from the parent HTML element after caching the attribute value. */ public void detachFromParent() { if (parent_ != null) { value_ = parent_.getAttribute(name_); } parent_ = null; }
/** * Gets the specified attribute. * @param attributeName attribute name * @return the value of the specified attribute, <code>null</code> if the attribute is not defined */ public Object jsxFunction_getAttribute(String attributeName) { attributeName = fixAttributeName(attributeName); final String value = getDomNodeOrDie().getAttribute(attributeName); if (value == DomElement.ATTRIBUTE_NOT_DEFINED) { if (getBrowserVersion().isIE()) { for (Scriptable object = this; object != null; object = object.getPrototype()) { final Object property = object.get(attributeName, this); if (property != NOT_FOUND) { return property; } } } return null; } return value; }
/** * Search for the namespace URI of the given prefix, starting from the specified element. * The default namespace can be searched for by specifying "" as the prefix. * @param element the element to start searching from * @param prefix the namespace prefix * @return the namespace URI bound to the prefix; or null if there is no such namespace */ public static String lookupNamespaceURI(final DomElement element, final String prefix) { String uri = DomElement.ATTRIBUTE_NOT_DEFINED; if (prefix.isEmpty()) { uri = element.getAttributeDirect("xmlns"); } else { uri = element.getAttribute("xmlns:" + prefix); } if (uri == DomElement.ATTRIBUTE_NOT_DEFINED) { final DomNode parentNode = element.getParentNode(); if (parentNode instanceof DomElement) { uri = lookupNamespaceURI((DomElement) parentNode, prefix); } } return uri; }
@Override public List<WebElement> findElementsByLinkText(String linkText) { assertElementNotStale(); String expectedText = linkText.trim(); List<? extends HtmlElement> htmlElements = ((HtmlElement) element).getHtmlElementsByTagName("a"); List<WebElement> webElements = new ArrayList<>(); for (DomElement e : htmlElements) { if (expectedText.equals(e.getTextContent().trim()) && e.getAttribute("href") != null) { webElements.add(getParent().newHtmlUnitWebElement(e)); } } return webElements; }
protected void assertElementNotStale(DomElement element) { SgmlPage elementPage = element.getPage(); Page currentPage = lastPage(); if (!currentPage.equals(elementPage)) { throw new StaleElementReferenceException( "Element appears to be stale. Did you navigate away from the page that contained it? " + " And is the current window focussed the same as the one holding this element?"); } // We need to walk the DOM to determine if the element is actually attached DomNode parentElement = element; while (parentElement != null && !(parentElement instanceof SgmlPage)) { parentElement = parentElement.getParentNode(); } if (parentElement == null) { throw new StaleElementReferenceException( "The element seems to be disconnected from the DOM. " + " This means that a user cannot interact with it."); } }
protected void printXml(final DomElement node) throws IOException { if (!isExcluded(node)) { final boolean hasChildren = node.getFirstChild() != null; builder_.append(indent_).append('<'); printOpeningTag(node); if (!hasChildren && !node.isEmptyXmlTagExpanded()) { builder_.append("/>").append('\n'); } else { builder_.append(">").append('\n'); for (DomNode child = node.getFirstChild(); child != null; child = child.getNextSibling()) { indent_.append(" "); if (child instanceof DomElement) { printXml((DomElement) child); } else { builder_.append(child); } indent_.setLength(indent_.length() - 2); } builder_.append(indent_).append("</").append(node.getTagName()).append('>').append('\n'); } } }
public IElement getParent() { DomNode p = element.getParentNode(); while (true) { if (p == null) return null; if (p instanceof HtmlElement) return new HtmlUnitElementImpl((HtmlElement) p); // get next parent p = p.getParentNode(); } }
/** * Sets the identifier this element. * * @param newId the new identifier of this element */ public final void setId(final String newId) { setAttribute("id", newId); }
private int getTopForAbsolutePositionWithInheritance() { int top = 0; final String t = getTopWithInheritance(); if (!AUTO.equals(t)) { // No need to calculate displacement caused by sibling nodes. top = pixelValue(t); } else { final String b = getBottomWithInheritance(); if (!AUTO.equals(b)) { // Estimate the vertical displacement caused by *all* siblings. // This is very rough, and doesn't even take position or display types into account. // It also doesn't take into account the fact that the parent's height may be hardcoded in CSS. top = 0; DomNode child = getElement().getDomNodeOrDie().getParentNode().getFirstChild(); while (child != null) { if (child instanceof HtmlElement && child.mayBeDisplayed()) { top += 20; } child = child.getNextSibling(); } top -= pixelValue(b); } } return top; }