簡體   English   中英

無法使用VBA從硒的下拉列表中進行選擇

[英]Unable to select from dropdown in selenium using vba

嗨,我是通過VBA接觸硒的新手,所以我試圖通過登錄網頁導航並將數據提取到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> 

這是錯誤代碼

在此處輸入圖片說明

我不確定為此單擊部分,但您需要從類名稱中刪除空格以擺脫該錯誤

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並選擇其他選項。

您實際上並沒有單擊下拉菜單然后選擇值。 您只需將下拉字段設置為所需的值即可。

這是我的小代碼段,該段代碼在我需要瀏覽的網站上執行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM