【问题标题】:Python Selenium window closing no matter whatPython Selenium窗口无论如何都会关闭
【发布时间】:2022-07-20 22:38:10
【问题描述】:

我真的不喜欢提问,但我就是不知道我的代码有什么问题。我是 selenium 的新手,如果有什么明显的地方请见谅。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(options=chrome_options, service=s)
driver.maximize_window()
driver.get('https://www.youtube.com')

此代码有效,并成功打开了 youtube,但是,窗口将在打开后不久关闭。为了解决这个问题,我在代码中添加了 'detach True' 选项,如上所示 (Python selenium keep browser open),但是,这没有奏效,窗口将在打开几秒钟后关闭。运行代码时也出现了这个错误。

[17708:21796:0720/212826.842:ERROR:device_event_log_impl.cc(214)] [21:28:26.841] USB:usb_device_handle_win.cc:1048 无法从节点连接读取描述符:连接到的设备系统不工作。 (0x1F)

我查看了 SO 上遇到此问题的其他人,但所有资源都说忽略它并且它不应该影响程序的运行。为了阻止弹出错误消息,我将此行放入我的代码中。 chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) 这阻止了错误的出现,但并没有阻止窗口关闭。

感谢任何帮助,我正在 Windows 10 上运行最新版本的 VS。

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    在您的测试用例完成运行后,无论如何它都会关闭浏览器。在您的情况下,浏览器将在您导航到 youtube 后立即关闭。您没有其他任何东西,只要您导航到 youtube,您的测试用例就完成了。

    但是,如果您想观察更多并在导航后留在 youtube 上,您可以添加等待时间,这样它就不会在导航到 youtube 后立即关闭。

    尝试在下面添加这一行,让它等待 10 秒。

    time.sleep(10)

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 1970-01-01
      • 2019-05-16
      • 2021-03-11
      • 2017-06-04
      • 1970-01-01
      • 2021-06-21
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多