'page.getHtmlElementById("tsf") wont work: com.gargoylesoftware.htmlunit.ElementNotFoundException:tsf

I am trying to run the tutorial on here. The code looks like this:

public class Test {

    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX);
        HtmlPage page = client.getPage("https://google.com/");
        
        
        // Getting Form from google home page. tsf is the form name 
        HtmlForm form = page.getHtmlElementById("tsf"); // Error occurs here
        
        form.getInputByName("q").setValueAttribute("test");
    
        // Creating a virtual submit button
        HtmlButton submitButton = (HtmlButton)page.createElement("button");
        submitButton.setAttribute("type", "submit");
        form.appendChild(submitButton);
        
          // Submitting the form and getting the result 
        HtmlPage newPage = submitButton.click();
    
          // Getting the result as text

        String text = page.asNormalizedText();
        System.out.println(text);
    }
}

But I am getting error message:

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[id] attributeValue=[tsf]
    at com.gargoylesoftware.htmlunit.html.HtmlPage.getHtmlElementById(HtmlPage.java:1670)
    at Test.main(Test.java:20)

Since this tutorial is relatively old, the ID tsf might be outdated. However, if I check the form name from the google home page, I cant figure it out. Maybe I dont understand the meaning of the whole HtmlForm object. (I am completely new to this topic)



Solution 1:[1]

There is no element with ID tsf anymore. Best way to check it out is to go to the site and use Web Developer Tools of your browser (f12) mostly on every browsers. You can see the whole HTML document from there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jose