読者です 読者をやめる 読者になる 読者になる

ボールを蹴りたいシステムエンジニア

ボール蹴りが大好きなシステムエンジニア、ボールを蹴る時間確保の為に時間がある時には勉強する。

PhantomJS+Seleniumで「undefined is not an object ~」エラー発生時の対処方法

PhantomJS+Seleniumでテスト自動化をしようした所、以下のsendKeysでエラー発生。

            PhantomJSDriver driver = new PhantomJSDriver(capabilities);

            ~省略~

            WebElement userInput = driver.findElement(By.name("mail"));
            userInput.sendKeys(user);
            WebElement sublit = driver.findElement(By.name("submit"));
            sublit.click();

「undefined is not an object ~」ってエラー。

org.openqa.selenium.WebDriverException: {"errorMessage":"undefined is not an object (evaluating 'B(A(a)).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"27","Content-Type":"application/json; charset=utf-8","Host":"localhost:5084","User-Agent":"Apache-HttpClient/4.5.2 (Java/1.8.0_45)"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\":wdc:1478407789862\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/6e59cb90-a3dc-11e6-bd6c-c520fa0cf11a/element/:wdc:1478407789862/click"}}
Command duration or timeout: 192 milliseconds
Build info: version: '3.0.0-beta2', revision: '2aa21c1', time: '2016-08-02 15:03:28 -0700'

Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, handlesAlerts=false, databaseEnabled=false, phantomjs.page.settings.userName=username, version=2.1.1, platform=XP, browserConnectionEnabled=false, proxy={proxyType=direct}, nativeEvents=true, acceptSslCerts=false, phantomjs.page.settings.resourceTimeout=10000, driverVersion=1.2.0, phantomjs.page.settings.userAgent=test, locationContextEnabled=false, webStorageEnabled=false, browserName=phantomjs, takesScreenshot=true, driverName=ghostdriver, javascriptEnabled=true, phantomjs.page.settings.password=password, cssSelectorsEnabled=true}]
Session ID: 6e59cb90-a3dc-11e6-bd6c-c520fa0cf11a
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:683)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:319)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
    at test.BlogmuraTest.checkSite(BlogmuraTest.java:34)
    at test.BlogmuraTest.main(BlogmuraTest.java:15)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '3.0.0-beta2', revision: '2aa21c1', time: '2016-08-02 15:03:28 -0700'

Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:138)
    ... 5 more
Caused by: org.openqa.selenium.WebDriverException: {"errorMessage":"undefined is not an object (evaluating 'B(A(a)).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"27","Content-Type":"application/json; charset=utf-8","Host":"localhost:5084","User-Agent":"Apache-HttpClient/4.5.2 (Java/1.8.0_45)"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\":wdc:1478407789862\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/6e59cb90-a3dc-11e6-bd6c-c520fa0cf11a/element/:wdc:1478407789862/click"}}
Build info: version: '3.0.0-beta2', revision: '2aa21c1', time: '2016-08-02 15:03:28 -0700'

調査した所、ユーザーエージェントの設定が不正な為?
以下のように修正したらエラー解決した。

before

            capabilities.setCapability("phantomjs.page.settings.userAgent", "Test User Agent");

after

            capabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0");

PhantomJsでは設定可能なUserAgentが決まってるとか?
もしかしたらドキュメントにその辺書かれてるかも。

解決に8時間くらい掛かってしまいました。

以下のページが解決の決め手でした、感謝。

java - PhantomJSDriver found Element but is still undefined - Stack Overflow