如何断言是否通过Selenium选择了单选按钮

时间:2018-10-19 21:34:20

标签: selenium selenium-webdriver css-selectors webdriver

在Selenium中,我使用伪选择器选择此单选按钮输入(下面的屏幕截图)。然后,我尝试使用assertTrue和方法isSelected来查看是否已选中该单选按钮。但我收到一个错误..见下文:

import unittest
from selenium.webdriver import Chrome

class MyTest(unittest.TestCase):

    def test_radio_button(self):
        filter_panel = self.browser.find_element_by_class_name('panel-filters')
        radio_button = filter_panel.find_element_by_css_selector('section.section-units > fieldset#filter-units > div.form-group:nth-child(2) > input')

        self.assertTrue(radio_button.isSelected());

运行Selenium测试时出现此错误:

self.assertTrue(filter_units.isSelected()); AttributeError:“ WebElement”对象没有属性“ isSelected”

如何测试是否选中了此单选按钮?


enter image description here

1 个答案:

答案 0 :(得分:-1)

当您通过 unittest 模块使用 Selenium Python 客户端时,相关方法为 is_selected() ,而不是isSelected()


is_selected()

is_selected()定义为:

is_selected()

Returns whether the element is selected.
Can be used to check if a checkbox or radio button is selected.

如果未选择单选按钮 确认,则可以使用以下解决方案之一:

radio_button = self.browser.find_element_by_css_selector("input.form-control#metric_0[name='filter_units'][value='MV']")
self.assertTrue(radio_button.is_selected()));