繁体   English   中英

Web scraping DEEPL.com using VBA Excel and Selenium

[英]Web scraping DEEPL.com using VBA Excel and Selenium

i'm trying to code a function to translate sentences in Excel using DEEPL.com My approach is using Selenium to scrape the web using Chrome (as IExplore is not supported by the web).

Public Function deepL(txt As String, inputLang As String, outputLang As String)
Dim url As String
Dim driver As New WebDriver

url = "https://www.deepl.com/translator#" & inputLang & "/" & outputLang & "/" & txt
driver.Start "Chrome"
driver.Timeouts.ImplicitWait = 5000
driver.Get url

deepL = driver.FindElementById("target-dummydiv").Text
driver.Close

End Function
----
Sub translating()
'test for word "probando" from "es" to "en"
'url: https://www.deepl.com/translator#es/en/probando
'it should return: "testing"
MsgBox (deepL("probando", "es", "en"))
End Sub

加载 web 时出现问题,因此加载时包含翻译的 div 为空,GET 指令返回空文本。 但 1 秒后,页面刷新并显示正确结果:

<div id="target-dummydiv" aria-hidden="true" class="lmt__textarea lmt__textarea_dummydiv" lang="en-US">testing</div>

我尝试添加 5 秒的隐式等待,以便给网页加载时间,但结果是一样的。

我究竟做错了什么?

编辑:我发现带有翻译的 div 具有可见性:隐藏。 如果我显示可见性,结果是正确的,但不知道如何在我的代码中得到它

好的,我找到了解决方案:

只需 select 翻译所在的 textarea 并使用.attribute("value") 而不是.text 获取翻译

deepL = driver.FindElementByCss("textarea.lmt__textarea.lmt__target_textarea.lmt__textarea_base_style").Attribute("value")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM