Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Last active April 14, 2018 19:35
Show Gist options
  • Save neuro-sys/41e368839a9b20dafb34 to your computer and use it in GitHub Desktop.
Save neuro-sys/41e368839a9b20dafb34 to your computer and use it in GitHub Desktop.
How to Fix PhantomJS to work with GhostDriver

Copy paste the line below to your terminal:

https://gist.githubusercontent.com/neuro-sys/3bf00b6cf28a93e07e44/raw/e8976da93a55e5ef7c5e8dc99c00772fffb06f6c/replacestring.c
gcc replacestring.c -oreplacestring
./replacestring "$(strings /usr/bin/phantomjs | grep "platform: ghostdriver")" " " < /usr/bin/phantomjs > phantomjs && chmod a+x phantomjs
sudo cp phantomjs /usr/bin/phantomjs

Alright, there's a bug in a recent version of PhantomJS which renders it unable to run with GhostDriver. The error is like this:

[neuro@localhost]$ phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
[INFO  - 2015-06-09T16:09:46.432Z] GhostDriver - Main - running on port 8080
[INFO  - 2015-06-09T16:09:46.433Z] GhostDriver - Main - registering to Selenium HUB 'http://127.0.0.1:4444' version:  using '127.0.0.1:8080' with org.openqa.grid.selenium.proxy.DefaultRemoteProxy as remote proxy.
[ERROR - 2015-06-09T16:09:46.437Z] GhostDriver - main.fail - {"line":96,"sourceURL":"","stack":"\tat \n\tat global code (:/ghostdriver/main.js:78:37)"}

  :262 in error
[neuro@localhost]$

There're several bug reports in the github bug tracker of the said project, and in one of them some nice gentleman points at how to fix the problem. (https://github.com/detro/ghostdriver/issues/394#issuecomment-75531714)

However this requires building the upstream yourself, which in my case takes forever to do so due to a large dependency chain (I've seen QT among them, god knows why).

Here's a quick and dirty way to fix the issue until your distribution updates the package.

  • Fire up your favorite hex editor and edit/view the phantomjs executable:
$ dhex /usr/bin/phantomjs
  • For ghostdriver is written in javascript and embedded in the phantomjs distribution as plain-text, look for the line that's causing the bug:

  • Remove the line by filling up it with 0x20 (space) values. The line that you need to find and fill with ascii space characters is this: platform: ghostdriver.system.os.name + '-' + ghostdriver.system.os.version + '-' + ghostdriver.system.os.architecture,

  • Run and profit.

[neuro@localhost ]$ phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
[INFO  - 2015-06-09T16:30:16.636Z] GhostDriver - Main - running on port 8080
[INFO  - 2015-06-09T16:30:16.636Z] GhostDriver - Main - registering to Selenium HUB 'http://127.0.0.1:4444' version:  using '127.0.0.1:8080' with org.openqa.grid.selenium.proxy.DefaultRemoteProxy as remote proxy.
[INFO  - 2015-06-09T16:30:16.689Z] HUB Register - register - Registered with grid hub: http://127.0.0.1:4444/ (ok)

Alternative to HEX editor

wget https://gist.githubusercontent.com/neuro-sys/3bf00b6cf28a93e07e44/raw/e8976da93a55e5ef7c5e8dc99c00772fffb06f6c/replacestring.c
gcc replacestring.c -oreplacestring
./replacestring "$(strings /usr/bin/phantomjs | grep "platform: ghostdriver")" " " < /usr/bin/phantomjs > phantomjs && chmod a+x phantomjs
sudo cp phantomjs /usr/bin/phantomjs
@JeremyMarshall
Copy link

Nice but you need to do sudo dhex /usr/local/bin/phantomjs if you have installed with brew on a mac and you can also get dhex from brew

@toadzhou
Copy link

toadzhou commented Nov 16, 2016

# ./f_phantomjs
Segmentation fault
-_-!

@neuro-sys
Copy link
Author

@toadzhou

^_^'

I fixed the copy-paste snippet (the gist was wrong revision!), but haven't tested other than seeing no segfaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment