【问题标题】:How to add chromedriver?如何添加chromedriver?
【发布时间】:2019-11-03 17:29:56
【问题描述】:

大约 8 小时我还没弄清楚如何安装 Chrome 驱动程序。我做了很多研究,但我从未尝试过。 这是我的部署包文件的内容:http://prntscr.com/o4kcjw 当我通过无服务器 CLI 尝试它时,我遇到了很多错误。我该如何解决这个问题?

我使用 virtualenv 创建并分配 selenium、pymsql 和 chromedriver 给 Lambda。 (压缩) 我的压缩文件中有一个 python 文件。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=960x900')

# Define browser driver
chrome_driver = '/chromedriver-Linux64.exe'
browser = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)

{ "errorMessage": "消息: 'chromedriver-Linux64.exe' 可执行文件 需要在 PATH 中。请参见 https://sites.google.com/a/chromium.org/chromedriver/home\n",
“errorType”:“WebDriverException”,“stackTrace”:[ " File \"/var/lang/lib/python3.7/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n", " 文件 \"/var/lang/lib/python3.7/imp.py\",第 171 行,在 load_source\n 模块 = _load(spec)\n", " 文件 \"\",第 696 行,在 _load\n" 中, " 文件 \"\",第 677 行,在 _load_unlocked\n", " 文件 \"\",第 728 行,在 exec_module\n" 中, " 文件 \"\",第 219 行,在 _call_with_frames_removed\n" 中, "文件\"/var/task/scraping.py\",第 16 行,在 \n browser = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)\n", " File \"/var/task/selenium/webdriver/chrome/webdriver.py\",第 73 行,在 init\n self.service.start()\n", " File \"/var/task/selenium/webdriver/common/service.py\",第 83 行,在 start\n os.path.basename(self.path) 中, self.start_error_message)\n" ] }

【问题讨论】:

    标签: python python-3.x aws-lambda selenium-chromedriver


    【解决方案1】:

    AWS Lambda 在 Linux 上运行。您必须下载linux chromedriver,并且文件名最后没有.exe。应该只是chromedriver

    【讨论】:

      【解决方案2】:

      您为什么不使用来自https://www.seleniumhq.org/download/ 的“普通”chromedriver?

      在 Windows 上这对我有用:

      driver = webdriver.Chrome(executable_path='D:/myPath/chromedriver.exe')
      

      我安装了我的 chromedriver,解压缩它,我保存它的路径是 'D:/myPath/'(只是一个例子)

      【讨论】:

      • 我做了,但它不再起作用了。 .zip 到 AWS Lambda。
      【解决方案3】:

      假设您的目录结构如下所示:

      project
      | main.py
      | chromedriver-Linux64
      

      您的 chromedriver 二进制文件应该与您的项目文件处于同一级别。此外,将指向 chromedriver 二进制文件的路径从 /chromedriver-Linux64.exe 更改为 ./chromedriver-Linux64.exe。您当前拥有的路径在根文件夹中查找二进制文件,而不是当前目录。 . 表示查看该文件所在的目录。

      【讨论】:

      • 我发送到 'chromedriver/chromedriver-Linux64.exe' 但我无法运行。
      • 你的目录结构是什么样的?
      【解决方案4】:

      这样写你的路径

      "C:\Users\arjun.bhardwaj\Downloads\drvr\chromedriver.exe

      【讨论】:

      • 但我的项目必须在 AWS Lambda 上运行;是不是很可笑?
      【解决方案5】:
      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      #can use below if pip/sudo install webdriver_manager
      from webdriver_manager.chrome import ChromeDriverManager
      
      chrome_options = Options()
      chrome_options.add_argument('--headless')
      #this below not really required if using --headless`enter code here`
      chrome_options.add_argument('--window-size=960x900') 
      # You can use the below to run
      browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chrome_options)
      browser.get('https://www.google.com/')
      

      【讨论】:

        猜你喜欢
        • 2018-06-21
        • 2017-01-20
        • 2015-09-14
        • 1970-01-01
        • 2021-04-14
        • 2017-05-24
        • 1970-01-01
        • 2015-12-22
        • 1970-01-01
        相关资源
        最近更新 更多