使用Selenium和Python3.x和Selenium选中复选框

Big*_*igD 3 selenium web-scraping python-3.x

我正在尝试检查页面上的复选框:https : //www.pkobp.pl/poi/?clients=1,2,3

<li class="poi-filter-top__el">
   <div class="poi-icon poi-icon--facility"></div>
   <input type="checkbox" id="poi-legend-facility" class="js-poi-legend" name="type" value="facility"> <label for="poi-legend-facility" class="poi-legend input-checkbox poi-filter-top__label">Oddzia?</label>  <a href="#facility" class="js-poi-action poi-filter-top__link" data-action="facility">Wybierz rodzaj</a>  
</li>
Run Code Online (Sandbox Code Playgroud)

我尝试这样做:

checkboxes = driver.find_elements_by_id("poi-legend-facility")

for checkbox in checkboxes:

    if not checkbox.is_selected():

        checkbox.click()
Run Code Online (Sandbox Code Playgroud)

但这是行不通的。你能帮助我吗?

Rat*_*nov 5

您只有一个具有此类ID(poi-legend-facility)的复选框。

您可以这样:

checkbox = driver.find_element_by_id("poi-legend-facility")

if not checkbox.is_selected():

   checkbox.click()
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试以下代码:

checkboxes = driver.find_elements_by_id("poi-legend-facility")
checkboxes[0].click()
Run Code Online (Sandbox Code Playgroud)

PS:使用ID查找元素要比通过XPath查找元素快。