2017-02-07 25 views
0

短版:定位網頁元素具有相同名稱

我需要一種方法來使用Excel的VBA腳本來用在具有各自具有相同的類名很多這樣的小部件在頁面上下拉列表控件進行交互。

龍版本:

我試圖寫一個VBA腳本,將在以下網站提供的文檔鏈接:

https://support.industry.siemens.com/cs/products/6es7516-3an01-0ab0/cpu-1516-3-pn-dp-1mb-prog-5mb-data?pid=578298&mlfb=6ES7516-3AN01-0AB0&mfn=ps&lc=en-WW

上面的鏈接是一個單一的產品。 很顯然,我希望腳本能夠獲取任何產品的文檔。

我遇到的問題是我需要與嵌套在文檔中的下拉列表進行交互。 我可以創建一個循環,定位在頁面列表:

'Dim NamePeek 
Set ie = New InternetExplorer 
ie.Visible = 1 
ie.navigate "https://support.industry.siemens.com/cs/products/6es7516-3an01-0ab0/cpu-1516-3-pn-dp-1mb-prog-5mb-data?pid=578298&mlfb=6ES7516-3AN01-0AB0&mfn=ps&lc=en-WW" 
Set Divs = ie.document.getElementsByTagName("div") 
For Each Div In Divs 
    'NamePeek = Div.className 
    If Div.className = "productfilter mfn" Then 
     Trigger01 = True 
     Trigger02 = False 
     Trigger03 = False 
    End If 
    If Div.className = "filtertitle" Then 
     Trigger02 = True 
     Trigger03 = False 
    End If 
    If Div.className = "dropdown" Then 
     Trigger03 = True 
    End If 
    If Trigger01 And Trigger02 And Trigger03 Then 
     'This is where the list I'd like to interact with is located. 
     'Div.selectedIndex = 2 
     Trigger01 = False 
     Trigger02 = False 
     Trigger03 = False 
    End If 
Next Div 

我知道「Div.selectedIndex = 2將無法工作,因爲沒有可用於該網站數據獲取HTMLDivElement對象.selectedIndex方法加載到Excel中。

我試圖使一個簡單的測試網頁只是一個單一的下拉菜單,然後用下面的方法成功與它交互:

Dim target As MSHTML.IHTMLSelectElement 

Set ie = New InternetExplorer 
ie.Visible = 1 

ie.navigate "file:///C:/xxxxxx/dropdowntestwebsite.html" 

Set target = ie.document.getElementById("DDList") 
target.selectedIndex = 3 

所以我想知道如果我可以使用IHTMLSelectElement打倒下降互動因爲沒有ID,IHTMLSelectElement對象不支持.getElement(s)ByTagName方法。

回答

0

西門子網站看起來像使用某種jQuery插件來控制其元素。小部件是插件嗎?選擇框沒有硬編碼的,所以你不能像一個簡單的選擇框控制他們,說喜歡

ie.getelementbyid("selectbox_div").selectedindex = 2. 

你必須要找到的JavaScript命令下降,填充通過VBA命令下拉菜單,然後問題,像

Call IE.document.parentWindow.execScript("nameofFunctionHere()", "JavaScript") 

我在與使用下拉框插件「選擇二」網站類似的問題。

+0

好吧,我會尋找一個JavaScript函數。 – John

+0

您的代碼內的鏈接正確選擇了您想要的產品代碼。我在西門子網站上更改了其他幾個選擇框,然後複製新的網址。當我將它粘貼到一個新標籤頁時,它會顯示我的新更改頁面。也許你可以使用url來選擇需要選擇的內容。嘗試選擇單個項目所需的所有內容,然後複製該網址。你應該能夠找出哪個部分的網址控制了選擇框。找出網址代碼並將您的選擇插入到這可能會更容易。 –

+0

我也注意到了。然而,有一部分url幾乎不可能通過程序確定:.../cpu-1516-3-pn-dp-1mb-prog-5mb-data?pid = 578298 ...我絕對可以使用url選擇某些盒子,但這種方法的問題是試圖選擇一個盒子產品。我不知道這個純英文描述會提前。 – John

0

我剛剛花了幾個星期的時間試圖修復一個使用select2 dropin的網頁,所以我知道你正在經歷什麼。我不知道MSHTML不能使用getelementbyTagname。也許你應該嘗試設置它,否則。這裏是我用來控制打開網頁的代碼。當然,該頁面必須已經在MSIE中打開,並且您必須至少知道文檔標題的一部分。

以下代碼將爲您設置它,然後您可以使用getElementByTagName。有時候,你可以得到你想要的parentsID.firschild之一的父元素....

Sub getSiemensBrowserAlreadyOpen() 

    Set objShell = CreateObject("Shell.Application") 
    Set objAllWindows = objShell.Windows 

    Boolean_indicator = False 

    Do While Not Boolean_indicator 
    For Each ow In objAllWindows 
     If (InStr(1, ow, "Internet Explorer", vbTextCompare)) Then 

      Set objIterator = CreateObject("Shell.Application") 
      For x = 0 To objIterator.Windows.Count 
       On Error Resume Next 
       current_title = objIterator.Windows(x).Document.Title 
       current_url = objIterator.Windows(x).Document.Location 

       If InStr(1, current_title, "Industry Support Siemens") > 0 Then 'is this my webpage? 
        Set IE = objIterator.Windows(x) 
        MsgBox "IE was properly set" 
        GoTo startProcess 
        Boolean_indicator = True 
        Exit For 
       End If 
      Next x 
     End If 
     Set objIterator = Nothing 
    Next ow 
    Boolean_indicator = True 
    Loop 
    MsgBox "Webpage was not found." 
    Exit Sub 
startProcess: 
    Set myDoc = IE.Document 

'**************************************** 
' Your operative code here 






'****************************************/ 
    Set objShell = Nothing 
    Set myDoc = Nothing 
    Set IE = Nothing 
End Sub 
相關問題