昨天我发布了这个Retrieving Data in Java。我很好奇有可能在Web浏览器打开时运行java程序,然后让它在网站上运行。如果我在浏览器上打开Facebook,是否可以在状态框中键入当前时间,然后单击发布?或者说我让程序能够从用户那里获取输入(可能使用扫描仪?),然后根据输入,它可以加载谷歌,将其输入搜索栏然后点击搜索。
答案 0 :(得分:4)
您可以使用Selenium:
执行此操作Selenium自动化浏览器。而已。你用这种力量做的是 完全取决于你。主要用于自动化Web应用程序 用于测试目的,但当然不仅限于此。 无聊的基于Web的管理任务可以(也应该!) 也是自动化的。
这是来自documentation page的示例,它在Google上搜索“Cheese”一词:
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("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());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}