Selenium 入门到精通系列:六

Selenium 入门到精通系列


PS:Checkbox方法

例子

HTML:

<html>
<head>
<title>测试页面</title>
</head>
<body>
<form action="" method="get">您喜欢的水果?<br /><br />
<label><input name="Fruit" type="checkbox" value="" />苹果 </label>
<label><input name="Fruit" type="checkbox" value="" />桃子 </label>
<label><input name="Fruit" type="checkbox" value="" />香蕉 </label>
<label><input name="Fruit" type="checkbox" value="" /></label>
</form>
</body>
</html>

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019-04-23 16:12:33
# @Author  : BenLam
# @Link    : https://www.cnblogs.com/BenLam/

from selenium import webdriver
driver=webdriver.Firefox()
driver.get('test.html')

#选中所有的tag name为input的元素
inputs=driver.find_elements_by_tag_name('input')

#for循环勾选
for i in inputs:
    if i.get_attribute('type')=='checkbox':
        i.click()
driver.quit()

猜你喜欢

转载自www.cnblogs.com/BenLam/p/10760951.html