开发者社区> 问答> 正文

Python Selenium通过data-filter-tag值单击输入标记

一码平川MACHEL 2019-01-23 16:28:41 581

我试图让selenium点击输入复选框但面临不同的错误。
这些复选框之间的唯一区别是data-filter-tag值,所以我尝试了这个。

driver.find_element_by_css_selector("input[data-filter-tag='am']").click()
错误

ElementNotVisibleException: Message: element not interactable
网页加载100%

HTML代码:

                        <ul>

                        <li class="filter-checkbox active-filter">
                            <div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl01$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="c"></div>
                            <span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">

Crown dealers

                            </span>
                        </li>

                        <li class="filter-checkbox active-filter">
                            <div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl02$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="r"></div>
                            <span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">

Authorized dealers

                            </span>
                        </li>

                        <li class="filter-checkbox active-filter">
                            <div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl03$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="cs"></div>
                            <span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">

Has service

                            </span>
                        </li>

                        <li class="filter-checkbox active-filter">
                            <div class="custom-checkbox" style="background-position: 0px center;"><input name="ctl00$ctl00$MainContentPlaceHolder$BaseContentPlaceHolder$pmainedge2edge4_0$ctl00$ctl14$rpFilters$ctl04$ctl00" type="checkbox" class="dealer-locator-filter" data-filter-tag="am"></div>
                            <span data-toggle="popover" data-placement="top" data-trigger="hover" data-content="" data-original-title="" title="">

Automower® dealers

                            </span>
                        </li>

                        </ul>

            </div>

完整代码:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome('./chromedriver/chromedriver')
### website url
driver.get('https://www.husqvarna.com/us/')
sleep(6)
driver.find_element_by_css_selector("input[data-filter-tag='am']").click()

Python
分享到
取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:26:43

    如果你用这个相当讨厌的CSS选择器选中一个复选框的周围div,你至少可以点击一个没有例外的复选框。

    checkbox = driver.find_element_by_css_selector("#MainContentPlaceHolder_BaseContentPlaceHolder_pmainedge2edge4_0_ctl00_ctl14_dealerFilters > section:nth-child(1) > div:nth-child(1) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(4) > div:nth-child(1)")
    checkbox.click()
    有很多JavaScript干扰了webdriver自动化。我还没有找到更好的解决方案,但至少你知道有一种方法可以与该复选框进行交互。

    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题