title: GhostDriver with PhantomJS and Selenium (Python) - "Connection Refused"
body: <p><strong>The Goal:</strong></p>
  
  <p>I am trying to perform some scraping in Python using a headless browser: Selenium with PhantomJs and GhostDriver.</p>
  
  <p>I am using Python 2.7 on a Mac running Mavericks. I work within emacs (although it also didn't work from Terminal).
  I have already overcome some errors such as "phantomjs - no such file or directory exists", but have got the latest Binaries from <a href="https://github.com/eugene1g/phantomjs/releases" rel="nofollow noreferrer">here</a>, which promise to be the latest, including a pending patch from the official PhantomJS team.</p>
  
  <p><strong>My Test Script:</strong></p>
  
  <pre><code>from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  
  desired_cap = {
          'phantomjs.page.settings.loadImages' : True,
          'phantomjs.page.settings.resourceTimeout' : 10000,
          'phantomjs.page.settings.userAgent' : "my_user_agent"
          }
  
  driver = webdriver.PhantomJS(executable_path= "/usr/local/bin/phantomjs", desired_capabilities=desired_cap)
  driver.set_window_size(1024, 768)
  driver.get('https://google.com/')
  
  driver.save_screenshot("testing.png")
  driver.page_source("source_code.txt")
  
  element = driver.find_element_by_xpath('--*[@id=-gbqfq-]')
  element.send_keys('testing')
  element.send_keys(Keys.ENTER)
  </code></pre>
  
  <p>Here is a link to the webdriver's simple explanation:
  <a href="http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_phantomjs/selenium.webdriver.phantomjs.webdriver.html" rel="nofollow noreferrer">http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_phantomjs/selenium.webdriver.phantomjs.webdriver.html</a></p>
  
  <p><strong>The Error Message:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/VdKrp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VdKrp.png" alt="The error message that I was getting"></a></p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>I took a simple example from a tutorial before trying to actually perform anything more complicated, but still get errors. One <a href="http://blog.likewise.org/2013/04/webdriver-testing-with-python-and-ghostdriver/" rel="nofollow noreferrer">tutorial</a>... a <a href="http://toddhayton.com/2015/02/03/scraping-with-python-selenium-and-phantomjs/" rel="nofollow noreferrer">second</a></p>
  
  <p>I made one last change to the phantomjs <code>service.py</code> file, which I found <a href="https://github.com/detro/ghostdriver/issues/236" rel="nofollow noreferrer">here</a>. Namely, I changed:</p>
  
  <p><code>self.process = subprocess.Popen(self.service_args,
  stdout=self._log, stderr=self._log)</code>  to:</p>
  
  <p><code>self.process = subprocess.Popen(['/usr/bin/env', 'phantomjs', '--webdriver=59202'])</code></p>
  
  <p>The last arguement <code>--webdriver</code> seems rather arbitrary to my inexperienced eyes. I thought it might correlate to the port that it used by ghostdriver, which is displayed after each run in the <code>ghostdriver.log</code> and is different each time. Because it changes each time, I don't think using it makes sense to have it static in the code - and after trying anyway, it didn't work.</p>
  
  <p><strong>The Question:</strong></p>
  
  <p>Does anybody have any ideas about why the connection is being refused??</p>