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.

8
  • Why do you do reminder.run() twice, one inside the try and one outside? Commented Apr 17, 2018 at 17:22
  • @Colin my bad, wrongful copy & paste. I corrected it. Commented Apr 17, 2018 at 20:52
  • Is MyReader a typo a well? Commented Apr 17, 2018 at 21:22
  • oh boy.. apologies. yes it was.. i hope this is all right now. Commented Apr 18, 2018 at 7: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. Commented Apr 18, 2018 at 8:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.