- 浏览: 636632 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (314)
- 生活 (2)
- c# (37)
- 技术 (3)
- 400电话 (0)
- 400常见问题 (0)
- 400资费 (0)
- html (7)
- css (0)
- 数据库 (7)
- javascript (16)
- php (33)
- asp.net mvc2 (10)
- mysql (9)
- C# 3.0 LinQ (10)
- vs2005或vs2008 (4)
- flash and as3 (7)
- fms (1)
- dedeCMS (11)
- java (33)
- j2me (1)
- swing (1)
- c++ (1)
- jquery easyui (3)
- jquery (5)
- android (29)
- MongoDB (9)
- VtigerCRM (1)
- test (0)
- linux (30)
- nutch (2)
- SqlServer数据库 (2)
- 数据检索 (2)
- java抓取 (11)
- 乐天 (1)
- 淘宝 (1)
- Silverlight4.0 (6)
- sphinx实时索引 (5)
- ecshop (9)
- codeigniter(CI) (3)
- axure6 (1)
- 京东店铺装修教程 (2)
- xpath (1)
- joomla (2)
- bpm (1)
- Bootstrap (2)
- knockout (4)
- ecstore (4)
- css3 (1)
- 微信 (2)
- dede (0)
- soa_edi (1)
- odoo (0)
- web (1)
最新评论
-
骑着蜗牛超F1:
在ie6下报了个stack overflow at line ...
兼容ie6和ie7 的16进制码流在html中显示为图片代码(base64) -
冰之海洋:
好像少了一句代码吧? FloatingFunc.show(th ...
android 一直在最前面的浮动窗口效果 -
yanzhoupuzhang:
连接有问题!
iis7.0官方下载 IIS 7.0(微软Web服务器组件IIS 7.0) 官方(windows 2003,XP,2000) -
whatable:
唉,楼主你都没有搞清楚重量级和轻量级。。。。既然引用了SWT, ...
java swing 内置浏览器打开网页显示flash图表-swt Browser应用 -
yy_owen:
我晕啊,你链接的什么内容额,我要的iis,你链接个视频什么意思 ...
iis7.0官方下载 IIS 7.0(微软Web服务器组件IIS 7.0) 官方(windows 2003,XP,2000)
7.3 点击Element和填写表单
我们可以安全的加载页面,抽取信息,但是有时浏览网页是需要填写表单并提交或者点击某个元素来触发onclick以便执行一段
JavaScript代码。
嗯,要想点击一个element需要获得这个element(前面的xpathNodes也许可以帮我们做到这点),如果是input element,我们需
要调用它的submit方法,如果是anchor的话,我们家需要访问它的href里的url。如果这个元素有onclick属性,这段脚本必需首先被
执行。
填写text field,需要修改DOM tree,设置属性的值(比如HTMLInputElement)或者插入一个文本子节点(比如
HTMLTextAreaElement)。看看下面的例子就会发现这并不困难。
译注:主要有2个方法 enter 和 click。
enter给text和textArea设置值,text直接设置value属性就行了,而textArea先要删除所有子节点,然后增加一个新的text
node来包含我们想要输入的内容。
click点击一个元素,比如element, anchor submit按钮等等。
package es.ladyr.javaxpcom.browser; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.ProgressEvent; import org.eclipse.swt.browser.ProgressListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.mozilla.dom.html.HTMLDocumentImpl; import org.mozilla.dom.NodeFactory; import org.mozilla.interfaces.nsIComponentManager; import org.mozilla.interfaces.nsIDOMDocument; import org.mozilla.interfaces.nsIDOMHTMLDocument; import org.mozilla.interfaces.nsIDOMNode; import org.mozilla.interfaces.nsIDOMWindow; import org.mozilla.interfaces.nsIDOMXPathEvaluator; import org.mozilla.interfaces.nsIDOMXPathNSResolver; import org.mozilla.interfaces.nsIDOMXPathResult; import org.mozilla.interfaces.nsISupports; import org.mozilla.interfaces.nsIWebBrowser; import org.mozilla.xpcom.Mozilla; import org.w3c.dom.DOMException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.html.HTMLAnchorElement; import org.w3c.dom.html.HTMLDocument; import org.w3c.dom.html.HTMLElement; import org.w3c.dom.html.HTMLFormElement; import org.w3c.dom.html.HTMLInputElement; import org.w3c.dom.html.HTMLTextAreaElement; public class SimpleBrowserWithClick { private final static String NS_IDOMXPATHEVALUATOR_CONTRACTID = "@mozilla.org/dom/xpath-evaluator;1"; private Browser browser; // We will need SWT display to execute methods // into the SWT event thread. private Display display; // Latch used to manage page loading // Uses a count of 1, so when the browser starts loading // a page, we create a new latch, which will be // decremented when the page is loaded. private CountDownLatch latch; // Default timeout to 60 seconds private long defaultTimeout = 60000; // XPath evaluator private nsIDOMXPathEvaluator xpathEval; /** * Creates a web browser which is able to load pages waiting until * the page is completely loaded and solve xpaths returning * the corresponding nodes. * */ public SimpleBrowserWithClick (final String xulrunnerPath) { // Use a latch to wait for the browser initialization. final CountDownLatch initLatch = new CountDownLatch(1); // MozillaBrowser needs a window manager to work. We are using SWT // for the graphical interface, so we need to execute MozillaBrowser // methods into the SWT event thread. If we were use another thread, // that methods could not work properly and throw an exception, // breaking the execution flow and crashing our application. new Thread("SWT-Event-Thread") { @Override public void run() { display = new Display(); Shell shell = new Shell(display); shell.setSize(800, 600); shell.open(); // If you have XULRunner installed, you can call the constructor without // the last parameter: // // final MozillaBrowser browser = new MozillaBrowser(shell,SWT.BORDER); // // That last parameter is the path for XULRunner files // (where you have uncompressed downloaded XULRunner package). try { browser = new Browser(shell, SWT.MOZILLA); } catch (SWTError e) { System.out.println("Could not instantiate Browser: " + e.getMessage ()); return; } // Adapt browser size to shell size browser.setBounds(shell.getClientArea()); // Listens for page loading status. browser.addProgressListener(new ProgressListener() { public void changed(ProgressEvent event) { } public void completed(ProgressEvent event) { // When a page is loaded, decrement the latch, // which count will be 0 after this call. latch.countDown(); } }); // Realease the initialization latch, which has value 1, // so after this call its value will be 0. initLatch.countDown(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } System.exit(0); } }.start(); try { // Waits until the initialization latch is released. initLatch.await(); } catch (InterruptedException e) { Thread.interrupted(); } // Creates the XPath evaluator XPCOM component Mozilla moz = Mozilla.getInstance(); nsIComponentManager componentManager = moz.getComponentManager(); xpathEval = (nsIDOMXPathEvaluator) componentManager.createInstanceByContractID( NS_IDOMXPATHEVALUATOR_CONTRACTID, null, nsIDOMXPathEvaluator.NS_IDOMXPATHEVALUATOR_IID); } /** * Loads an URL into the browser and waits until the page is * totally loaded. * @param url * @throws SimpleBrowserException */ public void go(final String url) throws SimpleBrowserException { // Creates a latch with count 1 latch = new CountDownLatch(1); // Uses the SWT event thread to execute the method to // load an URL in the browser. display.syncExec(new Runnable() { public void run() { browser.setUrl(url); } }); // Waits for the finish of the page loading, or for a given // timeout in case that the loading doesn't finish in a // reasonable time. boolean timeout = waitLoad(defaultTimeout); if (timeout) { throw new SimpleBrowserException("Timeout waiting page loading."); } } /** * * @return an W3C HTML Document implementation corresponding to * the Mozilla DOM HTML document currently loaded in the browser. * @throws SimpleBrowserException */ public HTMLDocument getW3CDocument() { class DocumentGetter implements Runnable { private nsIDOMHTMLDocument htmldoc; public void run(){ nsIWebBrowser webBrowser = (nsIWebBrowser)browser.getWebBrowser(); if (webBrowser == null) { System.out.println("Could not get the nsIWebBrowser from the Browser widget"); } nsIDOMWindow dw = webBrowser.getContentDOMWindow(); nsIDOMDocument nsDoc = dw.getDocument(); htmldoc = (nsIDOMHTMLDocument) nsDoc .queryInterface (nsIDOMHTMLDocument.NS_IDOMHTMLDOCUMENT_IID); } public nsIDOMHTMLDocument getHtmldoc() { return htmldoc; }} DocumentGetter dg = new DocumentGetter(); display.syncExec(dg); return HTMLDocumentImpl.getDOMInstance(dg.getHtmldoc()); } /** * * @param xpath * @return a list with the nodes corresponding to a given xpath. * @throws SimpleBrowserException */ public List<Node> xpathNodes(String xpath) { return xPathNodes(xpath, ((HTMLDocumentImpl) getW3CDocument()).getInstance()); } /** * * @param <T> * @param xpath * @param nodeClass * @return a list of <code>nodeClass</code> nodes corresponding * to a given xpath. * @throws SimpleBrowserException */ public <T extends Node> List<T> xpathNodes(String xpath, Class<T> nodeClass) { return (List<T>)xPathNodes(xpath, ((HTMLDocumentImpl) getW3CDocument()).getInstance()); } /** * Enters the given text in a W3C input node. If the node is not * a HTMLInputElement or a HTMLTextAreaElement instance, then * a exception is thrown. * * @param node * @param text * @throws SimpleBrowserException */ public void enter(final Node node, final String text) throws SimpleBrowserException { if (node instanceof HTMLInputElement) { HTMLInputElement textComponent = (HTMLInputElement) node; enter(textComponent, text); } else if (node instanceof HTMLTextAreaElement) { HTMLTextAreaElement textComponent = (HTMLTextAreaElement) node; enter(textComponent, text); } else { throw new SimpleBrowserException( "enter only works with textfield (HTMLInputElement) or textarea (HTMLTextAreaElement)"); } } /** * Enters the given text in a HTMLInputElement. If text is * <code>null</code>, then an empty string will be inserted. * * @param inputElement * @param text * @throws SimpleBrowserException */ public void enter(final HTMLInputElement inputElement, String text) { final String inputText; if (text == null) { inputText = ""; } else { inputText = text; } display.syncExec(new Runnable() { public void run() { inputElement.setValue(inputText); } }); } /** * Enters the given text in a HTMLTextAreaElement. If text is * <code>null</code>, then an empty string will be inserted. * * @param textArea * @param text * @throws SimpleBrowserException */ public void enter(final HTMLTextAreaElement textArea, String text) { final String inputText; if (text == null) { inputText = ""; } else { inputText = text; } display.syncExec(new Runnable() { public void run() { // Empty the text area NodeList nodeList = textArea.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { textArea.removeChild(nodeList.item(i)); } // Fill the text area with a new text node containing the given text try { textArea.appendChild(getW3CDocument().createTextNode(inputText)); } catch (DOMException e) { System.err.println("Problems inserting the new child node."); e.printStackTrace(); } } }); } /** * Clicks on a W3C node. If the HTML element has an attribute 'oncllick', * first try to execute the script and then click the element. If the node * is not an instance of HTMLInputElement, HTMLAnchorElement or HTMLElement, * then an exception will be thrown. * * @param node * @throws SimpleBrowserException */ public void click(Node node) throws SimpleBrowserException { // If the node is a instance of HTMLElement and contains an // 'onclick' attribute, then we must execute the script if ( node instanceof HTMLElement ){ final HTMLElement ele = (HTMLElement) node; display.syncExec(new Runnable() { public void run() { String onclick = ele.getAttribute("onclick"); if ( onclick != null && !onclick.equals("") ) { browser.execute(onclick); } } }); } // If the node is an instance of HTMLInputElement, then could be // a submit button (corresponding to types submit and image) then // we must submit the form only if it has the required attribute // 'action'. if (node instanceof HTMLInputElement) { HTMLInputElement button = (HTMLInputElement) node; if ( button.getType().equalsIgnoreCase("submit") || button.getType().equalsIgnoreCase("image") ){ String formAction = button.getForm().getAction(); if ( formAction != null && !formAction.equals("") ){ submitForm(button.getForm()); } } // If the node is an instance of HTMLAnchorElement we only // need to call 'go' method for the 'href' attribute. } else if (node instanceof HTMLAnchorElement) { HTMLAnchorElement link = (HTMLAnchorElement) node; if (link.getHref() != null && !link.getHref().equals("")) { go(link.getHref()); } // If the node is not an instance of HTMLElement class, then we // cannot click on it. } else if ( !(node instanceof HTMLElement) ) { throw new SimpleBrowserException( "Click only works with HTMLElements with onclick " + " attribute or links (HTMLAnchorElement) or buttons (HTMLButtonElement)"); } } private boolean waitLoad(long millis) { try { // Uses the latch, created by 'go' method to wait for // the finish of the page loading (it will occurs when // our 'progressListener' receives a event for its method // 'completed'), or for a given timeout in case that the // loading doesn't finish in a reasonable time. boolean timeout; timeout = !latch.await(millis,TimeUnit.MILLISECONDS); if (timeout) { // If the timeout expired, then we will stop // page loading. display.syncExec(new Runnable() { public void run() { browser.stop(); } }); // Waits for the loading is stopped latch.await(millis,TimeUnit.MILLISECONDS); } return timeout; } catch (InterruptedException e) { throw new Error(e); } } private List<Node> xPathNodes(String xpath, nsIDOMNode context) { // Obtain the Mozilla DOM HTML document HTMLDocumentImpl documentImpl = (HTMLDocumentImpl) getW3CDocument(); nsIDOMHTMLDocument document = documentImpl.getInstance(); // Creates a name space resolver for the document nsIDOMXPathNSResolver res = xpathEval.createNSResolver(document); List<Node> resultNodes = null; // Evaluates given XPath in a given context, using the resolver created // for the current document as an ordered iterator nsISupports obj = xpathEval.evaluate(xpath, context, res, nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null); // Obtain the interface corresponding to the XPath XPCOM results object nsIDOMXPathResult result = (nsIDOMXPathResult) obj.queryInterface( nsIDOMXPathResult.NS_IDOMXPATHRESULT_IID); try { // Extract result nodes for the XPath and add them // to a standard List. resultNodes = getNodes(result); } catch(org.mozilla.xpcom.XPCOMException e){ throw e; } return resultNodes; } private <T> List<T> getNodes(nsIDOMXPathResult result) { List<T> nodes = new ArrayList<T>(); nsIDOMNode node; while((node = result.iterateNext()) != null){ // Use the functionality provided by the mozdom4java // (in our case, patched) library to obtain the corresponding // W3C implementation of a node. nodes.add((T)NodeFactory.getNodeInstance(node)); } return nodes; } private void submitForm(final HTMLFormElement form) throws SimpleBrowserException { // Uses the latch to wait for response page loading when the form // is submitted. latch = new CountDownLatch(1); // Submits the form. display.syncExec(new Runnable() { public void run() { form.submit(); } }); // Waits for the server response, that is, until the response // page finish loading. boolean timeout = waitLoad(defaultTimeout); if (timeout) { throw new SimpleBrowserException("Timeout waiting page loading."); } } public static void main(String[] args) { String xulrunnerPath = null; if ( args.length > 0 ) { xulrunnerPath = args[0]; } // Instantiate our simple web browser final SimpleBrowserWithClick simpleBrowser = new SimpleBrowserWithClick(xulrunnerPath); try{ // Load a web page simpleBrowser.go("http://www.my400800.cn "); Thread.sleep(3000); // Get the W3C DOM anchor element containing the text 'Noticias' HTMLAnchorElement a = simpleBrowser.xpathNodes("//a[contains(text(),'Noticias')]", HTMLAnchorElement.class).get(0); // Click on the anchor previously obtained simpleBrowser.click(a); Thread.sleep(2000); // Get the input field to write search terms simpleBrowser.display.syncExec(new Runnable() { public void run(){ try{ Node node = simpleBrowser.xpathNodes("//input[@name='q']").get(0); simpleBrowser.enter(node, "nasdaq"); }catch (SimpleBrowserException sbe){ sbe.printStackTrace(); } } }); // Node node = simpleBrowser.xpathNodes("//input[@name='q']").get(0); // Enter the text 'nasdaq' in the input field Thread.sleep(2000); // Get the input button used to submit the form HTMLInputElement e = simpleBrowser.xpathNodes("//input[@value='Buscar en Noticias']", HTMLInputElement.class).get(0); // Click the input buuton and start the search for the term 'nasdaq' // in news section simpleBrowser.click(e); Thread.sleep(3000); // Load a different page with javascript examples simpleBrowser.go("http://www.codearchive.com/code/0300/0309-acces009.htm"); // Get a W3C anchor element containing an 'onlick' attribute a = simpleBrowser.xpathNodes("//a[contains(text(),'4')]", HTMLAnchorElement.class).get(0); // Click the anchor and then the javascript will be executed by // our browser simpleBrowser.click(a); Thread.sleep(3000); } catch (SimpleBrowserException e) { System.err.println("Problems calling go method."); e.printStackTrace(); } catch (InterruptedException e) { System.err.println("Problems calling sleep."); e.printStackTrace(); } Runtime.getRuntime().halt(0); } }
发表评论
-
Java和C#运行命令行并获取返回值 运行bat文件
2015-08-04 10:45 1340Java运行命令行的例子 import java.io.B ... -
C#和java调用phantomjs采集ajax加载生成的网页
2015-08-04 09:58 4328日前有采集需求,当我把所有的对应页面的链接都拿到 ... -
数据结构之BloomFilter
2014-12-04 10:12 1724BloomFilter是什么? Bloo ... -
在XULRunner中获得Cookie
2012-04-20 11:22 1187最近在看browser嵌入,找到XULRunner,http: ... -
XULRunner with Java: JavaXPCOM Tutorial 4
2012-04-13 17:04 13947. 用JavaXPCOM来自动浏览网页 在这一节里我 ... -
XULRunner with Java: JavaXPCOM Tutorial 3
2012-04-06 11:07 12206 加载页面的W3C DOM访问 6.1 mozdom4ja ... -
XULRunner with Java: JavaXPCOM Tutorial 1
2012-04-06 11:03 1764这篇教程更新于2008年夏 ... -
XULRunner with Java: JavaXPCOM Tutorial 2
2012-04-06 10:58 1858package es.ladyr.javaxpcom. ... -
SWT可交互式Browser控件
2012-04-06 10:55 3058在许多平台中,Browser控件皆被作为一个必需的控件给出,并 ... -
java应用程序中嵌入浏览器总结
2012-04-06 10:53 1902java应用程序中嵌套浏 ...
相关推荐
**XULRunner详解:下载、安装、配置与实例** XULRunner是一款开源的软件运行时环境,由Mozilla基金会开发,用于支持使用XUL(XML User Interface Language)编写的应用程序。XUL是一种基于XML的标记语言,它允许...
该项目包含有助于破解脚本。 如何为Mer配置开发环境 用于嵌入式设备的Gecko引擎开发非常耗资源。 如果要尝试,最好有一台运行Linux的强大计算机,并避免进行虚拟化。 首先,您需要安装Mer platform SDK并输入它。...
在Java中,通过JVM(Java Virtual Machine)和特定的库,可以将XULRunner内嵌到Swing应用中,实现一个功能完备的Web浏览器组件。 首先,理解XULRunner的工作原理至关重要。XULRunner提供了一个完整的运行时环境,...
2. **集成到Java项目**:将XULRunner库添加到Java项目的类路径中,以便在运行时调用。 3. **编写Java代码**:使用特定的API,如XPCOM(交叉平台组件对象模型),与XULRunner进行交互,创建并控制浏览器窗口。 4. **...
《XULRunner:Firefox浏览器内核的深度解析》 XULRunner,全称为“XML User Interface Library Runner”,是Mozilla基金会开发的一个开源软件框架,用于运行使用XUL(XML User Interface Language)界面描述语言...
NPAPI,作为早期浏览器扩展的接口标准,允许开发者创建能在多种浏览器上运行的插件,如Adobe Flash、Java等。然而,随着技术的发展,由于安全性和性能问题,NPAPI逐渐被取代,但仍有部分应用依赖于它,比如VLC媒体...
XULRunner 10.0.4 ESR版本提供了稳定性和安全性,而DJNativeSwing则增强了Java应用在用户界面方面的表现。对于需要在Windows环境中开发或运行使用XUL和Swing技术的Java应用的开发者来说,这是一个必不可少的资源。
5. **问题解决**:当遇到使用Uptana(可能是特定工具或软件的错误引用)时的问题,可能是因为缺少了像XULRunner这样的依赖。下载并安装正确的XULRunner版本后,可能需要重新配置或重启软件以解决依赖问题。 6. **...
5. **GUI构建**:XULRunner不仅仅用于浏览器扩展,也可以构建独立的GUI应用程序。通过XUL,开发者可以创建具有丰富交互特性的界面,同时利用Mozilla的底层技术,如CSS样式和JavaScript脚本。 **开发流程** 1. **...
XULRunner是一个开源的软件运行环境,由Mozilla基金会开发,主要用于支持使用XUL(XML User Interface Language)构建的应用程序。XUL是一种标记语言,类似于HTML,但设计用于创建跨平台的用户界面,尤其适用于桌面...
org.eclipse.swt.SWTError: No more handles [MOZILLA_FIVE_HOME=''] (java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: no swt-mozilla-gtk-4335 in java.library.path no swt-mozilla-gtk ...
这个压缩包包含了GeckoFx的33.09版本的源码以及对应的XULrunner运行时环境。 1. **GeckoFx简介**: - GeckoFx是.NET Framework的一个组件,它实现了对Mozilla的Gecko渲染引擎的封装,使得.NET开发者可以方便地利用...
5. **SDK内容**:Xulrunner SDK通常包含以下部分: - **头文件**:供C/C++编程使用的接口定义。 - **库文件**:链接到应用程序,实现XUL功能。 - **文档**:教程、API参考和其他开发者指南。 - **示例代码**:...
《XULRunner-win64-1.9.2.25:Java Swing的视图化展示利器》 在IT行业中,XULRunner是一款至关重要的工具,尤其对于开发者来说,它提供了一个强大的平台来运行基于XUL(XML User Interface Language)的应用程序。...
xulrunner-1.9.2-gnome-support_1.9.2.17+build3+nobinonly-0ubuntu0.8.04.1_i386
1. **Java编程**:Java是一种广泛使用的面向对象的编程语言,具有跨平台的特性,允许开发者编写一次代码,到处运行。在这个项目中,Java用于创建浏览器的后端逻辑和用户界面,展示其强大的应用程序开发能力。 2. **...
5. **安全性**:随着 29.0 版本的发布,XULRunner 引入了更多安全特性,如防止跨站脚本攻击(XSS)、点击劫持防护等,保障了用户数据的安全。 **Geckofx** Geckofx 是一个 .NET Framework 的封装库,它将 Mozilla ...
XULRunner是一个开源的软件运行环境,由Mozilla基金会开发,主要用于支持使用XUL(XML User Interface Language)构建的跨平台应用程序。XULRunner提供了一套完整的库和框架,使得开发者可以构建桌面应用,而无需...
**XULRunner详解** XULRunner是Mozilla基金会开发的一个开源软件平台,主要用于支持基于XUL(XML User Interface Language)的应用程序运行。XUL是一种标记语言,设计用于构建跨平台的图形用户界面,它允许开发者...
标题提到的"GeckoFx-5.0-0.1 and xulrunner-5.0"是两个在IT行业中与Web浏览器渲染引擎相关的关键组件,它们主要用于开发基于.NET Framework的应用程序,特别是那些需要自定义Web浏览功能或者嵌入式浏览器控件的情况...