#页面元素定位
from seleniumimport webdriver
import time
from selenium.webdriver.support.selectimport Select
# driver = webdriver.Firefox()
# driver = webdriver.Chrome()
# driver = webdriver.Ie()
driver = webdriver.Chrome()
driver.get("file:///D:/Python/huice/demo.html")
# driver.get("http://www.baidu.com")#会等待静态的资源加载完,不会等待动态的资源加载完
# #等待
# time.sleep(2)
# #找到输入框,输入测试
# driver.find_element_by_id("kw").send_keys("测试")
# #找到百度一下按钮,点击
# driver.find_element_by_id("su").click()
#下拉框 用Select()
ele = driver.find_element_by_name("select")
ele_obj = Select(ele)
# 用.select_by_index(索引)找下拉框元素
for iin range(4):
time.sleep(2)
ele_obj.select_by_index(1)
# 用select_by_value(value值)找下拉框元素,一般选中这种方式
ele_obj.select_by_value("3")
# 用select_by_visible_text(可见文本)找下拉框元素
ele_obj.select_by_visible_text("BMW")
#统计下拉框可选项
sel=len(ele_obj.options)
for iin range(len(sel)):
time.sleep(2)
ele_obj.select_by_index(i)
#单选框
driver.find_element_by_name("identity").click()
#判断单选框是否被选中
driver.find_element_by_name("identity").is_selected()
driver.find_element_by_class_name("BMW")
#复选框, 只能选一项复选框
driver.find_element_by_name("checkbox").click()
# 多选复选框
ele_list = driver.find_elements_by_name("checkbox")
for iin ele_list:
i.click()
#获取元素的内容
print(driver.find_element_by_name("info").text)
#获取元素的属性
url = driver.find_element_by_class_name("baidu").get_attribute("href")#获取页面百度的链接地址
driver.get(url)
driver.find_element_by_id("su").click()
网友评论