[英]Google chrome site closes immediately after being launched with selenium
[英]Google chrome closes immediately after being launched with selenium
我在 Mac OS X 上使用 selenium 和 python 3.6.3。
這段代碼運行良好,打開谷歌瀏覽器並且 chrome 保持打開狀態。:
chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("disable-infobars");
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.google.com/")
但是由於代碼包含在 function 中,瀏覽器在打開頁面后立即終止:
def launchBrowser():
chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("disable-infobars");
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.google.com/")
launchBrowser()
我想在 function 中使用相同的代碼,同時保持瀏覽器打開。
我的猜測是驅動程序會被垃圾收集,在 C++ 中,函數(或類)內的對象在脫離上下文時會被銷毀。 Python 的工作方式並不完全相同,但它是一種垃圾收集語言。 一旦不再引用對象,它們將被收集。
要解決您的問題,您可以將對象引用作為參數傳遞,或返回它。
def launchBrowser():
chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("start-maximized");
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.google.com/")
return driver
driver = launchBrowser()
只需簡單地添加:
while(True):
pass
到你的函數結束。 它會是這樣的:
def launchBrowser():
chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("disable-infobars");
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.google.com/")
while(True):
pass
launchBrowser()
一旦驅動程序的變量超出范圍,瀏覽器就會自動處理。 因此,為了避免退出瀏覽器,您需要在全局變量上設置驅動程序的實例:
Dim driver As New ChromeDriver
Private Sub Use_Chrome()
driver.Get "https://www.google.com"
' driver.Quit
End Sub
為了使 borwser 保持打開狀態,我正在這樣做:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def browser_function():
driver_path = "path/to/chromedriver"
chr_options = Options()
chr_options.add_experimental_option("detach", True)
chr_driver = webdriver.Chrome(driver_path, options=chr_options)
chr_driver.get("https://target_website.com")
這有點舊,但這里的答案並沒有解決問題。 一點谷歌搜索讓我來到這里
http://chromedriver.chromium.org/getting-started
這里的測試代碼使用 sleep 來保持瀏覽器打開。 我不確定是否有更好的選擇,所以我會在學習時更新它。
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
我的解決方案是先在init函數中定義驅動程序,然后它不會關閉瀏覽器,即使是actional
誰有同樣的問題,只需將驅動程序設置為全局,簡單如下:
global driver
driver.get("http://www.google.com/")
這解決了我這邊的問題,希望這會有所幫助
根據您共享的代碼塊,可以有 3 種可能的解決方案,如下所示:
binary_location
您必須將..
更改為.
( .
表示項目工作區)binary_location
您必須將..
更改為/myspace/chrome
(絕對 Chrome 二進制) 在啟動驅動程序時,添加開關executable_path
:
driver = webdriver.Chrome(chrome_options=options, executable_path=r'/your_path/chromedriver')
def createSession():
**global driver**
driver = webdriver.Chrome(chrome_driver_path)
driver.maximize_window()
driver.get("https://google.com")
return driver
我在使用Java時遇到了同樣的問題,在我的情況下,這是因為Chrome和Chrome驅動程序的版本不兼容。
具體而言:ChromeDriver不支持Chrome版本,例如:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 75
因此,瀏覽器在啟動后立即關閉。
為了解決這個問題,我更改了Chrome版本,問題消失了。
為防止這種情況發生,請確保您的driver
變量在您的函數之外定義或定義為全局變量,以防止在函數完成執行后立即對其進行垃圾回收。
在上面的例子中,這可能意味着:
driver = webdriver.Chrome(chrome_options=get_options())
def get_options():
chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("disable-infobars")
return chrome_options
def launchBrowser():
driver.get("http://www.google.com/")
launchBrowser()
嗨,以下解決方案對我有用,請試試這些。
解決方案1:使用 Selenium Web 驅動程序啟動后,Google chrome 會自動關閉。
檢查您擁有的 Chrome 瀏覽器驅動程序 exe 文件的版本和 google chrome 版本,如果驅動程序不兼容,則 selenium-web 驅動程序瀏覽器在 selenium-web 驅動程序打開后立即終止,嘗試根據您的 chrome 版本匹配驅動程序版本
瀏覽器關閉的原因是程序結束,驅動變量在最后一行代碼后被垃圾回收。 對於帖子中的第二個代碼,無論您是在函數中還是在全局范圍內使用它都沒有關系。 在解釋完最后一條語句后,驅動程序變量被垃圾收集,瀏覽器從程序終止中終止。
解決方案:設置 chrome 選項
使用時間模塊
在程序末尾使用無限循環來延遲程序關閉
添加實驗性選項 detach true 在這里工作:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.