Con*_*sed 5 python selenium google-chrome selenium-webdriver
下面是我正在使用的代码 - 我试图使其尽可能简洁。
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import bs4
options=Options()
#options.add_argument('--headless') # Works while not headless?
options.add_argument('--disable-gpu') # Last I checked this was necessary.
options.add_argument("--user-data-dir=profiles\\") #Keeps login data
options.add_argument("--profile-directory=Profile 1")
driver=webdriver.Chrome(chrome_options=options)
driver.get("http://www.google.com")
html=driver.page_source
soup=bs4.BeautifulSoup(html, "html.parser")
print(soup)
Run Code Online (Sandbox Code Playgroud)
主要问题源于--user-data-dir
和--profile-directory
参数。在我的测试示例中,自定义 chrome 配置文件(通常位于C:\Users\User\AppData\Local\Google\Chrome\User Data
)位于当前目录中,以使其与任何当前正在运行的 chrome 会话分开。
如果--headless
在使用上述参数时启用了该参数,驱动程序将挂起(CMD 保持不变,不会在 Python 命令行上产生输出)。但是,当未启用时,窗口会弹出并按预期执行。
但是,--headless
在使用上述默认目录中的任何配置文件时确实有效。
这是控制台输出;
[0120/222514.611:ERROR:gpu_process_transport_factory.cc(967)] Lost UI shared context.
DevTools listening on ws://127.0.0.1:59961/devtools/browser/ee317ed6-93c7-47c2-b26d-63647980ba0d
[0120/222514.619:ERROR:devtools_http_handler.cc(289)] Error writing DevTools active port to file
[0120/222514.624:ERROR:cache_util_win.cc(19)] Unable to move the cache: 0
[0120/222514.625:ERROR:cache_util.cc(140)] Unable to move cache folder profiles\Default\GPUCache to profiles\Default\old_GPUCache_000
[0120/222514.625:ERROR:disk_cache.cc(184)] Unable to create cache
[0120/222514.625:ERROR:shader_disk_cache.cc(622)] Shader Cache Creation failed: -2
Run Code Online (Sandbox Code Playgroud)
因此,看起来 Chromium 在某个地方假设我正在使用该Default
配置文件,而实际上我指定我没有使用该配置文件。
有没有人有什么建议?提前致谢!
我能够通过添加以下选项来运行您的代码。
options.add_argument('--remote-debugging-port=45447')
Run Code Online (Sandbox Code Playgroud)
如果没有它,代码会挂起大约一分钟,然后抛出以下错误:
WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Run Code Online (Sandbox Code Playgroud)
评论摘录为我指出了一个解决方案:
当您发送 --remote-debugging-port=0 到 Chrome 时,devtools 选择自己的端口并将其作为名为“DevToolsActivePort”的文件写入 chrome::DIR_USER_DATA。
基本上,一旦端口设置为默认值 0 以外的值,就不需要检查DevToolsActivePort文件。完整的评论和错误在这里:chromedriver bug
希望这可以帮助!
归档时间: |
|
查看次数: |
3103 次 |
最近记录: |