首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Excel中使用Selenium应用SendKeys Keys.Enter (或Keys.Return)?

如何在Excel中使用Selenium应用SendKeys Keys.Enter (或Keys.Return)?
EN

Stack Overflow用户
提问于 2020-06-23 09:35:26
回答 1查看 760关注 0票数 0

我正在整理一个词汇表和一个宏,以便从vocabulary.com中提取信息。搜索没有按钮,所以我必须使用Enter键,但Keys.Enter不起作用。

宏仍然有效,因为您不必按enter键即可显示站点的定义页面,当您在搜索字段中键入内容时,将弹出最顶部的自动完成结果的定义页面。

问题是,并不是在每种情况下都是最推荐的结果,这就是我正在寻找的单词。我需要让回车击键工作,这个宏是100%有用的。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-24 12:02:01

我没能让Enter键起作用,但以防其他人想从Vocabularly.com中提取定义,这里是起作用的。我最终只是点击了我正在寻找的特定单词的按钮。工作得很完美。

代码语言:javascript
复制
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

结束子对象

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62525980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档