gpt4 book ai didi

javascript - 使用 VBA 搜索网站

转载 作者:行者123 更新时间:2023-11-28 13:00:57 24 4
gpt4 key购买 nike

我想做的是搜索website使用VBA,在左侧框中输入一些单词,然后在右侧得到结果。

问题是我不懂 HTML,也不知道如何引用这个框。我使用 GetElementByID 但收到错误:

objIE.Document.GetElementByID("text-translation-video-ad").Value = "piłka".   
"Object doesn't support this property or method".

这是我的代码:

Sub www()

Set objIE = CreateObject("InternetExplorer.Application")

objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.AddressBar = 0
objIE.StatusBar = 0
objIE.Toolbar = 0
objIE.Visible = True
objIE.Navigate ("https://pl.pons.com/tłumaczenie-tekstu")

Do
DoEvents
Loop Until objIE.ReadyState = 4

pagesource = objIE.Document.Body.Outerhtml
objIE.Document.GetElementByID("text-translation-video-ad").Value = "piłka"
objIE.Document.GetElementByID("qKeyboardInputInitiator").Click

Do
DoEvents
Loop Until objIE.ReadyState = 4

End Sub

最佳答案

在不更改任何语言设置的情况下,以下内容翻译为“Hello”

代码:

Option Explicit

Public Sub GetInfo()
Dim IE As New InternetExplorer, html As HTMLDocument, translation As String
Const TRANSLATION_STRING As String = "Hello"

With IE
.Visible = True
.navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu"

While .Busy Or .readyState < 4: DoEvents: Wend

Set html = .document

With html
.querySelector("textarea.text-translation-source.source").Value = TRANSLATION_STRING
.querySelector("button.btn.btn-primary.submit").Click
Application.Wait Now + TimeSerial(0, 0, 3)
translation = .querySelector("div.translated_text").innerText
End With

Debug.Print translation
'Quit '<== Remember to quit application
End With

End Sub

查看:

Output

在立即窗口中打印:

Output

<小时/>

编辑:

后期绑定(bind)版本

Option Explicit

Public Sub GetInfo()
Dim IE As Object, html As Object

With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu"

While .Busy Or .readyState < 4: DoEvents: Wend

Set html = CreateObject("htmlfile")
Set html = .document

With html

.getElementsByClassName("text-translation-source source")(0).innerText = "Translate"
.getElementsByClassName("btn btn-primary submit")(0).Click
Application.Wait Now + TimeSerial(0, 0, 2)

Dim i As Long
For i = 0 To .getElementsByClassName("text-translation-target target").Length - 1
Debug.Print .getElementsByClassName("text-translation-target target")(i).innerText
Next i

Stop
End With
.Quit
End With

End Sub

关于javascript - 使用 VBA 搜索网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50598496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com