centos7 selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to b...

  1、查看安装的chrome浏览器版本

    

  2、查看版本对应的驱动

    https://sites.google.com/a/chromium.org/chromedriver/downloads

    

    

    下载后拷贝到/usr/local/bin/目录下

 

    结果在运行程序的时候右报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

    后来网上查下修改如下代码:

    

chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(chrome_options=chrome_options)

    就正常运行了

   3、完整代码

# coding=utf-8
import os
import re
from selenium import webdriver
# from pyvirtualdisplay import Display
from selenium.webdriver.chrome.options import Options
from datetime import datetime,timedelta
import time
from pyquery import PyQuery as pq
import re

import datetime

class consumer:

    def __init__(self):
        #通过配置文件获取IEDriverServer.exe路径
        # IEDriverServer ='C:\Program Files\Internet Explorer\IEDriverServer.exe'
        # self.driver = webdriver.Ie(IEDriverServer)
        # self.driver.maximize_window()
        # self.driver = webdriver.PhantomJS(service_args=['--load-images=false'])
        # self.driver = driver = webdriver.Chrome()

        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(chrome_options=chrome_options)

        # options = webdriver.FirefoxOptions()
        # # options.set_headless()
        # options.add_argument('--headless')
        # options.add_argument('--disable-gpu')
        # self.driver = webdriver.Firefox(firefox_options=options)
        # self.driver.set_page_load_timeout(10)
        # self.driver.maximize_window()

        # display = Display(visible=0, size=(800, 600))
        # display.start()
        # self.driver = webdriver.Firefox()
        # self.driver.maximize_window()



    def WriteLog(self, message,date):
        dir = os.path.join(os.getcwd(), 'consumer1')
        if not os.path.exists(dir):
            os.mkdir(dir)
        fileName = os.path.join(dir, date  +   '.txt')
        with open(fileName, 'a') as f:
            f.write(message)

    # http://search.cctv.com/search.php?qtext=消费主张&type=video
    def CatchData(self,url='http://search.cctv.com/search.php?qtext=%E6%B6%88%E8%B4%B9%E4%B8%BB%E5%BC%A0&type=video'):
        error = ''
        try:
            self.driver.get(url)

            selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
            doc = pq(selenium_html)


            message = '{0},{1}'.format( '标题', '时间')
            filename = datetime.datetime.now().strftime('%Y-%m-%d')
            self.WriteLog(message, filename)
            pages = doc("div[class='page']").find("a")
            # 2018-06-05 00:12:21
            pattern = re.compile("\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}")
            for index in range(1,6):
                url = "get_data('{0}', '消费主张', 'relevance', 'video', '-1', '1', '', '20', '1')".format(index)

                self.driver.execute_script(url)
                selenium_html = self.driver.execute_script("return document.documentElement.outerHTML")
                doc = pq(selenium_html)
                print(index)
                try:
                    Elements = doc("div[class='jvedio']").find("a")
                    for sub in Elements.items():
                        title = sub.attr('title')
                        print(title)
                        ts = pattern.findall(title)
                        strtime = ''
                        if ts and len(ts) == 1:
                            strtime = ts[0]
                        if strtime:
                            index1 = title.index(strtime)
                            title = str(title[0:index1]).replace("","")

                        title = '\n{0},{1}'.format(title, strtime)
                        self.WriteLog(title, filename)
                except Exception as e:
                    print("OS error: {0}".format(e))


        except Exception as e1:
            error = "ex"


# python "C:\Program Files (x86)\JetBrains\PyCharm 2016.2.3\helpers\pydev\setup_cython.py" build_ext --inplace


obj = consumer()

obj.CatchData()
# obj.CatchContent('')
# obj.export('')
View Code

 

 

    

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CentOS操作系统中出现此错误提示是因为无法找到Chrome驱动程序的可执行文件。Chrome驱动程序是用于控制和操作Chrome浏览器的工具。为了解决此问题,您可以按照以下步骤进行操作: 1. 首先,您需要下载与您使用的Chrome浏览器版本对应的Chrome驱动程序。您可以在Chrome浏览器的官方网站上找到可用的Chrome版本和对应的驱动程序版本。 2. 下载完成后,将Chrome驱动程序的可执行文件解压缩到一个您可以轻松访问的目录中,例如“/usr/local/bin”。 3. 接下来,您需要将该目录添加到系统的环境变量中。在终端中运行以下命令来编辑系统的环境变量配置文件: ``` sudo vi /etc/environment ``` 4. 在打开的文件中,找到"PATH="这一行,将路径添加到该行的最后,例如: ``` PATH="/usr/local/bin:$PATH" ``` 5. 保存并关闭文件,在终端中运行以下命令使更改生效: ``` source /etc/environment ``` 6. 最后,重新启动终端或重新登录系统,然后再次运行您的程序,应该能够找到Chrome驱动程序并正常工作了。 通过以上步骤添加Chrome驱动程序的可执行文件路径到系统的环境变量中,您就可以解决在CentOS操作系统中遇到的"chromedriver' executable needs to be in path"的错误提示了。 ### 回答2: "CentOS消息:chromedriver可执行文件需要在路径中。" 这个消息是因为在CentOS操作系统上运行web浏览器驱动程序chromedriver时,系统无法找到该可执行文件的路径。为了解决这个问题,您可以按照以下步骤操作: 1. 确保您的CentOS系统已经安装了Google Chrome浏览器。您可以通过在终端中运行以下命令来检查: ```shell google-chrome --version ``` 如果未安装Google Chrome,请按照官方文档中的指导进行安装。 2. 下载适用于CentOS的最新版本的chromedriver,可以从官方网站(https://sites.google.com/a/chromium.org/chromedriver/downloads)或其他可靠来源获取。 3. 解压下载的chromedriver文件。 4. 将解压后的chromedriver可执行文件移动到一个可执行文件的路径中,以便系统可以找到它。您可以将它移动到`/usr/local/bin/`目录下,使用以下命令: ```shell sudo mv /path/to/chromedriver /usr/local/bin/ ``` 注意将`/path/to/chromedriver`替换为实际文件的路径。 5. 确保移动后的chromedriver可执行文件在`/usr/local/bin/`目录中。 现在,您应该能够在CentOS系统上成功运行chromedriver了,而不再收到"chromedriver可执行文件需要在路径中"的消息。 ### 回答3: CentOS系统报错信息:“chromedriver'executable needs to be in path”意思是Chrome驱动程序没有在系统的路径中找到。Chrome驱动程序是一个连接Chrome浏览器和Selenium的桥梁,用于模拟用户在浏览器中的操作,使得Selenium可以自动化测试和控制浏览器。 要解决这个问题,可以按照以下步骤进行操作: 1. 首先,确保已经安装了Google Chrome浏览器。可以在终端中使用命令“google-chrome --version”来验证Chrome是否已成功安装并显示版本信息。 2. 接下来,需要下载对应的Chrome驱动程序。可以在https://sites.google.com/a/chromium.org/chromedriver/上找到适合您的Chrome浏览器版本的驱动程序。确保选择与Chrome浏览器版本匹配的驱动程序并下载。 3. 下载完成后,将驱动程序解压缩。可以选择将驱动程序放在任意文件夹中,只要该文件夹在系统的环境变量中即可。常见的目标位置是将驱动程序放在/usr/local/bin目录中。 4. 将驱动程序所在的目录路径添加到系统的环境变量中。在终端中使用以下命令: export PATH=$PATH:/path/to/chromedriver 其中,/path/to/chromedriver是驱动程序所在的目录路径。 5. 最后,重新运行您的程序或命令,CentOS系统就能够找到Chrome驱动程序,并成功执行相关操作了。 总之,CentOS系统报错信息“chromedriver'executable needs to be in path”通常是由于Chrome驱动程序没有正确安装或没有添加到系统的环境路径中所致。按照上述步骤,将驱动程序正确安装并添加到系统环境变量中,即可解决该问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值