Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Python 2.7 Python selenium CTRL+;C闭式镀铬器_Python 2.7_Selenium_Selenium Chromedriver_Try Except_Keyboardinterrupt - Fatal编程技术网

Python 2.7 Python selenium CTRL+;C闭式镀铬器

Python 2.7 Python selenium CTRL+;C闭式镀铬器,python-2.7,selenium,selenium-chromedriver,try-except,keyboardinterrupt,Python 2.7,Selenium,Selenium Chromedriver,Try Except,Keyboardinterrupt,如何在不关闭chromedriver的情况下捕获CTRL+C(键盘中断)。 当我通过cmd运行script.py时,它会关闭chromedriver driver = webdriver.Chrome() try: while True: #do stuff with chromedriver except KeyboardInterrupt: print "User pressed CTRL + C" #do other stuff with chrom

如何在不关闭chromedriver的情况下捕获CTRL+C(键盘中断)。 当我通过cmd运行script.py时,它会关闭chromedriver

driver = webdriver.Chrome()
try:
    while True:
        #do stuff with chromedriver
except KeyboardInterrupt:
    print "User pressed CTRL + C"
    #do other stuff with chromedriver
它确实捕捉到我脚本中的键盘中断,因此我的脚本继续,但chromedriver也会获取它并关闭它自己

编辑1:
通过CMD运行脚本或使用Pyinstaller冻结脚本并运行脚本时,解决方案不起作用(
IOError:[Errno 4]中断的函数调用

编辑2:

我还尝试让脚本忽略Errno 4(使用
try
异常除外
),但仍然有相同的结果(chromedriver关闭),因此简言之,根本没有帮助。

考虑使用
webdriver.Remote
风格。此选项不会在解释器中生成webdriver的本地版本,这将使您从SIGINT的麻烦中解脱出来

在另一个shell中启动webdriver-(Chrome的chromedriver,Firefox的geckodriver,等等) 注意监听端口。我将在这里使用默认值:chromedriver为9515,geckodriver为4444

在python脚本中:

铬:

driver=webdriver.Remote("http://127.0.0.1:9515",desired_capabilities=webdriver.DesiredCapabilities.CHROME)
Firerox:

driver=webdriver.Remote("http://127.0.0.1:4444",desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)

您也可以在启动驱动程序时禁用SIGINT处理。像这样:

import signal 

...

ps = signal.getsignal(signal.SIGINT) # backup signal handler 
signal.signal(signal.SIGINT, signal.SIG_IGN) # ignore signal temporarily

... = webdriver.Chrome(...) 

signal.signal(signal.SIGINT, ps) # restore original handler

我在Windows上有同样的问题,同样的代码在macOS和Linux上工作。我怀疑这是因为cmd.exe将CTRL+C发送到chromedriver.exe而不是python.exe。您试图提供哪些参数?好的,主要是用户数据目录和代理服务器,我使用的是socks5代理,希望保存环境。但我在评论时犯了一个错误,因为我使用的是ruby、watir和chromedriver。现在它正在使用caps设置。仍然无法直接获得选项或参数,但现在已满足要求。谢谢你的邀请。