0

I wrote a Selenium script in python and wrote an installer for in bash so that I could use that script on another machine (all Macs with the current OSX).

Here is the relevant stuff from the installer (machines are brand new Macs, so anything of interest first needed to be installed):

#!/bin/bash
#get neccesities
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install wget
brew install chromedriver
export PATH=$PATH:/usr/local/bin/chromedriver
brew install git
# config git
git config --global credential.helper osxkeychain
git config --global user.name "SOMENAME"
git config --global user.email "SOMEMAIL"
#get virtualenv
sudo easy_install pip
sudo pip install virtualenv
#get chrome
wget https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg
open ~/googlechrome.dmg
sudo cp -r /Volumes/Google\ Chrome/Google\ Chrome.app /Applications/
sudo diskutil unmountDisk /dev/disk3
rm -Rf ~/googlechrome.dmg
#clone repo and setup the venv
cd somewhere
git clone some_repo.git
virtualenv env
source ./env/bin/activate
pip install -r requirements.txt

Now, this installer has worked (more or less) on three to four machines in the last 2 months, but now I cannot seem to get the script running properly. When I try to run file.py I get the following error:

Traceback (most recent call last):
  File "file.py", line 56, in <module>
    reminder.driver.quit()
AttributeError: MyReminder instance has no attribute 'driver'

The actual issue lies before line 56 as chromedriver never opens up Chrome.

file.py

from selenium import webdriver

class MyReminder:
    def __init__(self,job):
        self.job = job

    def run(self):
        options = webdriver.ChromeOptions()
        options.add_argument("window-size=1280,960")
        self.driver = webdriver.Chrome(chrome_options=options)
        ## do some stuff ##

reminder = MyReminder(job.id)
while True:
    try:
        reminder.run()
    except:
        reminder.driver.quit()

To be precise, this python script currently works on four different machines. I am almost confident that the issue lies somewhere in the chromedriver/selenium/python interpreter/combo, I just do not understand where.

EDIT: Thanks to a helpful comment, I put the reminder.run() outside the try-block in order to get a more concise traceback:

Traceback (most recent call last):
  File "file.py", line 52, in <module>
    reminder.run()
  File "file.py", line 15, in run
    self.driver = webdriver.Chrome(chrome_options=options)
  File "/Users/.../env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/Users/.../env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 102, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver`

I managed to solve this issue using an answer someone else has already given here.

Thanks for the help.

  • Why do you do `reminder.run()` twice, one inside the `try` and one outside? – Colin Ricardo Apr 17 '18 at 17:22
  • @Colin my bad, wrongful copy & paste. I corrected it. – Daniel Stojanovic Apr 17 '18 at 20:52
  • Is `MyReader` a typo a well? – Colin Ricardo Apr 17 '18 at 21:22
  • oh boy.. apologies. yes it was.. i hope this is all right now. – Daniel Stojanovic Apr 18 '18 at 07:18
  • The attribute `self.driver` isn't created until `run()` completes successfully. On the machines where the try statement raises an error, the except block tries to access the as-of-yet uncreated attribute. Run `reminder.run()` outside the try block to see what error is raised and report back with a full traceback error. – Reti43 Apr 18 '18 at 08:01
  • You have also learnt not to use `except:` to catch all exceptions. You should explicitly catch only the exceptions you're prepared to deal with it and let everything else fail. – Reti43 Apr 18 '18 at 08:05
  • Thank you for that advice. While I generally understand the concept you are referring to and try to obide it whenever possible, this is a situation where I explicitly need the script to catch all exceptions. – Daniel Stojanovic Apr 18 '18 at 09:17
  • I added the new traceback error and found this answer to the issue at hand, which solved my problem. – Daniel Stojanovic Apr 18 '18 at 09:40

1 Answers1

0

Figured it out myself after eliminating the try-except block that was hiding the real traceback.

Chromedriver needs the file /etc/hosts to contain 127.0.0.1 localhost in order to execute properly.

With sudo echo "127.0.0.1 localhost" >> /etc/hosts, this can be done easily from the terminal.

Answer found here.