サイトを解析する必要があること。 Webdriver APIの基本

住宅 、製品情報、空室、 デートを 検索し 、会社の製品を競合他社と比較し、オンラインでレビューを調査します。







多くの有用な情報がインターネット上で公開されており、データを抽出する機能は生活と仕事に役立ちます。 webdriver APIを使用して情報を受信する方法を学びます。 出版物では、2つの例を示します。そのコードはgithubで入手できます。 記事の最後にあるスクリーンキャストは、プログラムがブラウザーを制御する方法に関するものです。



Webドライバーを使用するプログラムまたはスクリプトは、ブラウザーを制御します。テキストを入力し、リンクをたどり、要素をクリックし、ページとその要素からデータを抽出し、サイトのスクリーンショットを撮ります。



Webdriverが機能するには、2つのコンポーネントが必要です。ブラウザ/プロトコルサーバーと、プログラミング言語のライブラリとしてのクライアント部分です。



さまざまなプログラミング言語および仮想マシンからwebdriver APIを使用できます。 公式の webdriver クライアントは、C#、Ruby、Python、Javascript(Node)、およびPerl、Perl 6、PHP、Haskell、Objective-C、Javascriptのコミュニティクライアント用です、R、Dart、Tcl。



Webdriverは現在、まだ作業中のW3C標準です。 Webdriver APIは、Selenium-RC APIの進化の結果として、テスト目的でseleniumプロジェクトに最初に登場しました。



サーバーは、プロトコル言語を理解する別のプロセスを使用します。 このプロセスはブラウザを制御します。 以下のドライバーが利用可能です。





このリストから2つのドライバーが際立っています:





技術の本質...




さらなる研究のための最小理論。 クライアントでのアクションのシーケンス



したがって、 phantomjsの選択。 これは、webdriverプロトコルによって制御される本格的なブラウザです。 多くのプロセスを同時に実行できます。グラフィックサブシステムは必要ありません。javascriptは完全に内部で実行されます(htmlunitの制限とは対照的に)。 スクリプトをjavascriptで記述し、起動時にパラメーターとして渡すと、phantomJSはWebドライバープロトコルなしでスクリプトを実行でき、別のブラウザーでのデバッグも可能です。







以下は主にjava / groovy API用です。 他の言語のクライアントでは、関数とパラメーターのリストは似ているはずです。



Webドライバーを備えたサーバーを取得します。



String phantomJsPath = PhantomJsDowloader.getPhantomJsPath()
      
      





Mavenリポジトリーからphantomjsをロードし、このブラウザーへのパスをアンパックして返します。 使用するには、プロジェクトにmavenのライブラリcom.github.igor-suhorukov:phantomjs-runner:1.1から接続する必要があります。



以前にブラウザーのWebドライバーをローカルファイルシステムにインストールした場合は、この手順をスキップできます。



クライアントを作成し、サーバーに接続します



 WebDriver driver = new PhantomJSDriver(settings)
      
      





Webdriverプロトコルを介した対話用のポートを構成し、phantomjsプロセスを開始して接続します。



ブラウザで目的のページを開きます



 driver.get(url)
      
      





指定されたアドレスのページをブラウザで開きます。



興味のある情報を取得します



 WebElement leftmenu = driver.findElement(By.id("leftmenu")) List<WebElement> linkList = leftmenu.findElements(By.tagName("a"))
      
      





ドライバードライバーインスタンスとそこから取得した要素には、findElement、findElementsという2つの便利なメソッドがあります。 最初の要素は要素を返すか、要素が見つからない場合はNoSuchElementExceptionをスローします。 2番目-要素のコレクションを返します。



要素は、次のorg.openqa.selenium.Byクエリによって選択できます。





id、tagName、xpathを積極的に使用します。 xpathに慣れていない人のために- や記事を見てから、仕様書を読むことをお勧めします。



ページ上の要素とページでアクションを実行する



要素を使用して次のことができます。





 driver.getScreenshotAs(type)
      
      





ブラウザウィンドウのスナップショットを取得します。 標準のスナップショット機能への便利な追加としてaShotライブラリをお勧めします。ウィンドウ内の特定の要素のみのスナップショットを取得し、要素を画像として比較できるようにします。



スクリーンショットは次のように取得できます。





ブラウザ接続を閉じます



 driver.quit()
      
      





プロトコル接続を閉じ、この場合はphantomjsプロセスを停止します。



例1:ソーシャルネットワークプロファイルに関するグルーヴィーなスクリプトを歩く



コマンドを実行します:



 java -jar groovy-grape-aether-2.4.5.1.jar crawler.groovy http://??.com/catalog.php
      
      





実行に必要なファイルへのリンク:



コンソールのスクリプトは、ソーシャルネットワークからの情報に基づいて作成したhtmlファイルへのパスを出力します。 ページには、ユーザー名、ソーシャルネットワークへの最後のアクセスの瞬間、およびユーザーページ全体のスクリーンショットが表示されます。



groovy-grape-aether-2.4.5.1を使用してグルーブスクリプトを実行する理由
Groovy -grape-aether-2.4.5.1.jarは最近、 「スクリプト内のストリートマジック、またはGroovy、Ivy、Mavenをつなぐものは?」という記事で溝の組み立てについて説明しました。 groovy-all-2.4.5.jarとの主な違いは、グレープがaetherライブラリを使用するivyと比較してより正確な方法でリポジトリを操作できることと、アセンブリ内のリポジトリのアクセスクラスの可用性です。



crawler.groovy

 package com.github.igorsuhorukov.phantomjs @Grab(group='commons-io', module='commons-io', version='2.2') import org.apache.commons.io.IOUtils @Grab(group='com.github.detro', module='phantomjsdriver', version='1.2.0') import org.openqa.selenium.* import org.openqa.selenium.phantomjs.PhantomJSDriver import org.openqa.selenium.phantomjs.PhantomJSDriverService import org.openqa.selenium.remote.DesiredCapabilities @Grab(group='com.github.igor-suhorukov', module='phantomjs-runner', version='1.1') import com.github.igorsuhorukov.phantomjs.PhantomJsDowloader public class Crawler { public static final java.lang.String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" public static void run(String baseUrl) { def phantomJsPath = PhantomJsDowloader.getPhantomJsPath() def DesiredCapabilities settings = new DesiredCapabilities() settings.setJavascriptEnabled(true) settings.setCapability("takesScreenshot", true) settings.setCapability("userAgent", com.github.igorsuhorukov.phantomjs.Crawler.USER_AGENT) settings.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomJsPath) def WebDriver driver = new PhantomJSDriver(settings) def randomUrl = null def lastVisited=null def name=null boolean pass=true while (pass){ try { randomUrl = getUrl(driver, baseUrl) driver.get(randomUrl) def titleElement = driver.findElement(By.id("title")) lastVisited = titleElement.findElement(By.id("profile_time_lv")).getText() name = titleElement.findElement(By.tagName("a")).getText() pass=false } catch (NoSuchElementException e) { System.out.println(e.getMessage()+". Try again.") } } String screenshotAs = driver.getScreenshotAs(OutputType.BASE64) File resultFile = File.createTempFile("phantomjs", ".html") OutputStreamWriter streamWriter = new OutputStreamWriter(new FileOutputStream(resultFile), "UTF-8") IOUtils.write("""<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body> <p>${name}</p><p>${lastVisited}</p> <img alt="Embedded Image" src="data:image/png;base64,${screenshotAs}"></body> </html>""", streamWriter) IOUtils.closeQuietly(streamWriter) println "html ${resultFile} created" driver.quit(); } static String getUrl(WebDriver driver, String baseUrl) { driver.get(baseUrl) def elements = driver.findElements(By.xpath("//div[@id='content']//a")) def element = elements.get((int) Math.ceil(Math.random() * elements.size())) String randomUrl = element.getAttribute("href") randomUrl.contains("catalog") ? getUrl(driver, randomUrl) : randomUrl } } Crawler.run(this.args.getAt(0))
      
      





よく知っている溝は、 Gebがより適切なソリューションであることに気付くでしょう。 しかし、DSLの背後にあるWebdriverのすべての作業を隠すため、Gebはトレーニング目的には適していません。 審美的な理由から、私はあなたに同意します!



例2:javaのjava-sourceプログラムからプロジェクトデータを取得する



ここに例があります 。 実行するには、ストリームとtry-with-resourcesが使用されるため、java8が必要です。



 git clone https://github.com/igor-suhorukov/java-webdriver-example.git mvn clean package -Dexec.args="http://java-source.net"
      
      





この例では、xpathとaxisを使用してページから情報を抽出します。 例として、 Projectクラスのフラグメント。



 WebElement main = driver.findElement(By.id("main")); name = main.findElement(By.tagName("h3")).getText(); description = main.findElement(By.xpath("//h3/following-sibling::table/tbody/tr/td[1]")).getText(); link = main.findElement(By.xpath("//td[text()='HomePage']/following-sibling::*")).getText(); license = main.findElement(By.xpath("//td[text()='License']/following-sibling::*")).getText();
      
      





サイトから取得したデータの一部。 ファイルprojects.xml-プログラムの結果
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <projects source="http://java-source.net"> <category> <category>Open Source Ajax Frameworks</category> <project> <name>DWR</name> <description>DWR is a Java open source library which allows you to write Ajax web sites. It allows code in a browser to use Java functions running on a web server just as if it was in the browser. DWR works by dynamically generating Javascript based on Java classes. The code does some Ajax magic to make it feel like the execution is happening on the browser, but in reality the server is executing the code and DWR is marshalling the data back and forwards.</description> <license>Apache Software License</license> <link>http://getahead.org/dwr</link> </project> <project> <name>Google Web Toolkit</name> <description>Google Web Toolkit (GWT) is an open source Java software development framework that makes writing AJAX applications like Google Maps and Gmail easy for developers who don't speak browser quirks as a second language. Writing dynamic web applications today is a tedious and error-prone process; you spend 90% of your time working around subtle incompatibilities between web browsers and platforms, and JavaScript's lack of modularity makes sharing, testing, and reusing AJAX components difficult and fragile. GWT lets you avoid many of these headaches while offering your users the same dynamic, standards-compliant experience. You write your front end in the Java programming language, and the GWT compiler converts your Java classes to browser-compliant JavaScript and HTML.</description> <license>Apache Software License</license> <link>http://code.google.com/webtoolkit/</link> </project> <project> <name>Echo2</name> <description>Echo2 is the next-generation of the Echo Web Framework, a platform for developing web-based applications that approach the capabilities of rich clients. The 2.0 version holds true to the core concepts of Echo while providing dramatic performance, capability, and user-experience enhancements made possible by its new Ajax-based rendering engine.</description> <license>Mozilla Public License</license> <link>http://www.nextapp.com/platform/echo2/echo/</link> </project> <project> <name>ICEfaces</name> <description>ICEfaces is an integrated Ajax application framework that enables Java EE application developers to easily create and deploy thin-client rich Internet applications (RIA) in pure Java. ICEfaces leverages the entire standards-based Java EE ecosystem of tools and execution environments. Rich enterprise application features are developed in pure Java, and in a pure thin-client model. There are no Applets or proprietary browser plug-ins required. ICEfaces applications are JavaServer Faces (JSF) applications, so Java EE application development skills apply directly and Java developers are isolated from doing any JavaScript related development.</description> <license>Mozilla Public License</license> <link>http://www.icefaces.org</link> </project> <project> <name>SweetDEV RIA</name> <description>SweetDEV RIA is a complete set of world-class Ajax tags in Java/J2EE. It helps you to design Rich GUI in a thin client. SweetDEV RIA provides you Out-Of-The-Box Ajax tags. Continue to develop your application with frameworks like Struts or JSF. SweetDEV RIA tags can be plugged into your JSP pages.</description> <license>Apache Software License</license> <link>http://sweetdev-ria.ideotechnologies.com</link> </project> <project> <name>ItsNat, Natural AJAX</name> <description>ItsNat is an open source (dual licensed GNU Affero General Public License v3/commercial license for closed source projects) Java AJAX Component based Web Framework. It offers a natural approach to the modern Web 2.0 development. ItsNat simulates a Universal W3C Java Browser in the server. The server mimics the behavior of a web browser, containing a W3C DOM Level 2 node tree and receiving W3C DOM Events using AJAX. Every DOM server change is automatically sent to the client and updated the client DOM accordingly. Consequences: pure (X)HTML templates and pure Java W3C DOM for the view logic. No JSP, no custom tags, no XML meta-programming, no expression languages, no black boxed components where the developer has absolute control of the view. ItsNat provides an, optional, event based (AJAX) Component System, inspired in Swing and reusing Swing as far as possible such as data and selection models, where every DOM element or element group can be easily a component.</description> <license>GNU General Public License (GPL)</license> <link>http://www.itsnat.org</link> </project> <project> <name>ThinWire</name> <description>ThinWire is an development framework that allows you to easily build applications for the web that have responsive, expressive and interactive user interfaces without the complexity of the alternatives. While virtually any web application can be built with ThinWire, when it comes to enterprise applications, the framework excels with its highly interactive and rich user interface components.</description> <license>GNU Library or Lesser General Public License (LGPL)</license> <link>http://www.thinwire.com/</link> </project> </category> <category> <category>Open Source Aspect-Oriented Frameworks</category> <project> <name>AspectJ</name> <description>AspectJ is a seamless aspect-oriented extension to the Java programming language, Java platform compatible and easy to learn and use. AspectJ enables the clean modularization of crosscutting concerns such as: error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, multi-object protocols.</description> <license>Mozilla Public License</license> <link>http://eclipse.org/aspectj/</link> </project> <project> <name>AspectWerkz</name> <description>AspectWerkz is a dynamic, lightweight and high-performant AOP framework for Java. AspectWerkz offers both power and simplicity and will help you to easily integrate AOP in both new and existing projects. AspectWerkz utilizes runtime bytecode modification to weave your classes at runtime. It hooks in and weaves classes loaded by any class loader except the bootstrap class loader. It has a rich and highly orthogonal join point model. Aspects, advices and introductions are written in plain Java and your target classes can be regular POJOs. You have the possibility to add, remove and re-structure advice as well as swapping the implementation of your introductions at runtime. Your aspects can be defined using either an XML definition file or using runtime attributes.</description> <license>GNU Library or Lesser General Public License (LGPL)</license> <link>http://aspectwerkz.codehaus.org/</link> </project> <project> <name>Nanning</name> <description>Nanning Aspects is a simple yet scaleable aspect-oriented framework for Java.</description> <license>BSD License</license> <link>http://nanning.codehaus.org/</link> </project> <project> <name>JBossAOP</name> <description>JBoss-AOP allows you to apply interceptor technology and patterns to plain Java classes and Dynamic Proxies. It includes: * Java Class Interception. Field, constructor, and method interception, public, private, protected, and package protected, static and class members. * Fully compositional pointcuts caller side for methods and constructors, control flow, annotations. * Aspect classes Advices can be incapsulated in scoped Java classes * Hot-Deploy. Interceptors can be deployed, undeployed, and redeployed at runtime for both dynamic proxies and classes.(working) * Introductions. The ability to add any arbitrary interface to a Java class. Either an interceptor or a 'mixin' class can service method calls for the attached interfaces. * Dynamic Proxies. The ability to define a dynamic proxy and an interceptor chain for it. Proxies can either be created from an existing class, or from a set of interfaces ala java.lang.reflect.Proxy. * Metadata and Attribute Programming. The ability to define and attach metadata configuration to your classes or dynamic proxies. Interceptors can be triggered when metadata is added to a class. We also have Metadata Chains, the ability to define defaults at the cluster and application level, as well as the ability to override configuration at runtime for a specific method call. * Dynamic AOP. All aspected objects can be typecasted to an AOP api. You can do things like add/remove new interceptors to a specific instance or add/remove instance configuration/metadata at runtime.</description> <license>GNU Library or Lesser General Public License (LGPL)</license> <link>http://www.jboss.org/products/aop</link> </project> <project> <name>dynaop</name> <description>dynaop, a proxy-based Aspect-Oriented Programming (AOP) framework, enhances Object-Oriented (OO) design in the following areas: code reuse decomposition dependency reduction</description> <license>Apache Software License</license> <link>https://dynaop.dev.java.net/</link> </project> <project> <name>CAESAR</name> <description>CAESAR is a new aspect-oriented programming language compatible to Java, that is, all Java programs will run with CAESAR.</description> <license>GNU General Public License (GPL)</license> <link>http://caesarj.org/</link> </project> <project> <name>EAOP</name> <description>Event-based Aspect-Oriented Programming (EAOP) for Java. EAOP 1.0 realizes the EAOP model through the following characteristics: * Expressive crosscuts: execution points can be represented by events and crosscuts can be expressed which denote arbitrary relations between events. * Explicit aspect composition: Aspects may be combined using a (extensible) set of aspect composition operators. * Aspects of aspects: Aspects may be applied to other aspects. * Dynamic aspect management: Aspects may be instantiated, composed and destroyed at runtime.</description> <license>GNU General Public License (GPL)</license> <link>http://www.emn.fr/x-info/eaop/tool.html</link> </project> <project> <name>JAC</name> <description>JAC (Java Aspect Components) is a project consisting in developing an aspect-oriented middleware layer.</description> <license>GNU Library or Lesser General Public License (LGPL)</license> <link>http://jac.objectweb.org/</link> </project> <project> <name>Colt</name> <description>Open Source Libraries for High Performance Scientific and Technical Computing in Java</description> <license>The Artistic License</license> <link>http://hoschek.home.cern.ch/hoschek/colt/</link> </project> <project> <name>DynamicAspects</name> <description>DynamicAspects enables you to do aspect-oriented programming in pure Java. Using the \"instrumentation\" and \"agent\" features introduced with Sun JDK 1.5, aspects can be installed and deinstalled during runtime!</description> <license>BSD License</license> <link>http://dynamicaspects.sourceforge.net/</link> </project> <project> <name>PROSE</name> <description>The PROSE system (PROSE stands for PROgrammable extenSions of sErvices) is a dynamic weaving tool (allows inserting and withdrawing aspects to and from running applications) PROSE aspects are regular JAVA objects that can be sent to and be received from computers on the network. Signatures can be used to guarantee their integrity. Once an aspect has been inserted in a JVM, any occurrence of the events of interest results in the execution of the corresponding aspect advice. If an aspect is withdrawn from the JVM, the aspect code is discarded and the corresponding interception(s) will no longer take place.</description> <license>Mozilla Public License</license> <link>http://prose.ethz.ch/Wiki.jsp?page=AboutProse</link> </project> <project> <name>Azuki Framework</name> <description>The Azuki Framework is a java application framework, designed to reduce the development, deployment and maintenance costs of software systems. The Azuki Framework provides also an unique combination of powerful design patterns (decorator, injection, intercepter, command, proxy...). It provides a rapid application assembly from known components in order to build large systems. The software conception is split into two main stages : * Creation of independent components (technical & business service). * Definition of component dependencies (weaving)</description> <license>GNU Library or Lesser General Public License (LGPL)</license> <link>http://www.azuki-framework.org/</link> </project> <project> <name>CALI</name> <description>CALI is a framework to prototype and compose Aspect-Oriented Programming Languages on top of Java. It is based on an abstract aspect language that its extensible to implement new AOPL. As proof of approach and methodology, the following language have been implemented: -AspectJ (Dynamic part of AspectJ, where intertype declartion can be implemented using regular AspectJ); -EAOPJ : An implementation of Event-Based AOP for Java; -COOL: A DSAL for coordination; -Decorator DSAL. You can use CALI to implement your new AOPL and compose it with existing implementation or using existing implementation to write your applications with aspects form different AOPL.</description> <license>Other</license> <link>http://www.emn.fr/x-info/cali/</link> </project> </category> </projects>
      
      







これは、ChromeDriverドライバー(org.seleniumhq.selenium:selenium-chrome-driver:2.48.2)で同じ例を使用する方法です。 PhantomJSとは異なり、この場合、プログラムの起動中に何が起こるかを確認できます。リンクをたどってページをレンダリングします。







おわりに



Webdriver APIは、さまざまなプログラミング言語から使用できます。 ブラウザを制御し、ページから情報を抽出するスクリプトまたはプログラムを作成するのは非常に簡単です。Idタグ、CSSセレクター、またはXPath式によってページからデータを受信すると便利です。 ページとその上の個々の要素の写真を撮ることができます。 例とドキュメントに基づいて、サイトを操作するためのほぼあらゆる複雑なスクリプトを開発できます。 開発とデバッグには、通常のブラウザーとWebドライバーを使用することをお勧めします。 完全自動操作には、PhantomJSが最適です。



ウェブからオープンで有用な情報を抽出してください。



All Articles