java - 是否可以浏览网站 "without a browser"?

标签 java web

我只是想知道是否可以浏览网站(没有浏览器用户界面),然后在文本框中输入文本并按下按钮。

是否有任何库可以执行此操作,或者 Java 中有任何方法可以连接到站点并输入信息并按下按钮。

最佳答案

听起来您正在寻找 Selenium .

以下是来自其 Getting Started 的 Google 搜索示例指南:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Example  {
    public static void main(String[] args) {
        // Create a new instance of the html unit driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
    }
}

关于java - 是否可以浏览网站 "without a browser"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779739/

相关文章:

java - 无法 JDBC 连接到 jdbc :mysql//localhost:3306/database

java - 多线程和 Selenium

java - 为什么当我使用 while 循环时我的代码不起作用?

web - 从 Web 浏览器发送信标信号

javascript - jquery 中的数据表不实时刷新数据

java - 如何使 JTable 列大小完全(或紧密)适合内容?

javascript - 保护静态应用程序中的数据文件访问

javascript - 不使用 javascript 或 href 打开一个新窗口

python - ModelForm 编辑数据库记录的正确方法?

java - 使用 apache POI 从 xlsx 文件读取数据时出现线程 "main"org.apache.poi.POIXMLException 异常