簡體   English   中英

如何從 Python 以隱身模式打開 chrome

[英]how to open chrome in incognito mode from Python

這在powershell中有效:

Start-Process chrome.exe -ArgumentList @( '-incognito', 'www.foo.com' )

如何從 Python 實現這一點?

使用 webbrowser 在 chrome 中打開隱身模式的 Python 腳本

import webbrowser
url = 'www.google.com'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --incognito'
webbrowser.get(chrome_path).open_new(url)

在我的計算機上,intboolstring 的方法不起作用,另一種更全功能的方法是使用來自子進程模塊的 call(),盡管如果命令更改,仍然可以使用 system()。

from subprocess import call
call("\"C:\Path\To\chrome.exe\" -incognito www.foo.com", shell=True)

或者使用 system():

from os import system
system("\"C:\Path\To\chrome.exe\" -incognito www.foo.com")

如果將 chrome 添加到路徑或通過 powershell 運行命令,也可以僅使用“chrome.exe -incognito www.foo.com”啟動 chrome,如下所示:

system("powershell -C Start-Process chrome.exe -ArgumentList @( '-incognito', 'www.foo.com' )")

盡管此方法比將 chrome.exe 添加到路徑要慢得多。

使用os模塊執行命令。

import os
os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -ArgumentList @( '-incognito', 'www.foo.com'" )

可以在此處找到有關os.system更多信息。

import subprocess
subprocess.Popen(["C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "-incognito", "www.google.com"])

此代碼有效。 它啟動一個新的隱身標簽,然后切換驅動程序來控制新標簽

def incognito():
    global driver
    driver = webdriver.Chrome()
    driver.get('https://www.google.com')
    search=driver.find_element_by_id('lst-ib')
    incognito=search.send_keys(Keys.CONTROL+Keys.SHIFT+'N')
    driver.switch_to_window(driver.window_handles[-1])
    driver.get('https://web.whatsapp.com/')
    time.sleep(5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM