最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Selenium如何点击下拉框并选择某个值

selenium crifan 9541浏览 0评论

Selenium+Python想要实现:可以点击到对应的 选项 按钮,并选择下拉的某个值,比如5

selenium click select option and choose value

How to Select Option from DropDown using Selenium Webdriver

How to select a dropdown value in Selenium WebDriver using Java – Stack Overflow

此处不是select,子元素也不是option

且可以选择的value也没有,只有子元素a,里面的data-m的值是个json字符串,最后包含:itemCount":5,表示是第五个。

<ul id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menu” role=”group” aria-labelledby=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9″ class=”dropdown-menu”>

            <li role=”option”>

<a data-m=”{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;UpdateQuantity&quot;,&quot;bhvr&quot;:91,&quot;pid&quot;:&quot;8X58XHDX57SX&quot;,&quot;sku&quot;:&quot;F96R&quot;,&quot;itemCount&quot;:1}” id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menuItem_1″ class=”ember-view x-hidden-focus”>                          1

                      

</a>            </li>

            <li role=”option”>

<a data-m=”{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;UpdateQuantity&quot;,&quot;bhvr&quot;:91,&quot;pid&quot;:&quot;8X58XHDX57SX&quot;,&quot;sku&quot;:&quot;F96R&quot;,&quot;itemCount&quot;:2}” id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menuItem_2″ class=”ember-view”>                          2

                      

</a>            </li>

            <li role=”option”>

<a data-m=”{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;UpdateQuantity&quot;,&quot;bhvr&quot;:91,&quot;pid&quot;:&quot;8X58XHDX57SX&quot;,&quot;sku&quot;:&quot;F96R&quot;,&quot;itemCount&quot;:3}” id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menuItem_3″ class=”ember-view”>                          3

                      

</a>            </li>

            <li role=”option”>

<a data-m=”{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;UpdateQuantity&quot;,&quot;bhvr&quot;:91,&quot;pid&quot;:&quot;8X58XHDX57SX&quot;,&quot;sku&quot;:&quot;F96R&quot;,&quot;itemCount&quot;:4}” id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menuItem_4″ class=”ember-view”>                          4

                      

</a>            </li>

            <li role=”option”>

<a data-m=”{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;UpdateQuantity&quot;,&quot;bhvr&quot;:91,&quot;pid&quot;:&quot;8X58XHDX57SX&quot;,&quot;sku&quot;:&quot;F96R&quot;,&quot;itemCount&quot;:5}” id=”22bc22dd-ef9d-4d3c-8de9-1e7bc704f9f9_menuItem_5″ class=”ember-view x-hidden-focus”>                          5

                      

</a>            </li>

    </ul>

估计

Selecting a value from a drop-down option using selenium python – Stack Overflow

用xpath去选择,比较好。

如果不用option的value去选,倒是也可以考虑用index去选。

结果此处用:

cartNumOptionElemList = driver.find_elements_by_xpath(‘//ul[@class=”dropdown-menu”]/li[@role=”option”]’)

logging.info(“cartNumOptionElemList=%s”, cartNumOptionElemList)

carNumSelect = Select(cartNumOptionElemList)

carNumSelect.select_by_index(gCfg[“msStore”][“onceBuyNum”])

但是出错:

    carNumSelect = Select(cartNumOptionElemList)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/support/select.py”, line 36, in __init__

    if webelement.tag_name.lower() != “select”:

AttributeError: ‘list’ object has no attribute ‘tag_name’

所以去改为:

直接用for循环就好了。

另外参考:

python – find_elements_by_partial_link_text won’t locate the elements – Stack Overflow

去试试之前就知道的:

Xpath中用contains/startWith

去实现,此处再次判断底层的tag中class/id等包含字符串/或者说 以字符串 开始 

以便于更加精确的匹配:

具体写法,参考:

4. 查找元素 — Selenium-Python中文文档 2 documentation

XPath 1.0 Tutorial @ZVON.org

starts-with [XPath 2.0 Reference @ Zvon.org]

contains [XPath 2.0 Reference @ Zvon.org]

更加精确的是:

matches [XPath 2.0 Reference @ Zvon.org]

结果:

【已解决】selenium.common.exceptions.ElementNotVisibleException Message element not visible

然后就可以获得对应列表元素,并点击了。

【总结】

代码:

<code>cartNumOptionElemList = driver.find_elements_by_xpath('//ul[@class="dropdown-menu"]/li[@role="option"]')
</code>

对应网页:

点击了展开列表选项5之后效果:

转载请注明:在路上 » 【已解决】Selenium如何点击下拉框并选择某个值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.179 seconds, using 22.07MB memory