【问题标题】:Open new tabs with same URL, with different commands in each tab打开具有相同 URL 的新选项卡,每个选项卡中使用不同的命令
【发布时间】:2019-10-05 23:57:28
【问题描述】:

我需要访问一个网站,然后选择一个原因并点击一个按钮。因为我有多个变量(原因),所以我需要多次执行此操作,以便为每个变量创建报告。因为网页是表单,所以网页的url和title是一样的。

到目前为止,我已经创建了这段代码:

WB :=  ComObjCreate("InternetExplorer.Application")
WB.Visible:=True

wb.Navigate("http://url")
wb.document.title := "cause 1"

WinMaximize, % "ahk_id " wb.HWND
; Wait for page to load:
While wb.Busy or wb.ReadyState != 4
    Sleep, 100

wb.document.getElementsByName("id_cause")[0].value := 1
wb.document.getElementsByName("btnSearch")[0].Click()

wb.Navigate("http://url", 2048)
wb.document.title := "cause 2"


While wb.Busy or wb.ReadyState != 4
    Sleep, 100

wb.document.getElementsByName("id_cause")[0].value := 2
wb.document.getElementsByName("btnSearch")[0].Click()
return

正如我所料,所有命令都指向第一个选项卡。因此,第一个选项卡被重命名为原因 1,之后再次重命名原因 2。 在网上搜索解决方案,我看到了一些允许用户引用选项卡的代码,但它们都使用 URL 来区分选项卡。

有没有办法在这种情况下实现我的目的(相同的 URL)?我正在考虑诸如标签索引或重命名 URL 并使用命名进行标识之类的东西。

我在网上找到的代码之一是这个:

navOpenNewForegroundTab = 65536
navOpenInBackgroundTab = 4096
navOpenInNewWindow = 1
navOpenInNewTab = 2048

wb := ComObjCreate("InternetExplorer.Application") ; create a IE instance
IeHwnd := wb.Hwnd
wb.Visible := True
wb.Navigate("Google.com")
While wb.readyState != 4 || wb.document.readyState != "complete" || wb.busy ; wait for the page to load
   Sleep, 10


wb.Navigate("DuckDuckgo.com", navOpenInNewTab)
while !wb2
    wb2 := New_IE("DuckDuckgo.com")
wb2.Navigate("Google.com")
While wb2.readyState != 4 || wb2.document.readyState != "complete" || wb2.busy ; wait for the page to load
   Sleep, 10


wb.Navigate("DuckDuckgo.com", navOpenInNewTab)
while !wb3
    wb3 := New_IE("DuckDuckgo.com")
wb3.Navigate("Google.com")

While wb3.readyState != 4 || wb3.document.readyState != "complete" || wb3.busy ; wait for the page to load
   Sleep, 10

Msgbox then just use wb or wb2 or wb3 to control tab1, tab2 or tab3`n`nFor Example lets look up something in tab 2 now...

Something_to_look_for := "duckduckgo"
wb2.Navigate("Google.com/#q=" Something_to_look_for)
While wb2.readyState != 4 || wb2.document.readyState != "complete" || wb2.busy ; wait for the page to load
   Sleep, 10
return


New_IE(Url) {
    For wb in ComObjCreate("Shell.Application").Windows
    {
        If instr( wb.LocationUrl, URL) && InStr( wb.FullName, "iexplore.exe" )
            return wb
        else
            continue
    }
}

【问题讨论】:

  • 那你在网上找到的代码呢?它有效吗?它有什么问题?
  • 它基于 URL,但在我的情况下,所有选项卡中的 URL 都相同,因此它不起作用

标签: html dom autohotkey


【解决方案1】:

IEGetN() 是一个函数,它将为您提供具有指定标题的第 n 个 IE 窗口。窗口必须已经打开。

你可以这样使用它:

 wb1 := IEgetN("Google",1)
 wb5 := IEgetN("Google",5)

函数如下:

IEGetN(name="",_n=1) 
{
    _counter = 1
    IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
    Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
    for wb in ComObjCreate("Shell.Application").Windows()
        if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
        {
            if (_counter ==_n)
            {
                return wb 
            }
            _counter += 1
        }
}

【讨论】:

  • 我已尝试使用 google.com,但无法使用它。我写的代码在这里:pastebin.com/mu85j263。如您所见,它应该更改了选项卡 nr。 2 网页名称,但它没有。感谢您迄今为止的支持!
  • 给页面一些时间“呼吸”;在wb2 := IEgetN("Google",2)之前添加sleep, 2000
  • 实际上,我的测试失败了,因为代码会检查所有打开的 Internet Explorer 窗口中的选项卡,而不是活动窗口。有没有办法将名称检查限制在当前窗口?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2018-10-30
  • 2019-10-06
相关资源
最近更新 更多