【问题标题】:Is there a way in selenium python webdriver to assert that all checkboxes are either checked or unchecked?selenium python webdriver中有没有办法断言所有复选框都被选中或未选中?
【发布时间】:2015-01-30 00:56:03
【问题描述】:

我正在构建一个测试来检查单个页面上许多复选框的状态。

有没有办法我可以使用循环或类似的东西来断言它们都被选中或未选中?我是 python 和 python webdriver 的新手,所以我什至不确定如何开始这样的事情。

我使用 java 看到过类似问题的答案,但在 python 中没有。

感谢您的帮助。

@Anzel,这是我正在使用的代码:

checkboxes = driver.find_elements_by_xpath('.//input[@type="checkbox"]')
[c.is_selected() for c in checkboxes]
[True, True, False, False, False, False, False, False, False]
assert all([c.is_selected() for c in checkboxes])

这会得到以下错误:

Traceback (most recent call last):
File "test.py", line 71, in test_test
assert all([c.is_selected() for c in checkboxes])
AssertionError

感谢您的帮助。

【问题讨论】:

    标签: python selenium checkbox selenium-webdriver


    【解决方案1】:

    是的,您可以使用 is_selected() 来测试该元素是否被选中。如果你使用find_elements_by_xxx(),那么你只需要循环遍历它并assert他们的结果。

    让我们看一个示例:

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    url = 'http://www.tizag.com/htmlT/htmlcheckboxes.php'
    driver.get(url)
    checkboxes = driver.find_elements_by_name('sports')
    
    # simply use is_selected() can yield their Selected status
    [c.is_selected() for c in checkboxes]
    [False, False, False, False, True, False, False, True]
    

    # to assert they are all unchecked, use not any()
    assert not any([c.is_selected() for c in checkboxes])
    
    
    # so to assert they are all checked, just use all()
    assert all([c.is_selected() for c in checkboxes])
    
    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-661-c6f9ba4afaf2> in <module>()
    ----> 1 assert all([c.is_selected() for c in checkboxes])
    
    AssertionError: 
    

    更新:

    您可以通过 xpath 简单地找到所有复选框,这通常有效:

    driver.find_elements_by_xpath('.//input[@type="checkbox"]')
    

    但这不是保证,您确实需要不时提出自定义解决方案。

    这是一个Webdriver支持到find元素的列表:

    [_ for _ in dir(driver) if 'find' in _]
    Out[10]: 
    ['find_element',
     'find_element_by_class_name',
     'find_element_by_css_selector',
     'find_element_by_id',
     'find_element_by_link_text',
     'find_element_by_name',
     'find_element_by_partial_link_text',
     'find_element_by_tag_name',
     'find_element_by_xpath',
     'find_elements',
     'find_elements_by_class_name',
     'find_elements_by_css_selector',
     'find_elements_by_id',
     'find_elements_by_link_text',
     'find_elements_by_name',
     'find_elements_by_partial_link_text',
     'find_elements_by_tag_name',
     'find_elements_by_xpath']
    

    显然没有by_type,所以它会给你一个AttributeError

    【讨论】:

    • 感谢您的回复@Anzel,但我的每个复选框都有一个唯一的名称。我尝试了 checkboxes = driver.find_elements_by_type('checkbox') 但这给出了一个错误:'WebDriver' object has no attribute 'find_elements_by_type'
    • @user4476068,我已经更新了我的答案以提供一个通用的解决方案。但是您不应该依赖一刀切的方法,有人可以轻松地从divsvg 元素构建一个复选框...
    • 感谢您的更新。我的脚本通过了 find_elements_by_xpath 语句,但就像你上面的例子一样,我在“assert all([c.is_selected() for c in checkboxes])”处遇到了一个断言错误。我已经确保我的 True/False 与屏幕上的内容匹配,所以我不确定它为什么会失败。感谢您的帮助。
    • @user4476068,您能否在原始帖子中添加更新并显示您收到的代码+错误消息?我必须在这里说清楚,我希望你明白我不是想给你一个 FULL 解决方案(通常也是如此),而是你所掌握的基本技能/技术错过了这一点。
    • 我完全理解并且非常感谢您迄今为止的帮助。我几乎不得不用 python webdriver 跳进煎锅,所以此时我真的很绿。您的 cmets 非常有帮助和赞赏。我已经更新了我原来的问题,包括我正在使用的代码和它产生的错误。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2016-01-02
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多