Can't run PhantomJS with Flask on Ubuntu VPS
I am making a test box that requires tests to run through a web browser. I am using Ubuntu VPS 14 with LAMP stack, mod_wsgi, selenium 2.44 and PhantomJS 1.9 installed. I test the simplest code first:
from flask import Flask, request
from selenium import webdriver
app = Flask(__name__)
app.debug = True
@app.route("/test")
def test():
url = "http://www.google.com"
driver = webdriver.PhantomJS('./phantomjs')
driver.get(url)
result = driver.page_source
driver.close()
return result
if __name__ == "__main__":
app.run()
The code runs very smoothly on my local Ubuntu, it prints out google page when I connect to: 127.0.0.1:5000/test. On my Ubuntu VPS I already have my flask installed and working. Now I am using the same code as the index file (assuming all configs are in order and "hello world" is working). I have 500 Internal Server Error while connecting to http://xxx.xxx.xxx.xxx/test
Apache log sends the following error:
... service_args = service_args, log_path = service_log_path File "/usr/local/lib/python2.7/ddist-packages/selenium/webdriver/phantomjs/service.py" line 53 in init self._log = open (log_path , 'w') in> ignored Exception AttributeError: "Object" has no attribute '_log' "in> ignored
I changed the log_path for phatomJS but still have the same problem. However, if I open the python console, execute the string lines like this:
from selenium import webdriver
br = webdriver.PhantomJS('./phantomjs')
....
I have no errors. It took me a whole day to fix the problem, but I couldn't fix it. Any ideas how to solve this problem?
source to share
Please figure out the problem, the current phantomjs and init .py do not have sufficient permission to control the ghostdriver service.py. Here's the fix:
- Add new user: "add username" then set "add username sudo"
- Log in as a new user, make sure every command you run with starts with "sudo"
- In your flash application where init .py resides, create "ghostdriver.log" and "phantomjs" which is an executable file.
- Chmod both of them: 777: init .py, ghostdriver.log and phantomjs
-
Set a custom config in init .py for phantomjs:
br = webdriver.PhantomJS (service_log_path = './ghostdriver.log', executable_path = './phantomjs')
Which now, your selenium + flask + phantomjs is now working correctly.
source to share