#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Selenium3+webdriver学习笔记8(单选、复选框、弹窗处理)
'''

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys

import time,os
import random

# about:addons 火狐浏览器安装组件,访问的地址

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="测试部落"
delay=3
url="https://www.baidu.com/"
url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/pop08.html"
url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/box08.html"
url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/table08.html"
driver=webdriver.Firefox()

#弹出框 主要分 alert confirm prompt
#主要的方法有 获取文本值:text 输入内容:send_keys 取消:dismiss 确定:accept

driver.get(url)
time.sleep(delay)

#单选按钮操作
driver.find_element_by_id("boy").click()

#判断选择的状态
flag=driver.find_element_by_id("girl").is_selected()
print(flag)

#复选框操作
boxs=driver.find_elements_by_xpath(".//*[@type='checkbox']")
for i in boxs:
i.click()

#弹窗处理 alter confirm prompt
driver.find_element_by_id("alert").click()
driver.find_element_by_id("confirm").click()
driver.find_element_by_id("prompt").click()
time.sleep(delay)

t=driver.switch_to_alert()
print(t.text)

t.send_keys("需要输入你想要查看的")
#
t.accept
#
# t.dismiss()

# 表格页面数据获取
# 里面的body要换成tbody 行和列中的值可按顺序获取值
t=driver.find_element_by_xpath(".//*[@id='mytable']/tbody/tr[2]/td[1]")
print(t.text)

driver.quit()




posted on 2018-12-04 20:54  新美好时代  阅读(250)  评论(0编辑  收藏  举报