2017-08-14 75 views
2

後,我完成運行以下Python代碼:正確端接硒瀏覽器在Python

在我 Linux
from selenium.webdriver import Firefox 
from contextlib import closing 

with closing(Firefox()) as browser: 
    # some code here 

目前還有聽geckodriver過程。

$ ss -arp 
LISTEN  0  128             localhost:44132               *:*  users:(("geckodriver",21698,3)) 
LISTEN  0  128             localhost:57893               *:*  users:(("geckodriver",20242,3)) 
LISTEN  0  128             localhost:34439               *:*  users:(("geckodriver",19440,3)) 
LISTEN  0  128             localhost:35435               

如何調整Python代碼,以便它也終止進程?

回答

1

這是因爲在硒的情況下,close方法會關閉當前的窗口/瀏覽器,但不會退出它。您需要使用quit方法。所以你的代碼應該是

from selenium.webdriver import Firefox 
from contextlib import contextmanager 

@contextmanager 
def quiting(thing): 
    try: 
     yield thing 
    finally: 
     thing.quit() 

with quiting(Firefox()) as browser: 
    browser.get("http://sample.com")