【问题标题】:Unable to select from dropdown in selenium using vba无法使用 vba 从 selenium 的下拉列表中进行选择
【发布时间】:2019-02-13 03:14:47
【问题描述】:

您好,我是通过 VBA 接触 selenium 的新手,因此我试图通过登录网页导航并将数据提取到 excel 文件中来自动化流程。 根据我的研究,首先我通过单击调用下拉菜单,然后选择该选项。

下面是我的代码

Sub Praise()
Dim bot As New WebDriver
bot.Start "chrome"
bot.Get "https://dashboard.stripe.com/login"
bot.FindElementById("email").SendKeys ActiveCell.Value
bot.FindElementById("password").SendKeys ActiveCell.Offset(0, 1).Value
bot.FindElementByTag("form").submit
Application.Wait (Now + TimeValue("0:00:8"))
Set myelement = bot.FindElementByClass("bs-Link", Raise:=False)
If myelement Is Nothing Then
Else
bot.FindElementByClass("bs-Link").Click
End If
bot.FindElementByClass("db-AccountSwitcher-chevron").Click ' to call dropdown
bot.FindElementByClass("Text-color--white Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline").Click ' Error occurs in this line

End Sub

这是调用下拉菜单的代码

<div class="db-AccountSwitcher-button Box-root Flex-flex Flex-alignItems--center">
  <div class="Box-root Margin-right--8">
    <div class="db-AccountSwitcher-activeImage">
      <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
        <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
      </div>
    </div>
  </div>
  <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--dark Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--noWrap Text-display--inline">Masorini.com</span></div>
  <div class="Box-root Margin-left--8">
    <div class="db-AccountSwitcher-chevron"></div>
  </div>
</div>

这是下拉列表的代码

<div class="ScrollableMenu SearchableList-items" id="searchable-list-results-47" role="listbox" style="max-height: 200px;">
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Masorini.com</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Ariavoss</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Bikinimas</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Diva Noche</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="true" role="option">
    <div>
      <div class="Box-root Box-background--blue Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: rgb(255, 255, 255);"><span class="Text-color--white Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Bootyswim</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Zainno</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Actiwears</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Rockactive</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Cat Lovers</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Chicnuit</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Bootyswim</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Diva Noche</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Bikinimas</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">EpicDevil.co</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">probago.com</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Activepose</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">SiaRose.com</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Sunvoss</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Mosselli</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Aroabikini</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">Swimlemon</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">milewatches</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--fallback"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">stripe-northwrist@massiveshops.com</span></div>
      </div>
    </div>
  </div>
  <div aria-selected="false" role="option">
    <div>
      <div class="Box-root Box-background--white Padding-horizontal--8 Padding-vertical--4 Flex-flex Flex-alignItems--center" style="cursor: pointer; user-select: none;">
        <div class="Box-root Margin-right--8">
          <div class="db-AccountSwitcher-accountImage Box-root Box-background--white">
            <div class="db-AccountSwitcher-accountImage--content db-AccountSwitcher-accountImage--newAccount"></div>
          </div>
        </div>
        <div class="TruncatedText" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><span class="Text-color--default Text-fontSize--16 Text-fontWeight--medium Text-lineHeight--24 Text-typeface--base Text-wrap--wrap Text-display--inline">New account</span></div>
      </div>
    </div>
  </div>
</div>

这是错误代码

【问题讨论】:

  • 您的代码是否可以点击下拉菜单?您想从下拉列表中选择什么?
  • 是的,它点击并且列表在屏幕上可见,但是当我使用相同的方法点击列表中的选项时,它会引发错误!我添加了错误代码 sn-p !!
  • 你需要从类名中删除空格并用点替换它们。
  • 顺便说一下你想选择什么选项?
  • 实际上这是一个可重复的任务,所以我需要选择所有选项,但一次一个,基本上这些是客户不同的帐户。所以当我选择正确的帐户时,只有我才能找到相关信息。

标签: vba excel selenium-webdriver web-scraping


【解决方案1】:

我不确定这个点击部分,但您需要从类名中删除空格以消除该错误

bot.FindElementByCss(".Text-color--white.Text-fontSize--16.Text-fontWeight--medium.Text-lineHeight--24.Text-typeface--base.Text-wrap--wrap.Text-display--inline").Click 

目标Bootyswim

如果点击有效,那么您应该也可以使用:

bot.FindElementByXPath("//*[@role='option'][5]").Click

然后您将[5] 更改为您的其他选项。

尝试 xpath here 并选择其他选项。

【讨论】:

  • 嗨 QHarr,下面的代码就像一个魅力,bot.FindElementByXPath("//*[@role='option'][5]").Click。万岁!!!非常感谢!!
  • 不用担心。很高兴它有帮助。
  • 您好 QHarr,很抱歉打扰您,我再次停下来,因为我无法在搜索框中输入文本!这是代码 -

  • 你能在一个新问题中发帖以便清楚地看到 html 和问题吗?把链接放在这里,我会看看。注意:我没有看到输入标签。
【解决方案2】:

您实际上并没有点击下拉菜单然后选择值。您只需将下拉字段设置为您想要的值。

这是我的小代码 sn-p,它在我需要导航的网站上执行此操作。

Dim element As Selenium.WebElement
Set element = WaitForElement(byName, "Your element ID here")
element.AsSelect.SelectByText "The exact text that's in the drop down"

第一行调用下面的函数——我在等待网站完成加载时遇到了超时问题,所以我写了这个函数,以便能够智能地等待页面加载完毕,然后放弃找不到元素。

第二行明确地将下拉菜单设置为我正在寻找的值 - 没有点击元素并进行选择。

Private Function WaitForElement(ByVal Method As FindElementBy, ByVal elementID As String) As Selenium.WebElement

  Dim startTimer As Single
  startTimer = Timer

  Dim waitTime As Single
  waitTime = this.Driver.Timeouts.ImplicitWait / 100

  Dim webBit As Selenium.WebElement
  On Error Resume Next    'it is possible that we'll go looking for the element before it's been returned to view, that's fine, we're waiting for it
  While webBit Is Nothing And Timer < (startTimer + waitTime)
    Select Case Method
      Case byClass
        Set webBit = this.Driver.FindElementByClass(elementID)
      Case byCSS
        Set webBit = this.Driver.FindElementByCss(elementID)
      Case byID
        Set webBit = this.Driver.FindElementById(elementID)
      Case byLinkText
        Set webBit = this.Driver.FindElementByLinkText(elementID)
      Case byName
        Set webBit = this.Driver.FindElementByName(elementID)
      Case byPartialLinkText
        Set webBit = this.Driver.FindElementByPartialLinkText(elementID)
      Case byTag
        Set webBit = this.Driver.FindElementByTag(elementID)
      Case byXPath
        Set webBit = this.Driver.FindElementByXPath(elementID)
    End Select
  Wend
  On Error GoTo 0         'resume normal error handling

  Set WaitForElement = webBit

End Function

【讨论】:

  • 嗨弗里曼,感谢您的帮助,但是由于我是新手,我无法理解,我的网页代码没有 ID 元素,而是只有 ClassName 元素。那么我该如何调整你的代码以适应我的情况。
  • "Your element ID here" 替换为"Your element name here"。此外,您不必使用WaitForElement 功能,您可以直接转到您命名的bot.FindElementByName
  • 相信在您的情况下,"Your element name here" 将是 "Bootyswim" - 这似乎是当前选择的组合框中的文本。
  • 这很有趣,如果你不介意的话,我可能会开玩笑。我不确定它是否会在这种情况下工作,因为 AsSelect 不需要选择标签吗?
  • @QHarr 你可以随意拥有它。经过几周的混乱,这对我有用。它的发布带有隐含的“不保证适用于目的。使用风险自负。”
猜你喜欢
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多