【问题标题】:Unable to select check box from dropdown in webpage using Selenium with VBA无法从使用 Selenium 和 VBA 的网页下拉列表中选择复选框
【发布时间】:2019-01-29 22:17:55
【问题描述】:

您好,我是通过 VBA 接触 selenium 的新手,我正在尝试选择下拉列表,我需要从下拉列表中选择一个选项,即复选框。很遗憾,我无法分享链接。

我尝试了一些代码但它不起作用,下面是我尝试的代码

bot.FindElementByXPath("//*[contains(text(),'GT - ALL')]").Click

这是网页元素

<div class="form-group">
<label for="billerId"> Mid </label><br>
<select name="billerId" id="billerId" class="billerId form-control" multiple="multiple" style="display: none;">
<option value="7">(7) Pay</option>
<option value="11">(11)</option>
<option value="GT1">(1) GT - ALL</option>
<option value="GT7">(7) GT - S1- LB</option>
<option value="GT8">(8) GT - S2 - LB</option>
<option value="GT9">(9) GT - S3</option>
<option value="GT6">(6) GT Whistle</option>
<option value="GT4">(4) GT -LB</option>
<option value="1">(1) Main - PP (2)</option>
<option value="4">(4) MTEST</option>
<option value="2">(2) test</option>
<option value="12">(12) RR1</option>
<option value="10">(10) RR Data</option>
<option value="8">(8) RR 2Mid</option>
<option value="9">(9) RR 3</option>
<option value="6">(6) Silver New PP </option>
<option value="5">(5) SILVER </option>
<option value="3">(3) Strike</option>
</select>
<div class="ms-parent billerId form-control"><button type="button" class="ms-choice"><span class="placeholder">All</span><div class=""></div></button>
<div class="ms-drop bottom" style="display: none;">
  <div class="ms-search"><input type="text" autocomplete="off" autocorrect="off" autocapitilize="off" spellcheck="false"></div>
  <ul style="max-height: 250px;">
    <li class="ms-select-all"><label><input type="checkbox" data-name="selectAllbillerId">[Select All]</label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="7"><span>(7) </span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="11"><span>(11) </span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT1"><span>(1) GT - ALL</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT7"><span>(7) GT - LB</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT8"><span>(8) GT - S2 - 02/11/17 LB</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT9"><span>(9) GT - S3</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT6"><span>(6) GT Whistle</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="GT4"><span>(4) GT -LB</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="1"><span>(1) Main - PP (2)</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="4"><span>(4) MTEST</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="2"><span>(2) test</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="12"><span>(12) RR1</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="10"><span>(10) RR Data</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="8"><span>(8) RR 2Mid</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="9"><span>(9) RR 3</span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="6"><span>(6) Silver New PP </span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="5"><span>(5) SILVER </span></label></li>
    <li class="" style="false"><label class=""><input type="checkbox" data-name="selectItembillerId" value="3"><span>(3) Strike</span></label></li>
    <li class="ms-no-results" style="display: none;">No matches found</li>
  </ul>
</div>

【问题讨论】:

  • 请解释一下不起作用是什么意思?

标签: excel vba selenium-webdriver web-scraping


【解决方案1】:

当您使用 * 标识要定位的选择器时,该 xpath 似乎有两个元素。由于这会返回多个元素,因此它在列表中,因此您无法调用 click 它。

为了使点击生效,您只需要获取您想要点击的元素。不确定哪个是按钮,但这些是两者的显式 xpath:

//option[contains(text(), 'GT - ALL')] 

或者

//span[contains(text(), 'GT - ALL')]

【讨论】:

  • 我为延误道歉!以下代码对我有用,谢谢! //span[contains(text(), 'GT - ALL')]
【解决方案2】:

您可以使用比 xpath 更快的属性 = 值 css 选择器。请注意,文本值似乎是:(1) GT - ALL

您的下拉菜单和复选框是不同的。


输入复选框元素(由值为checkboxtype 属性指示,它是input 标签元素的一部分)

对于您可以使用的复选框

bot.findElementByCss("input[type=checkbox][value='GT1']").click

您可以将其缩短为:

bot.findElementByCss("input[value='GT1']").click

下拉菜单(由父select标签和子option标签元素表示):

有一个非复选框,您可以将以下内容用于:

bot.findElementByCss("option[value='GT1']").click

或者

bot.findElementByCss("option[value='GT1']").Selected = True

如果需要,您可以将 GT1 换成其他值。

【讨论】:

  • 我为延误道歉!以下代码对我有用,谢谢! bot.findElementByCss("option[value='GT1']").click
  • 只能接受一个。当您获得超过 20 分(或类似的分数)时,您可以投票支持您认为有帮助的其他答案。这个想法是你接受你认为对你最好的答案,随着时间的推移,希望 Stackoverflow 的其余部分投票并确保最有用的答案上升到顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 2015-03-13
  • 2017-06-01
  • 2022-12-15
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多