Chromedriver无法在python Selenium上运行 [英] Chromedriver not working on python selenium

查看:78
本文介绍了Chromedriver无法在python Selenium上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python编写了一个Selenium脚本,并用bash编写了一个安装程序,这样我就可以在另一台机器上使用该脚本(所有装有当前OSX的Mac).

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).

以下是安装程序中的相关内容(机器是全新的Mac,因此首先需要安装任何感兴趣的东西)

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

现在,该安装程序在过去两个月中已(在或多或少)在三到四台计算机上工作,但是现在我似乎无法正常运行该脚本.当我尝试运行file.py时,出现以下错误:

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'

实际问题出在第56行之前,因为chromedriver从不打开Chrome.

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()

确切地说,此python脚本当前可在四台不同的计算机上运行.我几乎可以确定问题出在chromedriver/selenium/python解释器/combo中,我只是不知道在哪里.

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.

由于提供了有益的评论,我将hinter.run()放在了try块之外,以便获得更简洁的回溯:

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.

感谢您的帮助.

推荐答案

在消除隐藏了 real 追溯的try-except块之后,我自己解决了这个问题.

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

Chromedriver需要文件/etc/hosts 包含 127.0.0.1 localhost 才能正常执行.

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

使用 sudo echo"127.0.0.1 localhost">>/etc/hosts ,这可以从终端轻松完成.

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

此处找到答案

这篇关于Chromedriver无法在python Selenium上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆