使用 user-data-dir 参数时找不到 DevToolsActivePortfile/Chrome 无法访问

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

我正在尝试自动化本问题范围之外的另一个用例所需的 cookie 收集。当我尝试运行代码时,出现此错误,该错误仅在我在选项中使用

user-data-dir
和/或
profile-directory
参数时才会发生。首先我会得到
DevToolsActivePort not found
错误,但是添加
remote-debugging-port=8000
参数后,我得到
chrome not reachable
错误。

有几点需要注意:

  • 需要使用默认内置浏览器(Microsoft Edge)
  • 用户必须登录
  • 如果可能的话,我想在无头模式下运行它
  • 使用
    Edge
    ChromiumEdge
    类根本没有区别

我尝试在无头模式下运行,它没有给我任何错误,但用户没有登录到 Microsoft/Google 帐户。我已经搜索了几乎整个互联网,但没有结果

我的代码(在Python中使用selenium):

import time
import os

from selenium.webdriver import EdgeService
from selenium.webdriver import ChromiumEdge
from selenium.webdriver import EdgeOptions

# service = EdgeService(r'assets\msedgedriver.exe')
# service.start()

options = EdgeOptions()
options.add_argument(f"user-data-dir=C:\\Users\\{os.getlogin()}\\AppData\\Local\\Microsoft\\Edge\\User Data")
options.add_argument("profile-directory=Default")
options.add_argument("--disable-dev-shm-using") # an answer on stackoverflow which did not work
options.add_argument("remote-debugging-port=8000") # another one which did not work
options.binary_location = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"

driver = ChromiumEdge(options=options)

driver.get("https://google.com")
# driver.get("https://bing.com")

driver.save_screenshot("ss2.png")
with open("cookies.txt", "w") as f:
    contents = ""
    cookies = driver.get_cookies()
    for cookie in cookies:
        contents += cookie.get('name') + ": " + cookie.get('value') + "\n"
    f.write(contents)

我收到的错误(Pycharm 控制台输出以管理员身份运行以确保足够的权限):

Traceback (most recent call last):
  File "C:\path\to\project\test2.py", line 18, in <module>
    driver = ChromiumEdge(options=options)
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 56, in __init__
    super().__init__(
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\path\to\project\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Microsoft Edge failed to start: exited normally.
  (chrome not reachable)
  (The process started from msedge location C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe is no longer running, so msedgedriver is assuming that msedge has crashed.)
Stacktrace:
    GetHandleVerifier [0x00007FF6149E2602+60402]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF614967352+253666]
    (No symbol) [0x00007FF614739C99]
    (No symbol) [0x00007FF614772FB2]
    (No symbol) [0x00007FF61476D18B]
    (No symbol) [0x00007FF6147B423F]
    (No symbol) [0x00007FF6147AB863]
    (No symbol) [0x00007FF61477C585]
    (No symbol) [0x00007FF61477B993]
    (No symbol) [0x00007FF61477CD14]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF614BC34A4+1161924]
    (No symbol) [0x00007FF6147FE5F6]
    Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6148BB083+37459]
    Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6148B2C4D+3613]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF614BC21E4+1157124]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6149716A8+295480]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61496CB74+276228]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61496CCA2+276530]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF61495FA91+222753]
    BaseThreadInitThunk [0x00007FFF6B647344+20]
    RtlUserThreadStart [0x00007FFF6B7826B1+33]


Process finished with exit code 1
python python-3.x selenium-webdriver
1个回答
0
投票

我也面临着同样的问题,有什么解决办法吗

下面是我的代码:

从硒导入网络驱动程序

user_data_dir = r'C:\Users\\AppData\Local\Google\Chrome\User Data'

选项= webdriver.ChromeOptions()

options.add_argument("--start-maximized")

options.add_argument('--disable-blink-features=AutomationControlled')

options.add_argument("--disable-plugins-discovery")

options.add_argument('--user-data-dir='+user_data_dir)

options.add_argument('--profile-directory=默认')

driver = webdriver.Chrome(选项=选项)

driver.get('https://stackoverflow.com/')

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