嗨,我是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">[email protected]</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>
这是错误代码
我不确定这个的点击部分,但你需要从类名中删除空格以消除该错误
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并选择其他选项。
您实际上没有单击下拉列表然后选择值。您只需将下拉字段设置为您想要的值即可。
这是我的小代码片段,它在我需要导航的网站上执行此操作。
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