detatch 似乎根本不起作用。我拥有的脚本(当在命令提示符下运行而不是空闲时)在完成时关闭窗口。我认为添加分离选项可以防止这种情况发生。
我能做些什么来防止这个彩色打印机窗口在完成时关闭?为什么这不起作用?
代码:
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import selenium.webdriver.chrome.options as Options
from webdriver_manager.chrome import ChromeDriverManager
print("leeeeeel 0.1")
chrome_options = Options.Options()
chrome_options.add_argument("detatch")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()) ,options=chrome_options)
driver.get("https://www.google.com")
print("1")
命令提示符输出:
>python "close test.py"
leeeeeel 0.1
====== WebDriver manager ======
Current google-chrome version is 100.0.4896
Get LATEST chromedriver version for 100.0.4896 google-chrome
Driver [C:Userszfqaaa.wdmdriverschromedriverwin32100.0.4896.60chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:54339/devtools/browser/0132a2ac-6783-4485-bbbc-8b8a10178be2
1
[1772:5288:0420/135655.757:ERROR:device_event_log_impl.cc(214)] [13:56:55.757] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[1772:5288:0420/135655.758:ERROR:device_event_log_impl.cc(214)] [13:56:55.759] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[1772:5288:0420/135655.761:ERROR:device_event_log_impl.cc(214)] [13:56:55.761] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
当我在IDE中运行它时,无论是否添加detatch,chrome窗口都会保持打开状态。为什么detatch不起作用?
您不需要添加参数,而是需要添加experimental_option
,如下所示:
import selenium.webdriver.chrome.options as Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
"未被检测的硒";在某种程度上是正确的,但它的示例方式并没有充分说明可以完成什么或如何工作,下面的代码显示了如何更好地实现这一点,以及使Seleniumchrome驱动程序在最新版本的Selenium中运行的新格式的额外好处。(截至本文撰写之时,它是4.5.0版本)。以下代码经过测试并有效:代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('detach', True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com/")
希望这能帮助。。。。。。
问候
Colin