使用AHK脚本访问运行时生成的HTML

时间:2014-08-09 21:23:29

标签: csv automation autohotkey

如何访问AHK上运行时生成的HTML?

在邮政编码粘贴到搜索框后(我在此网站上发布了http://www.postcodeanywhere.co.uk/demos/address-finder/

,我正在尝试访问下拉框的内容

enter image description here

对于2个条目,网页代码如下:

 <div class="pca pcalist">
    <div class="pcaitem pcafirstitem pcaselected" title=
    "MK10 0AA, A C R Logistics, Standing Way, Brinklow, Milton Keynes">
        MK10 0AA, A C R Logistics, Standing Way, Brinklow, Milton Keynes
    </div>

    <div class="pcaitem pcalastitem" title=
    "MK10 0AA, Waitrose, A C R Logistics, Standing Way, Brinklow, Milton Keynes">
    MK10 0AA, Waitrose, A C R Logistics, Standing Way, Brinklow, Milton
    Keynes
    </div>
</div>

对于2个以上的条目,网页代码如下:

<div class="pca pcalist">
    <div class="pcaitem pcafirstitem pcaselected" title=
    "MK13 7NA, 1, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 1, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 2, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 2, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 3, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 3, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 4, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 4, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 5, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 5, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 6, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 6, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 7, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 7, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 8, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 8, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 9, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 9, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 10, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 10, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 11, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 11, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 12, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 12, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 13, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 13, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 14, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 14, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 15, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 15, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem" title=
    "MK13 7NA, 16, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 16, Bleasdale, Heelands, Milton Keynes
    </div>


    <div class="pcaitem pcalastitem" title=
    "MK13 7NA, 17, Bleasdale, Heelands, Milton Keynes">
        MK13 7NA, 17, Bleasdale, Heelands, Milton Keynes
    </div>
</div>

这是我的代码到目前为止 - 它只返回运行时的国家/地区列表而不是实际地址,我怀疑它指向文档中的另一个pcaitem类...

;// Fileread, test, somefilename
fileread, AddressList, mk-data-000001-1.csv


IE := ComObjCreate("InternetExplorer.Application")
IE.Navigate("http://www.postcodeanywhere.co.uk/demos/address-finder/")
ComObjError(false)
IE.Visible := true


loop 14
{
Send {Tab down}{Tab up}
Sleep 200
}


loop, parse, AddressList, `n, `r
{
    Clipboard = %A_LoopField%
    Send, ^v


Name_Elements := IE.document.getElementsByClassName("pcaitem")
loop, % Name_Elements.length
{
Addr_text := Name_Elements[A_Index-1].innertext
} 
}

mk-data-000001-1.csv的样本内容

MK1 1AS
MK1 1AX
MK1 1BA
MK1 1BB
MK1 1BE
MK1 1BG
MK1 1BH
MK1 1BJ
MK1 1BL
MK1 1BN
MK1 1BP
MK1 1BQ
MK1 1BU
MK1 1BW
MK1 1BX

1 个答案:

答案 0 :(得分:2)

我相信你的假设是正确的。 我认为更精确一点并不痛苦?
我使用了以下内容:

IE.document.getElementsByClassName("pcalist")[0].getElementsByClassName("pcaitem")

这是一个测试示例

SetTitleMatchMode,2

IE := ComObjCreate("InternetExplorer.Application")
IE.Navigate("http://www.postcodeanywhere.co.uk/demos/address-finder/")
ComObjError(false)
IE.Visible := true
MsgBox Press OK when the page has been fully loaded.
Sleep 200
WinMaximize, Internet Explorer
Sleep 1000

;click the search input field
send {PGDN}
Sleep 200
x:=A_ScreenWidth//2
Click, %x% 250
Sleep 200

;search text
Send, MK1 1AS
Sleep 200

Data:=""
ResultsList := IE.document.getElementsByClassName("pcalist")[0].getElementsByClassName("pcaitem")
Loop % ResultsList.Length
    Data .= ResultsList[A_Index-1].innerHTML
StringReplace,Data,Data,</span>,`n,All
Data:=RegExReplace(Data,"<span[^<>]+>",", ")
MsgBox % Data

截图 image