【问题标题】:Select checkbox using Selenium with Python3.x and Selenium使用 Selenium 和 Python3.x 和 Selenium 选择复选框
【发布时间】:2018-04-13 18:35:25
【问题描述】:

我正在尝试检查页面上的复选框: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>

我尝试这样做:

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

for checkbox in checkboxes:

    if not checkbox.is_selected():

        checkbox.click()

但它不起作用。你能帮助我吗?

【问题讨论】:

  • 是否有多个复选框

标签: python-3.x selenium web-scraping


【解决方案1】:

试试这个 xpath

checkElements= driver.find_element_by_xpath("//input[@type='checkbox' and @value='facility']")
checkElements.click()

我不知道python所以可能是语法错误但路径是正确的

【讨论】:

    【解决方案2】:

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

    你可以这样做:

    checkbox = driver.find_element_by_id("poi-legend-facility")
    
    if not checkbox.is_selected():
    
       checkbox.click()
    

    或者在你的情况下试试这个代码:

    checkboxes = driver.find_elements_by_id("poi-legend-facility")
    checkboxes[0].click()
    

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

    【讨论】:

    • 不错的答案,@RatmirAsanov
    【解决方案3】:

    这是我能找到的唯一方法来检查该页面中的所有框并跳出循环。也试试这个。

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    driver.get('https://www.pkobp.pl/poi/?clients=1,2,3')
    for tickbox in driver.find_elements_by_css_selector(".input-checkbox"):
        try:
            tickbox.click()
            time.sleep(7)
        except:
            break
    driver.quit()
    

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2019-11-26
      • 2016-03-24
      相关资源
      最近更新 更多