简体   繁体   中英

Chromedriver not working on python selenium

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM