excel - 如何在 Excel 中使用 Selenium 应用 SendKeys Keys.Enter(或 Keys.Return)?
问题描述
我正在整理一个词汇表并整理一个宏来从词汇表中提取信息。搜索没有按钮,所以我必须使用回车键,但Keys.Enter
不起作用。
该宏仍然有效,因为您不必按网站的 Enter 键来显示定义页面,以显示您在搜索字段中键入时弹出的最顶部的自动完成结果。
问题是,并非在每种情况下,我正在寻找的词都是最重要的建议结果。我需要让 Enter 击键才能使这个宏 100% 有用。
Sub VocabularyWebScraper()
'Application.ScreenUpdating = False
Dim Driver As New Selenium.ChromeDriver
Dim Keys As Selenium.Keys
Dim count As Long
Sheets("Vocabulary.com Scraping").Activate
Set Driver = CreateObject("Selenium.ChromeDriver")
Set Keys = CreateObject("Selenium.Keys")
count = 1
While (Len(Range("A" & count)) > 0)
Driver.Get "https://www.vocabulary.com/dictionary/"
Driver.FindElementById("search").SendKeys Range("A" & count) + Keys.Enter
Driver.Wait 1000
On Error Resume Next
Range("B" & count) = Driver.FindElementByClass("short").Text
Range("C" & count) = Driver.FindElementByClass("long").Text
count = count + 1
Wend
Driver.Quit
'Application.ScreenUpdating = True
End Sub
解决方案
我无法使用 enter 键,但以防万一其他人想从 Vocabularly.com 提取定义,这里是有效的。我最终只是单击了我正在寻找的特定单词的按钮。工作完美。
Sub VocabularyWebScraper()
Application.ScreenUpdating = False
Dim Driver As New Selenium.ChromeDriver
Dim count As Long
Dim word As String
Sheets("Vocab Web Scraping").Activate
Set Driver = CreateObject("Selenium.ChromeDriver")
count = 1
While (Len(Range("A" & count)) > 0)
word = Range("A" & count)
Driver.Get "https://www.vocabulary.com/dictionary/"
Driver.FindElementById("search").SendKeys word
Driver.Wait 1000
Driver.FindElementByCss("li[word='" + word + "']").Click
Driver.Wait 2000
On Error Resume Next
Range("B" & count) = Driver.FindElementByClass("short").Text
Range("C" & count) = Driver.FindElementByClass("long").Text
count = count + 1
Wend
Driver.Quit
Application.ScreenUpdating = True
结束子
推荐阅读
- selenium - 在 for 循环中添加 WebDriverWait 的问题 - selenium
- bash - 在 bash 中理解 bc
- linux - 在函数调用中注册的更改
- python - 从 AWS Lambda Python 访问 GCP 计算定价 API
- python - 如何在 python 中使用 url
- node.js - 如何从后端的端点返回 json 数据
- java - 时间片、上下文切换和线程干扰之间的区别
- powershell - 如何找到哪个嵌套循环是优秀的
- python - 使用 Selenium 和 Beautifulsoup 进行 Python 抓取无法提取嵌套标签,错误对象不可调用
- xpsdocument - 如何通过修改 .gpd 文件将 pasersize 添加到 Microsoft XPS 文档编写器