java 网络冲浪/浏览器自动化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6363350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Surfing/Browser Automation
提问by Jimmy Huch
I am in the process of creating a Java program that goes on the internet, signs in to website accounts and posts stuff. E.g. Run Program - > Tumblr - > Post "Helow World" -> Log Out of Tumblr.
我正在创建一个可以在 Internet 上运行、登录网站帐户并发布内容的 Java 程序。例如运行程序 -> Tumblr -> 发布“Helow World” -> 退出 Tumblr。
I am currently using the Robot class to do this... http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html
我目前正在使用 Robot 类来执行此操作... http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html
But looking ahead, I see a daunting future (web page updates will crash the program because it is based on coordinates, mouse clicks and keyboard.)
但展望未来,我看到了令人生畏的未来(网页更新会使程序崩溃,因为它基于坐标、鼠标点击和键盘。)
Is there someway I can do web browser automation? (e.g. surfing to websites, filling out forms etc.) (preferably in Java, python, C++ or php)
有什么办法可以使网络浏览器自动化吗?(例如浏览网站、填写表格等)(最好使用 Java、python、C++ 或 php)
回答by cmwright
Selenium is a great option for exactly what you need. Not only can you write scripts for it in java (as well as many other languages) but you can install the browser plugin and have it record your actions to quickly learn it's syntax.
Selenium 是满足您需求的绝佳选择。您不仅可以用 Java(以及许多其他语言)为其编写脚本,还可以安装浏览器插件并让它记录您的操作以快速了解其语法。
回答by Brian Agnew
Watijis a Java-based web testing framework that will drive a web browser. Although it's nominally for testing, it can do what you want. You can intelligently search for button/controls to drive, and because it's controlling the browser, all the client side functionality (scripts etc.) will be triggered correctly.
Watij是一个基于 Java 的 Web 测试框架,它将驱动 Web 浏览器。虽然它名义上是用于测试,但它可以做你想做的。您可以智能地搜索要驱动的按钮/控件,并且因为它控制浏览器,所以所有客户端功能(脚本等)都将被正确触发。
回答by Simarjot Singh Jassal
You can also try JExplorer Teamdev: Jexplorer. But it's not for free unless you are a student or working in an Open Source community. This tool uses swings to simulate the Internet Explorer. Watij is based on JExplorer
你也可以试试 JExplorer Teamdev: Jexplorer。但除非您是学生或在开源社区工作,否则它不是免费的。这个工具使用swings来模拟Internet Explorer。Watij 基于 JExplorer
回答by Guru
You can use HTMLUnit to program java code to simulate browser usage.
您可以使用 HTMLUnit 编写 java 代码来模拟浏览器的使用。
download jar file at : http://www.java2s.com/Code/Jar/h/Downloadhtmlunit211jar.htm
在以下位置下载 jar 文件:http: //www.java2s.com/Code/Jar/h/Downloadhtmlunit211jar.htm
Get Started at : http://htmlunit.sourceforge.net/gettingStarted.html
回答by Magor_k
hy i wanted to have a same program as you which is surfing on the internet.
hy 我想有一个和你一样的程序,它在互联网上冲浪。
i used selenium in chrome. If you want to use selenium you have to download from http://www.seleniumhq.org/download/--- the latest version and implement in neatbeans or eclipse the jar files. (Selenium Client & WebDriver Language Bindings, Selenium Standalone Server) After this you have to download from google https://sites.google.com/a/chromium.org/chromedriver/-- chrome driver also the latest version extract the file and save on your pc. Here is the code what i used:
我在铬中使用了硒。如果你想使用 selenium,你必须从http://www.seleniumhq.org/download/下载 最新版本并在neatbeans 或 eclipse 中实现 jar 文件。(Selenium Client & WebDriver Language Bindings, Selenium Standalone Server) 在这之后你必须从谷歌下载https://sites.google.com/a/chromium.org/chromedriver/-- chrome 驱动程序也是最新版本的文件和保存在您的电脑上。这是我使用的代码:
package teszt;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Teszt {
public static void main(String[] args) {
String exePath = "C:\Users\Magor\Downloads\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}}