表示しているページのサイズ(画面キャプチャのサイズ、もしくは類似のサイズ)をselenumの何かしらのクラスで取得する方法

526 views
Skip to first unread message

松浦佑

unread,
Oct 8, 2015, 3:55:42 AM10/8/15
to 日本Seleniumユーザーコミュニティ
seleniumで、巨大なページの画面キャプチャを取得するとOutOfMemoryが発生してしまいます。 
短時間で大量のJavaヒープを消費するためOutOfMemoryErrorになっているようなので、 
巨大なページであればキャプチャを取らないように条件分岐させたいと思っています。 

 そこで表示したページのサイズ(画面キャプチャのサイズ、もしくは類似のサイズ)をselenumの何かしらのクラスで取得する方法 
をご存知な方いらっしゃいましたらお教え頂きたいです。 

▼具体的な問題▼ 
具体的には、 
http://backyard.imjp.co.jp/static/feed/atom.xml 
を以下のコードで画面キャプチャを採取しようとするとOutOfMemoryが発生します。 


▼キャプチャ採取のコード▼ 
                //取得したURLのスクリーンショット作成
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile, new File(scrFileName));

▼発生している例外▼ 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.lang.StringCoding.decode(StringCoding.java:215)
    at java.lang.String.<init>(String.java:451)
    at java.lang.String.<init>(String.java:503)
    at org.openqa.selenium.remote.http.HttpMessage.getContentString(HttpMessage.java:110)
    at org.openqa.selenium.remote.http.HttpResponse.getContentString(HttpResponse.java:1)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.decode(JsonHttpResponseCodec.java:76)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:137)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:170)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:393)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:618)
    at org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:354)
    at sample.SandBox.main(SandBox.java:168)

▼試してみたけどダメだったこと▼ 
  1.  JVMの起動パラメータで、-Xms1024m、-Xmx1024mにした。
  2.  ファイルコピーをせずに一時ディレクトリでのキャプチャ採取のみにした。(FileUtils.copyFile(scrFile, new File(scrFileName)); をコメントアウト)

▼やりたいこと。質問▼ 
短時間で大量のJavaヒープを消費するためOutOfMemoryErrorになっているようなので、 
巨大なページの画面キャプチャ採取をあきらめ、 
巨大なページであればキャプチャを取らないように条件分岐させたいと思っています。 

 そこで表示したページのサイズ(画面キャプチャのサイズ、もしくは類似のサイズ)をselenumの何かしらのクラスで取得する方法 
をご存知の方はお教え頂きたいです。 

戸田広

unread,
Oct 8, 2015, 1:48:24 PM10/8/15
to 松浦佑, 日本Seleniumユーザーコミュニティ
こんばんは、戸田です。

WebElement.getSize() をbody要素について実行すれば
ページ全体の幅・高さのオブジェクトを取得できます。

    Dimension rootDomSize = driver.findElement(By.tagName("body")).getSize();


以上です。




2015年10月8日 16:55 松浦佑 <tasu...@imjp.co.jp>:

--
このメールは Google グループのグループ「日本Seleniumユーザーコミュニティ」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには seleniumjp+...@googlegroups.com にメールを送信してください。
このグループに投稿するには selen...@googlegroups.com にメールを送信してください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

IMJ)松浦佑

unread,
Oct 8, 2015, 9:27:27 PM10/8/15
to 戸田広, 日本Seleniumユーザーコミュニティ
戸田さん

はじめまして。松浦と申します。

早速のご返信ありがとうございます。
試してみます!

取り急ぎお礼まで。




2015年10月9日 2:48 戸田広 <hiros...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages