如何在Repl.it上使用selenium?

问题描述 投票:0回答:2

我正在尝试在 Repl 中使用 Selenium,但由于某种原因它似乎不起作用,有办法解决这个问题吗?

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)

driver.get("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

views = driver.find_element_by_class("view-count style-scope ytd-video-view-count-renderer")
print(views)

这是我似乎遇到的两个错误。

Traceback (most recent call last):
  File "/home/runner/JustStuff/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    driver = webdriver.Chrome(options=chrome_options)
  File "/home/runner/JustStuff/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/home/runner/JustStuff/venv/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__
    self.service.start()
  File "/home/runner/JustStuff/venv/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

python selenium selenium-webdriver selenium-chromedriver read-eval-print-loop
2个回答
7
投票

你需要去replit.nix,点击隐藏文件,你就会看到它, 您需要将 pkgs.chromium 和 pkgs.chromedriver 导入 deps

{ pkgs }: {
deps = [
    pkgs.python38Full
    pkgs.chromium
    pkgs.chromedriver
];

别忘了刷新页面


0
投票

不涉及手动编辑

replit.nix
的答案:

在壳里,

  1. 运行
    chromedriver --help
    - 这会将 chromedriver 添加到您的
    replit.nix
  2. 运行
    chromium --help
    - 这将安装 chromium。有两个安装选项:
    ungoogled-chromium.out
    chromium.out
    。我选择了
    ungoogled-chromium.out

可能还有其他方法来安装它们,但是

--help
可以做到这一点。之后就是代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

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

driver.get('https://hoffa.medium.com')
html = driver.page_source
driver.quit()
print(html.count('<article>'))

为我工作。

© www.soinside.com 2019 - 2024. All rights reserved.