【问题标题】:selenium: either chrome not reachable or DevToolsActivePort file doesn't existselenium:chrome 无法访问或 DevToolsActivePort 文件不存在
【发布时间】:2022-01-04 15:50:56
【问题描述】:

我在 Mac 和 pycharm 中使用 selenium,这是我使用的版本:

chrome:  Version 96.0.4664.110 (Official Build) (x86_64)
chromedriver: 96.0.4664.45
selenium: 4.1.0

这是我正在运行以抓取页面的代码,

import sys
from bs4 import BeautifulSoup
from selenium import webdriver
import numpy as np


def main():
    pages = np.arange(1, 2, 1)

    for page in pages:

        results = []
        page = "https://www.spanishdict.com/conjugate/" + str(page)

        sys.path.insert(0, '/usr/lib/chromium-browser/chromedriver')
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument("--disable-setuid-sandbox")
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument("--remote-debugging-port=9222 ")

        driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)
        driver.get(page)

        soup = BeautifulSoup(driver.page_source, 'html.parser')
        for element in soup.find_all('a'):
            name = element.text
            results.append(name)

        print(results)

if __name__ == '__main__':
    main()

当我评论 chrome_options.add_argument("--remote-debugging-port=9222 ") 时,我得到错误:DevToolsActivePort 文件不存在,当我取消注释时,我得到错误 chrome not reachable。 以前的代码在 chrome 91 上可以正常工作,但由于它已更新,它不再工作了。我试过重新安装、重启我的笔记本电脑,甚至是我在网上找到的另一个调试端口(“--remote-debugging-port=12582”),但似乎没有任何效果

【问题讨论】:

    标签: python selenium-webdriver pycharm selenium-chromedriver arguments


    【解决方案1】:

    即使尝试了我在 stackoverflow 中找到的几乎所有解决方案,甚至降级了我的 chrome,我还是意识到没有任何效果。因此,我安装了firfox geckodriver for mac,并将其放在我的/usr/local/bin 中。但请注意 Mac 11 或更高版本可能不允许它运行,因此请查看此documentation 以解决问题。 之后您可以按照medium link. 测试 geckdriver。 还将以下路径添加到 webdriver。

    driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
    

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2019-11-24
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多