Example #1
0
 public static HtmlPage trainStrength(HtmlPage page, int amount) throws IOException {
   System.out.println("Training " + amount + " strength...");
   for (DomNode n : page.getElementById("divStrength").getDescendants()) {
     // System.out.println(n.toString());
     HtmlTextInput textInput;
     HtmlSubmitInput submitInput;
     if (n.toString()
         .contains("HtmlTextInput[<input type=\"text\" value=\"1\" id=\"t\" name=\"t\">]")) {
       textInput = (HtmlTextInput) n;
       textInput.setValueAttribute(Integer.toString(amount));
       System.out.println(textInput.toString());
     } else if (n.toString()
         .contains("HtmlSubmitInput[<input type=\"submit\" value=\"Train\">]")) {
       submitInput = (HtmlSubmitInput) n;
       page = submitInput.click();
       System.out.println(submitInput.toString());
     }
   }
   /*
   System.out.println(this.user.getUsername()+"-"+"Training "+amount+" strength...");
   HtmlTextInput amountInput = (HtmlTextInput) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[1]/input[1]").get(0);
   amountInput.setValueAttribute(Integer.toString(amount));
   HtmlElement e = (HtmlElement) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[3]/input").get(0);
   e.click();
   */
   System.out.println(page.asText());
   return page;
 }
 /** {@inheritDoc} */
 @Override
 public String asXml() {
   final StringBuilder sb = new StringBuilder();
   for (final DomNode node : getChildren()) {
     sb.append(node.asXml());
   }
   return sb.toString();
 }
Example #3
0
  /**
   * Indicates if script execution is necessary and/or possible.
   *
   * @return <code>true</code> if the script should be executed
   */
  private boolean isExecutionNeeded() {
    final SgmlPage page = getPage();

    if (!isDirectlyAttachedToPage()) {
      return false;
    }

    // If JavaScript is disabled, we don't need to execute.
    if (!page.getWebClient().isJavaScriptEnabled()) {
      return false;
    }

    // If innerHTML or outerHTML is being parsed
    if (page instanceof HtmlPage && ((HtmlPage) page).isParsingHtmlSnippet()) {
      return false;
    }

    // If the script node is nested in an iframe, a noframes, or a noscript node, we don't need to
    // execute.
    for (DomNode o = this; o != null; o = o.getParentNode()) {
      if (o instanceof HtmlInlineFrame || o instanceof HtmlNoFrames) {
        return false;
      }
    }

    // If the underlying page no longer owns its window, the client has moved on (possibly
    // because another script set window.location.href), and we don't need to execute.
    if (page.getEnclosingWindow() != null && page.getEnclosingWindow().getEnclosedPage() != page) {
      return false;
    }

    // If the script language is not JavaScript, we can't execute.
    if (!isJavaScript(getTypeAttribute(), getLanguageAttribute())) {
      final String t = getTypeAttribute();
      final String l = getLanguageAttribute();
      LOG.warn(
          "Script is not JavaScript (type: " + t + ", language: " + l + "). Skipping execution.");
      return false;
    }

    // If the script's root ancestor node is not the page, the the script is not a part of the page.
    // If it isn't yet part of the page, don't execute the script; it's probably just being cloned.
    DomNode root = this;
    while (root.getParentNode() != null) {
      root = root.getParentNode();
    }
    if (root != getPage()) {
      return false;
    }

    return true;
  }
Example #4
0
 public HashMap<Integer, DomNode> buildFinalTree(HashMap<Integer, DomNode> allNodes) {
   HashMap<Integer, DomNode> finalNodeList = new HashMap<Integer, DomNode>();
   for (int i = 0; i < allNodes.size(); i++) {
     DomNode tempNode = allNodes.get(i);
     if (tempNode.getChildrenNos() != null) {
       for (int child : tempNode.getChildrenNos()) {
         tempNode.addChild(allNodes.get(child));
       }
     }
     finalNodeList.put(tempNode.getId(), tempNode);
   }
   return finalNodeList;
 }
 /**
  * <b>DOM L1</b> Returns the attribute value, with character and entity references substituted.
  * <em>NOTE: entity refs as children aren't currently handled.</em>
  */
 public String getNodeValue() {
   // If we have a simple node-value, use that
   if (first == null) {
     return (value == null) ? "" : value;
   }
   // Otherwise collect child node-values
   StringBuffer buf = new StringBuffer();
   for (DomNode ctx = first; ctx != null; ctx = ctx.next) {
     switch (ctx.nodeType) {
       case Node.TEXT_NODE:
         buf.append(ctx.getNodeValue());
         break;
       case Node.ENTITY_REFERENCE_NODE:
         // TODO
         break;
     }
   }
   return buf.toString();
 }