Selenium + Python。如何点击复选框?

时间:2018-07-20 09:40:55

标签: selenium selenium-webdriver checkbox automated-tests frontend

我有不知道如何解决的问题。我需要设置为TRUE复选框,声明为:

Warning: Did not expect server HTML to contain a <h1> in <div>.

因为这些元素在我在python循环中创建的列表中,该列表在元素上进行了迭代:

implementation 'com.jakewharton:butterknife:8.8.1',{
        exclude group: 'com.android.support'
    })

但是每次我遇到错误:

<li class="consents">
    <form class="consents__form">
        <div class="checkbox-group">
            <input name="5b51a49046e0fb0001bd7b5e" class="checkbox-group__input" id="5b51a49046e0fb0001bd7b5e-SJ7k5D714Q" type="checkbox" value="false">
            <label class="checkbox-group__label" for="5b51a49046e0fb0001bd7b5e-SJ7k5D714Q">
                 <span>...</span>
                 <span class="checkbox-group__required">*</span>
            </label>
         </div>
         <button class="consents__submit" type="submit">Dalej</button>
    </form>
</li>

我也尝试使用xpath,但是效果是一样的:(

1 个答案:

答案 0 :(得分:0)

对不起!我的错! @Andersson说对了,我的代码中也有一个错误。循环看起来像下面这样:

self.driver.find_element_by_css_selector('li.consents')
consents = self.driver.find_elements_by_css_selector('div.checkbox-group')
    for consent in consents:
        checkbox = consent.find_element_by_css_selector('label.checkbox-group__label')
        checkbox.click()