How would I enter text into a website with java?

3.8k views Asked by At

I'm just trying to find out how to tell java to open a website and enter text (preferably a string value) into a text field

For example go to Google and search any text( it does not have to be user entered)

I realize that it wont actually open any browser or print anything from a website. I just need to know this basic part to build on for my program.

2

There are 2 answers

0
Wins On

If you try to query to Google in your example and want to get the search result, you can use query string and read its html result

1
anthoK On

Figured it out . I had to use a combination of Selenium and HtmlUnit. My code is something like this

WebDriver driver = new HtmlUnitDriver();

driver.get("https://www.google.com");

WebElement element = driver.findElement(By.name("q"));

element.sendKeys("Hello");


driver.quit();