python - 谷歌浏览器 - 如何激活谷歌浏览器并切换到特定的标签窗口?

标签 python windows google-chrome autohotkey hotkeys

在 Windows 系统上,我运行着 Python/QT GUI。现在按下按钮,我需要在我的应用程序前激活最小化或非最小化的谷歌浏览器。

如何激活 Google chrome,然后通过 static title nameprocess id titles(使用 Python 或其他方式)切换到非常具体的 TAB?

(例如激活第二个选项卡) enter image description here

Python:(不工作它只是打开它)

import webbrowser
url = 'http://docs.python.org/'
chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
webbrowser.get(chrome_path).open(url)

Ahk:失败

#d::
list := Acc_Get("Object", "4.23.1", 0, "ahk_class MozillaWindowClass")
;MsgBox % list.accChildCount-1
for each, tab in Acc_Children(list) {
   MsgBox, % tab.accName(0)
   tab.accDoDefaultAction(0)
}
Return


#c::WinActivate( "Calculator", "calc" )

#NoTrayIcon
#SingleInstance force

WinActivate( TheWindowTitle, TheProgramTitle )
{
    SetTitleMatchMode,2
    DetectHiddenWindows, Off

    IfWinExist, %TheWindowTitle%
    {
        WinGet, winid, ID, %TheWindowTitle%
        DllCall("SwitchToThisWindow", "UInt", winid, "UInt", 1)
    }
    Else
    {
        Run %TheProgramTitle%
    }
    Return
}

也试试:

If ChromeHasTabNamed( "Activating Chrome Tab" )
    MsgBox, Yes
Return


ChromeHasTabNamed( X ) {
    SetTitleMatchMode, 2
    WinGetTitle, title, - Chrome
    While Not InStr( list, title "`n" ) {
        list .= title "`n"
        ControlSend,, ^{Tab}, - Chrome
        Sleep, 50
        WinGetTitle, title, - Chrome
    }
    Return, InStr( list, X ) ? True : False
}

尝试直到死亡:

SetTitleMatchMode, 2
WinWaitActive - Google Chrome
ControlFocus, Chrome_RenderWidgetHostHWND1
Loop, 15
{
   WinGetTitle, Title, A  ;get active window title
   if(InStr(Title, "Gmail")>0)
   {
      break ; Terminate the loop
   }
   Send ^{Tab}
   Sleep, 50
}

return

最佳答案

有效。

chrome := "- Google Chrome"
found := "false"
tabSearch := "Gmail"
curWinNum := 0
SetTitleMatchMode, 2
WinGet, numOfChrome, Count, %chrome% ; Get the number of chrome windows
WinActivateBottom, %chrome% ; Activate the least recent window
WinWaitActive %chrome% ; Wait until the window is active
ControlFocus, Chrome_RenderWidgetHostHWND1 ; Set the focus to tab control ???
while (curWinNum < numOfChrome and found = "false") { 
    WinGetTitle, firstTabTitle, A ; The initial tab title
    title := firstTabTitle
    Loop {
        if(InStr(title, tabSearch)>0){
            found := "true"
            break
        }
        Send {Ctrl down}{Tab}{Ctrl up}
        Sleep, 50
        WinGetTitle, title, A  ;get active window title
        if(title = firstTabTitle){
            break
        }
    }
    WinActivateBottom, %chrome%
    curWinNum := curWinNum + 1
}

if(found = "false"){
    Run "https://gmail.com"
}    
return

关于python - 谷歌浏览器 - 如何激活谷歌浏览器并切换到特定的标签窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46406504/

相关文章:

python - docopt - 每个参数需要特定的数据类型

c++ - 使用 dev C++ 将文本输入框添加到 C++ gui

javascript - 什么时候应该使用 WebHID 而不是 WebUSB?

python - 保存为 pdf 时保留原始图像数据

python - 如何在装饰器存在的情况下找到从 url 解析的 View

windows - 运行批处理文件,在卸载时使用 Wix 安排任务

css - Chrome 中的宋体

google-chrome - 颜色选择器 - Chrome 开发者工具的建议

python - 赋值中的类型不兼容(表达式的类型为 "List[<nothing>]",变量的类型为 (...)

c++ - 在另一个窗口中捕获窗口输出