【问题标题】:TypeError when using undetected-chromedriver使用未检测到的 chromedriver 时出现 TypeError
【发布时间】:2022-10-26 00:29:36
【问题描述】:

我正在使用 undetected-chromedriver 库但是当我运行时

import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get('https://nowsecure.nl')

我,不断收到以下错误:

[Process Process-1:
Traceback (most recent call last):
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\site-packages\undetected_chromedriver\dprocess.py", line 59, in _start_detached
    p = Popen([executable, *args], stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 947, in __init__    
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1356, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 561, in list2cmdline 
   for arg in map(os.fsdecode, seq):
  File "C:\Users\DC\AppData\Local\Programs\Python\Python39\lib\os.py", line 822, in fsdecode
    filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not NoneType](url)

如何解决这个问题?谢谢

【问题讨论】:

    标签: python selenium-webdriver selenium-chromedriver undetected-chromedriver


    【解决方案1】:

    这意味着尚未找到 Chrome 可执行文件。

    使用browser_executable_path 初始化uc.Chrome 时,您需要指定浏览器的路径:

    browser_executable_path: str, optional, default: None - use find_chrome_executable
        Path to the browser executable.
        If not specified, make sure the executable's folder is in $PATH
    

    【讨论】:

      【解决方案2】:
      chrome_options = uc.ChromeOptions()
      chrome_options.add_argument("--no-sandbox")
      chrome_options.add_argument("--disable-dev-shm-usage")
      chrome_options.add_argument("--start-maximized")
      chrome_options.add_argument('--disable-popup-blocking')
      
      driver = uc.Chrome(options=chrome_options)
      
      driver.get('https://nowsecure.nl')
      

      【讨论】: