com.gargoylesoftware.htmlunit.ElementNotFoundException Java Examples
The following examples show how to use
com.gargoylesoftware.htmlunit.ElementNotFoundException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlPageTest.java From htmlunit with Apache License 2.0 | 6 votes |
/** * @exception Exception if the test fails */ @Test public void getElementByName() throws Exception { final String html = "<html><body>\n" + "<div id='a' name='a'>foo</div>\n" + "<div id='b1' name='b'>bar</div>\n" + "<div id='b2' name='b'>baz</div></body></html>"; final HtmlPage page = loadPage(html); assertEquals(page.getElementById("a"), page.getElementByName("a")); assertEquals(page.getElementById("b1"), page.getElementByName("b")); page.getElementByName("b").remove(); assertEquals(page.getElementById("b2"), page.getElementByName("b")); boolean thrown = false; try { page.getElementByName("c"); } catch (final ElementNotFoundException e) { thrown = true; } assertTrue(thrown); }
Example #2
Source File: Document.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Create a new DOM text node with the given data. * * @param newData the string value for the text node * @return the new text node or NOT_FOUND if there is an error */ @JsxFunction public Object createTextNode(final String newData) { Object result = NOT_FOUND; try { final DomNode domNode = new DomText(getDomNodeOrDie().getPage(), newData); final Object jsElement = getScriptableFor(domNode); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug("createTextNode(" + newData + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domNode.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
Example #3
Source File: HtmlLabel.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Gets the element referenced by this label. That is the element in the page which id is * equal to the value of the for attribute of this label. * @return the element, {@code null} if not found */ public HtmlElement getReferencedElement() { final String elementId = getForAttribute(); if (!ATTRIBUTE_NOT_DEFINED.equals(elementId)) { try { return ((HtmlPage) getPage()).getHtmlElementById(elementId); } catch (final ElementNotFoundException e) { return null; } } for (final DomNode element : getChildren()) { if (element instanceof HtmlInput) { return (HtmlInput) element; } } return null; }
Example #4
Source File: HtmlElement.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Searches for an element based on the specified criteria, returning the first element which matches * said criteria. Only elements which are descendants of this element are included in the search. * * @param elementName the name of the element to search for * @param attributeName the name of the attribute to search for * @param attributeValue the value of the attribute to search for * @param <E> the sub-element type * @return the first element which matches the specified search criteria * @throws ElementNotFoundException if no element matches the specified search criteria */ public final <E extends HtmlElement> E getOneHtmlElementByAttribute(final String elementName, final String attributeName, final String attributeValue) throws ElementNotFoundException { WebAssert.notNull("elementName", elementName); WebAssert.notNull("attributeName", attributeName); WebAssert.notNull("attributeValue", attributeValue); final List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue); if (list.isEmpty()) { throw new ElementNotFoundException(elementName, attributeName, attributeValue); } return list.get(0); }
Example #5
Source File: HtmlSelect.java From htmlunit with Apache License 2.0 | 6 votes |
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * Sets the "selected" state of the specified option. If this "select" element * is single-select, then calling this method will deselect all other options. * * Only options that are actually in the document may be selected. * * @param isSelected true if the option is to become selected * @param optionValue the value of the option that is to change * @param invokeOnFocus whether to set focus or not. * @param <P> the page type * @return the page contained in the current window as returned * by {@link com.gargoylesoftware.htmlunit.WebClient#getCurrentWindow()} */ @SuppressWarnings("unchecked") public <P extends Page> P setSelectedAttribute(final String optionValue, final boolean isSelected, final boolean invokeOnFocus) { try { final boolean attributeOnly = hasFeature(JS_SELECT_SET_VALUES_CHECKS_ONLY_VALUE_ATTRIBUTE) && !optionValue.isEmpty(); final HtmlOption selected; if (attributeOnly) { selected = getOptionByValueStrict(optionValue); } else { selected = getOptionByValue(optionValue); } return setSelectedAttribute(selected, isSelected, invokeOnFocus, true, false, true); } catch (final ElementNotFoundException e) { for (final HtmlOption o : getSelectedOptions()) { o.setSelected(false); } return (P) getPage(); } }
Example #6
Source File: XMLDOMDocument.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Creates a text node that contains the supplied data. * @param data the value to be supplied to the new text object's <code>nodeValue</code> property * @return the new text object or <code>NOT_FOUND</code> if there is an error */ @JsxFunction public Object createTextNode(final String data) { Object result = NOT_FOUND; try { final DomText domText = new DomText(getPage(), data); final Object jsElement = getScriptableFor(domText); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug("createTextNode(" + data + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domText.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
Example #7
Source File: XMLDOMDocument.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Creates a text node that contains the supplied data. * @param data the value to be supplied to the new text object's <code>nodeValue</code> property * @return the new text object or <code>NOT_FOUND</code> if there is an error */ @JsxFunction public Object createTextNode(final String data) { Object result = NOT_FOUND; try { final DomText domText = new DomText(getPage(), data); final Object jsElement = getScriptableFor(domText); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug("createTextNode(" + data + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domText.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
Example #8
Source File: HtmlSelect.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * Sets the "selected" state of the specified option. If this "select" element * is single-select, then calling this method will deselect all other options. * * Only options that are actually in the document may be selected. * * @param isSelected true if the option is to become selected * @param optionValue the value of the option that is to change * @param invokeOnFocus whether to set focus or not. * @param <P> the page type * @return the page contained in the current window as returned * by {@link com.gargoylesoftware.htmlunit.WebClient#getCurrentWindow()} */ @SuppressWarnings("unchecked") public <P extends Page> P setSelectedAttribute(final String optionValue, final boolean isSelected, final boolean invokeOnFocus) { try { final boolean attributeOnly = hasFeature(JS_SELECT_SET_VALUES_CHECKS_ONLY_VALUE_ATTRIBUTE) && !optionValue.isEmpty(); final HtmlOption selected; if (attributeOnly) { selected = getOptionByValueStrict(optionValue); } else { selected = getOptionByValue(optionValue); } return setSelectedAttribute(selected, isSelected, invokeOnFocus, true, false, true); } catch (final ElementNotFoundException e) { if (hasFeature(SELECT_DESELECT_ALL_IF_SWITCHING_UNKNOWN)) { for (final HtmlOption o : getSelectedOptions()) { o.setSelected(false); } } return (P) getPage(); } }
Example #9
Source File: HtmlElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Searches for an element based on the specified criteria, returning the first element which matches * said criteria. Only elements which are descendants of this element are included in the search. * * @param elementName the name of the element to search for * @param attributeName the name of the attribute to search for * @param attributeValue the value of the attribute to search for * @param <E> the sub-element type * @return the first element which matches the specified search criteria * @throws ElementNotFoundException if no element matches the specified search criteria */ public final <E extends HtmlElement> E getOneHtmlElementByAttribute(final String elementName, final String attributeName, final String attributeValue) throws ElementNotFoundException { WebAssert.notNull("elementName", elementName); WebAssert.notNull("attributeName", attributeName); WebAssert.notNull("attributeValue", attributeValue); final List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue); if (list.isEmpty()) { throw new ElementNotFoundException(elementName, attributeName, attributeValue); } return list.get(0); }
Example #10
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Create a new DOM text node with the given data. * * @param newData the string value for the text node * @return the new text node or NOT_FOUND if there is an error */ @JsxFunction public Object createTextNode(final String newData) { Object result = NOT_FOUND; try { final DomNode domNode = new DomText(getDomNodeOrDie().getPage(), newData); final Object jsElement = getScriptableFor(domNode); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug("createTextNode(" + newData + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domNode.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
Example #11
Source File: HtmlPage.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the first anchor with the specified text. * @param text the text to search for * @return the first anchor that was found * @throws ElementNotFoundException if no anchors are found with the specified text */ public HtmlAnchor getAnchorByText(final String text) throws ElementNotFoundException { WebAssert.notNull("text", text); for (final HtmlAnchor anchor : getAnchors()) { if (text.equals(anchor.asText())) { return anchor; } } throw new ElementNotFoundException("a", "<text>", text); }
Example #12
Source File: HtmlPageTest.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Verifies that a cloned HtmlPage has its own "idMap_". * @throws Exception if the test fails */ @Test public void clonedPageHasOwnIdMap() throws Exception { final String content = "<html><head><title>foo</title>" + "<body>" + "<div id='id1' class='cl1'><div id='id2' class='cl2'></div></div>" + "</body></html>"; final HtmlPage page = loadPage(content); final HtmlElement id1 = (HtmlElement) page.getDocumentElement().getLastChild().getLastChild(); assertEquals("id1", id1.getId()); assertSame(id1, page.getHtmlElementById("id1")); final HtmlPage clone = page.cloneNode(true); assertSame(id1, page.getHtmlElementById("id1")); final HtmlElement id1clone = (HtmlElement) clone.getDocumentElement().getLastChild().getLastChild(); assertNotSame(id1, id1clone); assertEquals("id1", id1clone.getId()); assertSame(id1clone, clone.getHtmlElementById("id1")); assertNotSame(id1clone, page.getHtmlElementById("id1")); assertNotSame(id1, clone.getHtmlElementById("id1")); page.getHtmlElementById("id2").remove(); try { page.getHtmlElementById("id2"); fail("should have thrown ElementNotFoundException"); } catch (final ElementNotFoundException enfe) { // expected } assertNotNull(clone.getHtmlElementById("id2")); }
Example #13
Source File: XMLDOMDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Creates an element node using the specified name. * @param tagName the name for the new element node * @return the new element object or <code>NOT_FOUND</code> if the tag is not supported */ @JsxFunction public Object createElement(final String tagName) { if (tagName == null || "null".equals(tagName)) { throw Context.reportRuntimeError("Type mismatch."); } if (StringUtils.isBlank(tagName) || tagName.indexOf('<') >= 0 || tagName.indexOf('>') >= 0) { throw Context.reportRuntimeError("To create a node of type ELEMENT a valid name must be given."); } Object result = NOT_FOUND; try { final DomElement domElement = (DomElement) getPage().createElement(tagName); final Object jsElement = getScriptableFor(domElement); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug("createElement(" + tagName + ") cannot return a result as there isn't a JavaScript object for the element " + domElement.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
Example #14
Source File: HtmlSelect.java From htmlunit with Apache License 2.0 | 5 votes |
private HtmlOption getOptionByValueStrict(final String value) throws ElementNotFoundException { WebAssert.notNull("value", value); for (final HtmlOption option : getOptions()) { if (option.getAttributeDirect("value").equals(value)) { return option; } } throw new ElementNotFoundException("option", "value", value); }
Example #15
Source File: PropertyTable.java From warnings-ng-plugin with MIT License | 5 votes |
/** * Returns whether the specified property table is visible on the specified page. * * @param page * the whole details HTML page * @param property * the property table to extract * * @return {@code true} if the table is visible, {@code false} otherwise */ public static boolean isVisible(final HtmlPage page, final String property) { try { getTitleOfTable(page, property); return true; } catch (ElementNotFoundException e) { return false; } }
Example #16
Source File: HtmlFormUtil.java From jenkins-test-harness with MIT License | 5 votes |
/** * Returns all the {@code <input type="submit">} elements in this form. */ public static List<HtmlElement> getSubmitButtons(final HtmlForm htmlForm) throws ElementNotFoundException { final List<HtmlElement> list = htmlForm.getElementsByAttribute("input", "type", "submit"); // collect inputs from lost children for (final HtmlElement elt : htmlForm.getLostChildren()) { list.add(elt); } return list; }
Example #17
Source File: HtmlFormUtil.java From jenkins-test-harness with MIT License | 5 votes |
/** * Gets the first {@code <input type="submit">} element in this form. */ public static HtmlElement getSubmitButton(final HtmlForm htmlForm) throws ElementNotFoundException { List<HtmlElement> submitButtons = getSubmitButtons(htmlForm); if (!submitButtons.isEmpty()) { return submitButtons.get(0); } for (HtmlElement element : htmlForm.getElementsByTagName("button")) { if(element instanceof HtmlButton) { return element; } } return null; }
Example #18
Source File: HtmlSelect.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the {@link HtmlOption} object that has the specified text. * * @param text the text to search by * @return the {@link HtmlOption} object that has the specified text * @exception ElementNotFoundException If a particular element could not be found in the DOM model */ public HtmlOption getOptionByText(final String text) throws ElementNotFoundException { WebAssert.notNull("text", text); for (final HtmlOption option : getOptions()) { if (option.getText().equals(text)) { return option; } } throw new ElementNotFoundException("option", "text", text); }
Example #19
Source File: HtmlFormUtil.java From jenkins-test-harness with MIT License | 5 votes |
/** * Get the form button having the specified text/caption. * @param htmlForm The form containing the button. * @param caption The button text/caption being searched for. * @return The button if found. * @throws ElementNotFoundException Failed to find the button on the form. */ public static HtmlButton getButtonByCaption(final HtmlForm htmlForm, final String caption) throws ElementNotFoundException { for (HtmlElement b : htmlForm.getElementsByTagName("button")) { if(b instanceof HtmlButton && b.getTextContent().trim().equals(caption)) { return (HtmlButton) b; } } throw new ElementNotFoundException("button", "caption", caption); }
Example #20
Source File: HtmlFormTest.java From htmlunit with Apache License 2.0 | 5 votes |
/** * @throws Exception if the test fails */ @Test public void getInputByValue() throws Exception { final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <input type='submit' name='button' value='xxx'/>\n" + " <input type='text' name='textfield' value='foo'/>\n" + " <input type='submit' name='button1' value='foo'/>\n" + " <input type='reset' name='button2' value='foo'/>\n" + " <input type='submit' name='button' value='bar'/>\n" + "</form></body></html>"; final HtmlPage page = loadPage(html); final HtmlForm form = page.getHtmlElementById("form1"); final List<String> actualInputs = new ArrayList<>(); for (final HtmlInput input : form.getInputsByValue("foo")) { actualInputs.add(input.getNameAttribute()); } final String[] expectedInputs = {"textfield", "button1", "button2"}; assertEquals("Get all", expectedInputs, actualInputs); assertEquals(Collections.EMPTY_LIST, form.getInputsByValue("none-matching")); assertEquals("Get first", "button", form.getInputByValue("bar").getNameAttribute()); try { form.getInputByValue("none-matching"); fail("Expected ElementNotFoundException"); } catch (final ElementNotFoundException e) { // Expected path. } }
Example #21
Source File: HtmlFormTest.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Test that {@link HtmlForm#getTextAreaByName(String)} returns * the first textarea with the given name. * * @throws Exception if the test page can't be loaded */ @Test public void getTextAreaByName() throws Exception { final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <textarea id='ta1_1' name='ta1'>hello</textarea>\n" + " <textarea id='ta1_2' name='ta1'>world</textarea>\n" + " <textarea id='ta2_1' name='ta2'>!</textarea>\n" + "</form></body></html>"; final HtmlPage page = loadPage(html); final HtmlForm form = page.getHtmlElementById("form1"); assertEquals("First textarea with name 'ta1'", page.getElementById("ta1_1"), form.getTextAreaByName("ta1")); assertEquals("First textarea with name 'ta2'", page.getElementById("ta2_1"), form.getTextAreaByName("ta2")); try { form.getTextAreaByName("ta3"); fail("Expected ElementNotFoundException as there is no textarea with name 'ta3'"); } catch (final ElementNotFoundException e) { // pass: exception is expected } }
Example #22
Source File: HtmlFormTest.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Test that {@link HtmlForm#getButtonByName(String)} returns * the first button with the given name. * * @throws Exception if the test page can't be loaded */ @Test public void getButtonByName() throws Exception { final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <button id='b1_1' name='b1' value='hello' type='button'/>\n" + " <button id='b1_2' name='b1' value='world' type='button'/>\n" + " <button id='b2_1' name='b2' value='!' type='button'/>\n" + "</form></body></html>"; final HtmlPage page = loadPage(html); final HtmlForm form = page.getHtmlElementById("form1"); assertEquals("First button with name 'b1'", page.getElementById("b1_1"), form.getButtonByName("b1")); assertEquals("First button with name 'b2'", page.getElementById("b2_1"), form.getButtonByName("b2")); try { form.getTextAreaByName("b3"); fail("Expected ElementNotFoundException as there is no button with name 'b3'"); } catch (final ElementNotFoundException e) { // pass: exception is expected } }
Example #23
Source File: HtmlForm.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first input element which is a member of this form and has the specified name. * * @param name the input name to search for * @param <I> the input type * @return the first input element which is a member of this form and has the specified name * @throws ElementNotFoundException if there is not input in this form with the specified name */ @SuppressWarnings("unchecked") public final <I extends HtmlInput> I getInputByName(final String name) throws ElementNotFoundException { final List<HtmlInput> inputs = getInputsByName(name); if (inputs.isEmpty()) { throw new ElementNotFoundException(HtmlInput.TAG_NAME, "name", name); } return (I) inputs.get(0); }
Example #24
Source File: Window.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
private static Object getFrameWindowByName(final HtmlPage page, final String name) { try { return page.getFrameByName(name).getScriptableObject(); } catch (final ElementNotFoundException e) { return NOT_FOUND; } }
Example #25
Source File: HtmlPage.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first frame contained in this page with the specified name. * @param name the name to search for * @return the first frame found * @exception ElementNotFoundException If no frame exist in this page with the specified name. */ public FrameWindow getFrameByName(final String name) throws ElementNotFoundException { for (final FrameWindow frame : getFrames()) { if (frame.getName().equals(name)) { return frame; } } throw new ElementNotFoundException("frame or iframe", "name", name); }
Example #26
Source File: HtmlSelect.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the {@link HtmlOption} object that corresponds to the specified value. * * @param value the value to search by * @return the {@link HtmlOption} object that corresponds to the specified value * @exception ElementNotFoundException If a particular element could not be found in the DOM model */ public HtmlOption getOptionByValue(final String value) throws ElementNotFoundException { WebAssert.notNull("value", value); for (final HtmlOption option : getOptions()) { if (option.getValueAttribute().equals(value)) { return option; } } throw new ElementNotFoundException("option", "value", value); }
Example #27
Source File: HtmlSelect.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
private HtmlOption getOptionByValueStrict(final String value) throws ElementNotFoundException { WebAssert.notNull("value", value); for (final HtmlOption option : getOptions()) { if (option.getAttributeDirect("value").equals(value)) { return option; } } throw new ElementNotFoundException("option", "value", value); }
Example #28
Source File: HtmlSelect.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the {@link HtmlOption} object that has the specified text. * * @param text the text to search by * @return the {@link HtmlOption} object that has the specified text * @exception ElementNotFoundException If a particular element could not be found in the DOM model */ public HtmlOption getOptionByText(final String text) throws ElementNotFoundException { WebAssert.notNull("text", text); for (final HtmlOption option : getOptions()) { if (option.getText().equals(text)) { return option; } } throw new ElementNotFoundException("option", "text", text); }
Example #29
Source File: HtmlPage.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first form that matches the specified name. * @param name the name to search for * @return the first form * @exception ElementNotFoundException If no forms match the specified result. */ public HtmlForm getFormByName(final String name) throws ElementNotFoundException { final List<HtmlForm> forms = getDocumentElement().getElementsByAttribute("form", "name", name); if (forms.isEmpty()) { throw new ElementNotFoundException("form", "name", name); } return forms.get(0); }
Example #30
Source File: HtmlPage.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first anchor with the specified text. * @param text the text to search for * @return the first anchor that was found * @throws ElementNotFoundException if no anchors are found with the specified text */ public HtmlAnchor getAnchorByText(final String text) throws ElementNotFoundException { WebAssert.notNull("text", text); for (final HtmlAnchor anchor : getAnchors()) { if (text.equals(anchor.asText())) { return anchor; } } throw new ElementNotFoundException("a", "<text>", text); }