Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
硒不';t在键盘中断时退出(Python)_Python_Selenium - Fatal编程技术网

硒不';t在键盘中断时退出(Python)

硒不';t在键盘中断时退出(Python),python,selenium,Python,Selenium,当用户中断程序时,我试图关闭我的应用程序打开的Firefox实例。但是selenium并没有关上窗户。下面是显示问题的示例代码: import time from selenium import webdriver driver = webdriver.Firefox() try: while True: driver.get("https://google.com") time.sleep(5) except KeyboardInterrupt:

当用户中断程序时,我试图关闭我的应用程序打开的Firefox实例。但是selenium并没有关上窗户。下面是显示问题的示例代码:

import time

from selenium import webdriver

driver = webdriver.Firefox()

try:
    while True:
        driver.get("https://google.com")
        time.sleep(5)
except KeyboardInterrupt:
    print("Quitting")
    driver.quit()
    print("Done")

当我点击Ctrl+C时,我看到控制台上打印“退出”,几分钟后,我看到控制台上打印“完成”,程序结束。但firefox窗口仍然打开。我如何解决这个问题

编辑:


这也行不通。如果等待循环结束,则浏览器将成功关闭。但是如果在循环的中间命中CTRL+C,浏览器仍然是打开的。< /P> < P>循环时没有提供终止条件。您的while循环正在无休止地运行。
键盘中断块永远不会执行,因为控件不会退出while循环。

您可以尝试在代码中使用
with
语句,详细信息如下:

如果您想捕获
键盘中断
或/和
系统退出
,您可以找到有用的信息

我在终端和Firefox中测试了我的代码和下面的代码,完全符合预期:

import time
from selenium import webdriver

driver = webdriver.Firefox()

try:
    for i in range(5):
        print("Looping")
        driver.get("https://google.com")
        time.sleep(5)
except KeyboardInterrupt:
    print("quitting: KeyboardInterrupt")
finally:
    driver.quit()
    print("Done")

我对这个问题进行了研究,并检查了github上Stackoverflow和Selenium讨论论坛上所有可能的资源

由于没有适当的解决办法,同样的问题后来也提出并解决了。您可以在这里查看详细信息。此问题特定于Windows和firefox。它在MAC OS上运行良好。我知道这是由于Geckodriver发生的主要原因是它的崩溃,在这里你可以检查一下

他们也尝试了最新的Python绑定和最新的Geckodriver。没有一个干净的解决方案可用,而是一个解决办法,可以杀死firefox驱动程序生成的
firefox.exe
进程

请看一看

    tasklist = check_output(["tasklist", "/fi", "imagename eq firefox.exe"], shell=True).decode()
    currentFFIDs = re.findall(r"firefox.exe\s+(\d+)", tasklist)

    driver = webdriver.Firefox(options=opts, executable_path='./bin/geckodriver.exe')

    tasklist = check_output(["tasklist", "/fi", "imagename eq firefox.exe"], shell=True).decode()
    firefoxIds = set(re.findall(r"firefox.exe\s+(\d+)", tasklist)).difference(currentFFIDs)

 # do your stuff

    try:
        driver.close()
        driver.quit()

        # Could't close the driver via normal means-- Force Close #
        except:
            taskkill = 'taskkill /f '+''.join(["/pid "+f+" " for f in firefoxIds]).strip()
            check_output(taskkill.split(), shell=True)
            print("\nHAD TO FORCE-CLOSE FIREFOX", flush=True)

是的。异常导致循环结束。它不会停止。检查示例程序。只需运行无限while循环,并将键盘中断条件置于外部,而阻止它将不起作用。所以你最好在while块中提供退出条件。自己运行程序。我运行示例循环,同时将键盘中断条件放入if块的while循环中。并在第一次迭代后退出。将while替换为limited for循环。例如,对于范围(10)中的i:
。然后在循环的中间命中CTRL+C。如果没有键盘中断异常,浏览器将成功关闭。但如果有,它就不起作用了。那不起作用。我尝试在
中使用
退出()
,除了
之外,但没有成功。它在并没有异常的情况下工作,但当有键盘中断异常时,它不会退出。只是在终端中尝试,并没有问题。你的“我的应用”是什么?我复制粘贴了你的代码。我执行了它,并在第二条“循环”消息后按Ctrl+C。它打印“退出:键盘中断”,几秒钟后打印“完成”。但firefox窗口仍然打开。有我可以与您共享的日志吗?您如何启动应用程序?只是python文件吗?我在windows上。我从pylauncher开始:
py-3。\repro.py
。我也试过用PyCharm来执行它。结果是一样的。是的,它是唯一的文件。请尝试发送驱动程序。先关闭。。。(尝试/捕获它..如果得到异常,请使用驱动程序。退出..如果未捕获异常,请使用驱动程序。退出)。。。我认为部分问题在于驱动程序/有线协议的半异步性质。它不喜欢在仍在运行任务时退出。为了取消任务,我使用另一个线程,该线程设置bool以首先中断现有循环。(不是立即…)对于.get,驱动程序将等待返回pageload状态,例如。。。你必须让这种交流发生(或不发生)。@pcalkins我也想到了这一点。因此,当代码处于
时间段时,我小心地按Ctrl-C。sleep(5)
部分。这并没有改变结果。我还尝试在退出()之前使用
close()
。结果相同。
import time
from selenium import webdriver

driver = webdriver.Firefox()

try:
    for i in range(5):
        print("Looping")
        driver.get("https://google.com")
        time.sleep(5)
except KeyboardInterrupt:
    print("quitting: KeyboardInterrupt")
finally:
    driver.quit()
    print("Done")
    tasklist = check_output(["tasklist", "/fi", "imagename eq firefox.exe"], shell=True).decode()
    currentFFIDs = re.findall(r"firefox.exe\s+(\d+)", tasklist)

    driver = webdriver.Firefox(options=opts, executable_path='./bin/geckodriver.exe')

    tasklist = check_output(["tasklist", "/fi", "imagename eq firefox.exe"], shell=True).decode()
    firefoxIds = set(re.findall(r"firefox.exe\s+(\d+)", tasklist)).difference(currentFFIDs)

 # do your stuff

    try:
        driver.close()
        driver.quit()

        # Could't close the driver via normal means-- Force Close #
        except:
            taskkill = 'taskkill /f '+''.join(["/pid "+f+" " for f in firefoxIds]).strip()
            check_output(taskkill.split(), shell=True)
            print("\nHAD TO FORCE-CLOSE FIREFOX", flush=True)