Excel vba翻译IE.Document为空

时间:2019-02-13 09:58:28

标签: excel vba web-scraping

这是我用来翻译excell工作表中的字段的VBA脚本。 问题是该脚本对我有用,大约在2 3个月前,但是现在IE.Document在翻译后为空。 该页面提供了正确的翻译,但是我无法在Excel工作表中得到结果

inputstring = "en"
outputstring = "da"
text_to_convert = str

'open website

IE.Visible = True
IE.navigate "https://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert

Do Until IE.ReadyState = 4
    DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:4"))

Do Until IE.ReadyState = 4
    DoEvents
Loop
test = IE.Document.getElementById("result_box")

1 个答案:

答案 0 :(得分:0)

使用适当的页面等待。我还使用了其他元素选择器

Option Explicit
Public Sub GetTranslation()
    Dim ie As New InternetExplorer, ws As Worksheet
    Dim inputString As String, outputString As String, text_to_convert As String, translation As String
    inputString = "en"
    outputString = "da"
    text_to_convert = "Banana"
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With ie
        .Visible = True
        .Navigate2 "https://translate.google.com/#" & inputString & "/" & outputString & "/" & text_to_convert
        While .Busy Or .readyState < 4: DoEvents: Wend
        translation = ie.document.querySelector(".translation").innerText
        ws.Cells(1, 1) = translation
        .Quit
    End With
End Sub