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

时间:2019-01-22 20:45:25

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

我正在尝试使硒单击输入复选框,但面临不同的错误。
这些复选框之间的唯一区别是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="">


<i class="c-icon hq-icon hq-icon-crown-dealer hq-icon-small " title=""></i>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="">


<i class="c-icon hq-icon hq-icon-reseller hq-icon-small " title=""></i>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="">


<i class="c-icon hq-icon hq-icon-servicing-workshop hq-icon-small " title=""></i>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="">


<i class="c-icon hq-icon hq-icon-category-robotic-lawn-mowers hq-icon-small " title=""></i>Automower® dealers
                                </span>
                            </li>

                            </ul>

                </div>

网站:link

完整代码:

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()

1 个答案:

答案 0 :(得分:2)

如果使用此相当讨厌的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自动化。我还没有找到更好的解决方案,但是至少您知道有一种方法可以与该复选框进行交互。