Python - 使用 autoit 自动启动 Chrome 并在地址栏中输入 URL

Python - Automating Chrome to start and enter URL in address bar using autoit

我是 AutoIt 及其 Python PyAutoIt 库的新手。我想让我的 Python 脚本打开 Chrome 浏览器,按 ALT+D 转到地址栏Chrome 浏览器,输入 https://www.yahoo.com 并按 ENTER

这是我尝试执行的代码:

import autoit

autoit.run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
autoit.send( "!d^ahttps://www.yahoo.com{ENTER}" ) 

此代码中的问题是它在将 URL 写入我的 Python IDE 时也在将其写入浏览器。

只是提一下 !d 表示 ALT+D^a 表示 CTRL+A.

OS: Windows 10 x64
Python IDE: Anaconda Spyder
PyAutoIt 版本:0.5

我对评论中michael_health的内容做了一些研究,发现了以下问题的解决方案:

import autoit
import time

google_chrome_pid = autoit.run('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -incognito', show_flag=3) # run in incognito mode, start maximized
autoit.win_wait_active("New Tab", timeout=10) # active window 
time.sleep(3)
autoit.opt("SendKeyDelay", 100) # Slow down typing to 100 milliseconds
autoit.send("!dhttps://www.yahoo.com")
time.sleep(2)
autoit.send("{ENTER}")