HTML源码:
<!DOCTYPE html>
<div lang="en"></div></div>
<head>
<meta charset="UTF-8">
<title>多选文本框</title>
</head>
<body>
<from>
<input type="radio" name="fruit" value="berry" />草莓</input>
<br/>
<input type="radio" name="fruit" value="watermelon" />西瓜</input>
<br/>
<input type="radio" name="fruit" value="orange" />橙子</input>
</from>
</body>
</html>
python+selenium源码:
from selenium import webdriver
import unittest
import time
class LianXi_test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.url = r"E:\python\demo\Xpath\demo1.html"
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def test_get(self):
# ger自己的html网页
self.driver.get(self.url)
# 使用Xpath定位获取Value属性值为'berry'的input元素对象,也就是草莓选项
i = self.driver.find_element_by_xpath("//input[@value='berry']")
i.click()
time.sleep(1)
self.assertTrue(i.is_selected(), "草莓单选框未被选中")
if i.is_selected(): # 果草莓单选项被成功选中,重新选择西瓜
o = self.driver.find_element_by_xpath("//input[@value='watermelon']")
o.click()
time.sleep(1)
# 选择西瓜后,断言草莓选项是否处于未选中状态
self.assertFalse(i.is_selected())
p = self.driver.find_elements_by_xpath("//input[@name='fruit']")
for u in p:
time.sleep(1)
if u.get_attribute("value") == "orange":
time.sleep(1)
if not u.is_selected():
u.click()
time.sleep(1)
self.assertEqual(u.get_attribute("value"), "orange")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()