通过Excel VBA按下“应用”按钮时未检测到网页搜索过滤器输入

时间:2018-07-24 20:13:49

标签: html excel vba excel-vba internet-explorer-11

我正在尝试编写一个VBA程序,该程序将通过LinkedIn查找在特定公司中担任特定职位的人。我已经想出了如何打开LinkedIn搜索窗口,打开所有过滤器以及输入所需公司名称和职位的方法,但是 一旦我点击“应用”按钮以应用这些过滤器,它们就是无法识别为过滤器 。它单击按钮,就好像我根本没有在过滤器框中输入任何内容一样。

我发现问题不是我单击“应用”按钮的方法,而是缺少输入识别。 只有在添加字符串之前或之后单击相应的过滤区域/框,我的输入字符串才会被识别为过滤器。

话虽如此,我知道我正在寻找的解决方案是输入字符串,然后单击同一过滤器框,然后单击“应用过滤器”按钮。 < / p>

下面是我的代码,用于将我的特定过滤器输入到LinkedIn,然后单击“应用”按钮。

Sub Fill()

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim AllFiltersButton As MSHTML.IHTMLElement
Dim ApplyButton As MSHTML.IHTMLElement2
Dim ApplyButtons As MSHTML.IHTMLElementCollection2

Dim Company As String, Position As String

Company = Range("A2").Value
Position = ("""" + Range("B1").Value + """")

IE.Visible = True
IE.navigate "https://www.linkedin.com/search/results/people/?facetGeoRegion=%5B%22us%3A70%22%5D&origin=FACETED_SEARCH"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("button")

For Each AllFiltersButton In HTMLButtons
    If AllFiltersButton.className = "search-filters-bar__all-filters button-tertiary-medium-muted flex-shrink-zero mr3" Then AllFiltersButton.Click
Next AllFiltersButton

Set HTMLInput = HTMLDoc.getElementById("search-advanced-company")
HTMLInput.Value = Company

Set HTMLInput = HTMLDoc.getElementById("search-advanced-title")
HTMLInput.Value = Position

Set ApplyButtons = HTMLDoc.getElementsByTagName("button")

For Each ApplyButton In ApplyButtons
    If ApplyButton.className = "search-advanced-facets__button--apply button-primary-large" Then ApplyButton.Click
Next ApplyButton

我已选择所有必需的引用(Microsoft HTML对象库,Microsoft Internet控件,Microsoft Office 15.0对象库等)。 代码的这一部分似乎可以正常工作。

花了数小时试图找出一种在这些搜索/过滤器框中单击的方法之后,我偶然发现了一种应该可以使用的方法,但是我似乎无法针对我的具体情况修改代码。

Set evt = ie.Document.createEvent("keyboardevent")
evt.initEvent "change", True, False
PW.all(0).dispatchEvent evt

我尝试采用这种方法以点击进入公司搜索框(但不起作用),如下所示:

Dim vSelect As HTMLSelectElement
Dim eventClick As Object

Set vSelect = HTMLDoc.getElementById("search-advanced-company")
Set eventClick = HTMLDoc.createEvent("click")
eventClick.initEvent "change", True, False
vSelect.dispatchEvent eventClick

我应该如何修改此代码以单击进入搜索/过滤器框?

如果有人可以以任何方式帮助我,将不胜感激。

谢谢。

编辑:关于是否允许在LinkedIn上抓取数据,我收到的信息不一。为了澄清起见,我不会会将此代码用于数据抓取,以避免任何侵权问题。

1 个答案:

答案 0 :(得分:0)

在使用了经常被避免和不尊重的SendKeys函数之后,我终于能够使它起作用。

下面是更正的代码:

Sub Fill()

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLInputs As MSHTML.IHTMLElementCollection2
Dim AllFiltersButton As MSHTML.IHTMLElement
Dim ApplyButton As MSHTML.IHTMLElement
Dim ApplyButtons As MSHTML.IHTMLElementCollection3
Dim Company As String, Position As String

Company = Range("A2").Value
Position = ("""" + Range("B1").Value + """")

IE.Visible = True
IE.navigate "https://www.linkedin.com/search/results/people/?facetGeoRegion=%5B%22us%3A70%22%5D&origin=FACETED_SEARCH"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("button")

For Each AllFiltersButton In HTMLButtons
    If AllFiltersButton.className = "search-filters-bar__all-filters button-tertiary-medium-muted flex-shrink-zero mr3" Then AllFiltersButton.Click
Next AllFiltersButton

Set HTMLInputs = HTMLDoc.getElementsByTagName("input")
HTMLInputs(2).Value = Position
HTMLInputs(2).Focus
Application.SendKeys "{LEFT}", True

HTMLInputs(3).Value = Company
HTMLInputs(3).Focus
Application.SendKeys "{LEFT}", True

Set ApplyButtons = HTMLDoc.getElementsByTagName("button")

For Each ApplyButton In ApplyButtons
    If ApplyButton.className = "search-advanced-facets__button--apply button-primary-large" Then ApplyButton.Click
Next ApplyButton

End Sub

大声“谢谢!”向@ashleedawg提出导致解决方案的建议!