简体   繁体   English

Chromedriver无法在python Selenium上运行

[英]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). 我用python编写了一个Selenium脚本,并用bash编写了一个安装程序,这样我就可以在另一台机器上使用该脚本(所有装有当前OSX的Mac)。

Here is the relevant stuff from the installer (machines are brand new Macs, so anything of interest first needed to be installed): 这是安装程序中的相关内容(机器是全新的Mac,因此首先需要安装任何感兴趣的东西):

#!/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. 现在,该安装程序在过去2个月中已(在或多或少)在三到四台计算机上工作,但是现在我似乎无法正常运行该脚本。 When I try to run file.py I get the following error: 当我尝试运行file.py时,出现以下错误:

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. 实际问题出在第56行之前,因为chromedriver从不打开Chrome。

file.py 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. 确切地说,此python脚本当前可在四台不同的计算机上运行。 I am almost confident that the issue lies somewhere in the chromedriver/selenium/python interpreter/combo, I just do not understand where. 我几乎可以确定问题出在chromedriver / selenium / python解释器/ combo中,我只是不知道在哪里。

EDIT: Thanks to a helpful comment, I put the reminder.run() outside the try-block in order to get a more concise traceback: 编辑:由于提供了有益的评论,我将hinter.run()放在try块之外,以便获得更简洁的回溯:

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. 消除隐藏真实回溯的try-except块后,自己找出了答案。

Chromedriver needs the file /etc/hosts to contain 127.0.0.1 localhost in order to execute properly. Chromedriver需要文件/ etc / hosts包含127.0.0.1 localhost才能正常执行。

With sudo echo "127.0.0.1 localhost" >> /etc/hosts , this can be done easily from the terminal. 使用sudo echo "127.0.0.1 localhost" >> /etc/hosts ,可以从终端轻松完成此操作。

Answer found here . 这里找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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