Reputation: 94
Trying to get search results on the IMDb website. Code is the following:
public static void main(String[] args) throws IOException
{
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
HtmlPage page1 = webClient.getPage("http://www.imdb.com");
HtmlInput input1 = page1.getElementByName("q");
input1.setValueAttribute("Teenage Mutant Ninja Turtles");
HtmlSubmitInput submit = page1.getElementByName("navbar-submit-button");
page1 = submit.click();
System.out.println(page1.asText());
webClient.closeAllWindows();
}
The output I get is:
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[navbar-find] at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementByName(HtmlPage.java:1747) at main.Main.main(Main.java:37) Java Result: 1
The id from the submit button is: navbar-submit-button.
The source code is this:
<button id="navbar-submit-button" class="primary btn" type="submit"><div class="magnifyingglass navbarSprite"></div></button>
Thank you in advance.
Upvotes: 0
Views: 61
Reputation: 94
The way it worked for me, was to use XPath. This was I could just choose the id. Went great. Thanks for your help.
Upvotes: 0
Reputation: 2037
There are two important situations in your example:
1 - You need an element named q
because of the line:
HtmlInput input1 = page1.getElementByName("q");
2 - You have an element with id="navbar-submit-button"
, so you have to use getElementById:
HtmlSubmitInput submit = page1.getElementById("navbar-submit-button");
Upvotes: 2