【发布时间】:2017-02-28 19:31:07
【问题描述】:
我目前正在尝试使用 python 为我在高中的编码 II 课程编写一个基本的智能镜。我想做的一件事是全屏打开新标签(使用chrome)。我目前有它,所以我可以打开网址,但我没有全屏显示它们。关于我可以用来全屏打开 chrome 的代码有什么想法吗?
【问题讨论】:
标签: python python-3.x tkinter python-webbrowser
我目前正在尝试使用 python 为我在高中的编码 II 课程编写一个基本的智能镜。我想做的一件事是全屏打开新标签(使用chrome)。我目前有它,所以我可以打开网址,但我没有全屏显示它们。关于我可以用来全屏打开 chrome 的代码有什么想法吗?
【问题讨论】:
标签: python python-3.x tkinter python-webbrowser
如果你使用selenium,只需如下代码:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://google.com')
driver.maximize_window()
【讨论】:
正如建议的那样,硒是完成任务的好方法。
为了让它全屏而不是最大化,我会使用:
chrome_options.add_argument("--start-fullscreen");
或
chrome_options.add_argument("--kiosk");
第一个选项模拟 F11 压力,您可以按 F11 退出。第二个使您的 chrome 进入“kiosk”模式,您可以按 ALT+F4 退出。
其他有趣的标志是:
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
这些将删除 chrome 驱动程序暴露的顶部栏,说它是 dev chrome 版本。
完整的脚本是:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
# chrome_options.add_argument("--start-fullscreen");
chrome_options.add_argument("--kiosk");
driver = webdriver.Chrome(executable_path=rel("path/to/chromedriver"),
chrome_options=chrome_options)
driver.get('https://www.google.com')
"path/to/chromedriver"应该指向与你从here下载的chrome版本兼容的chrome驱动
【讨论】: