# coding:utf-8
import os
import time
from selenium import webdriver #引入selenium自动化测试包
from selenium.webdriver.support.ui import Select #通过select选择定位下拉框
from selenium.webdriver.common.keys import Keys #引入key包 调用键盘按键操作
from selenium.webdriver.common.action_chains import ActionChains #引入点击操作包 用于右击 双击 拖动等操作
'''
输入点击
'''
# driver = webdriver.Ie()
# driver.maximize_window()
# driver.get("http://www.baidu.com")
# driver.implicitly_wait(30)
# driver.find_element_by_id("kw").clear() #默认清除输入框内容
# driver.find_element_by_id("kw").send_keys("selenium") #输入框输入'selenium'
# driver.find_element_by_id("su").click() #进行点击 click也可以用 submit(仅提交可用,点击不可用)
# time.sleep(5)
# driver.quit()
'''
context_click() 右击
double_click() 双击
右击 双击的用法
'''
#定位到要右击的元素
# qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标右键操作
# ActionChains(driver).context_click(qqq).perform()
# ActionChains(driver).double_click(qqq).perform()
'''
drag_and_drop() 拖动
拖动的用法
'''
#定位元素的原位置
# element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
# target = driver.find_element_by_name("target")
#执行元素的移动操作
# ActionChains(driver).drag_and_drop(element, target).perform()
'''
前进后退
'''
# driver = webdriver.Chrome()
# driver.maximize_window()
# #访问百度首页
# first_url= 'http://www.baidu.com'
# print "now access %s" %(first_url)
# driver.get(first_url)
# time.sleep(3)
# #访问新闻页面
# second_url='http://news.baidu.com'
# print "now access %s" %(second_url)
# driver.get(second_url)
# time.sleep(3)
# #返回(后退)到百度首页
# print "back to %s "%(first_url)
# driver.back()
# time.sleep(3)
# #前进到新闻页
# print "forward to %s"%(second_url)
# driver.forward()
# time.sleep(5)
# driver.quit()
'''
下拉框和简易弹窗操作
'''
# driver= webdriver.Ie()
# driver.get("http://www.baidu.com")
# driver.maximize_window()
# #进入搜索设置页
# driver.find_element_by_link_text("设置").click()
# driver.find_element_by_link_text("搜索设置").click()
# time.sleep(5)
# #设置每页搜索结果为50条
# select = Select(driver.find_element_by_tag_name("select"))
# select.select_by_visible_text('每页显示50条')
# time.sleep(3)
# #保存设置的信息
# driver.find_element_by_link_text('保存设置').click()
# time.sleep(2)
# #弹窗处理
# driver.switch_to_alert().accept()
# #跳转到百度首页后,进行搜索表(一页应该显示50条结果)
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_id("su").click()
# time.sleep(3)
# driver.quit()
'''
模拟登陆
'''
# driver = webdriver.Ie()
# driver.maximize_window()
# driver.get("http://www.bdwork.com/member.php?mod=logging&action=login&referer=")
# driver.implicitly_wait(30)
# # 登陆
# driver.find_element_by_name("username").send_keys("xxxx")
# driver.find_element_by_name("password").send_keys("xxxxx")
# driver.find_element_by_name("loginsubmit").click()
# time.sleep(3)
# driver.quit()
'''
读取本地文件输入到搜索框
'''
# source = open('路径', 'r')
# values = source.readlines() #按行读取整个文件
# source.close()
# # 执行循环
# for serch in values:
# browser = webdriver.Chrome()
# browser.maximize_window()
# browser.get("http://www.baidu.com")
# serch = serch.decode('gb18030') #处理中文编码问题
# browser.find_element_by_id("kw").send_keys(serch) #将文件读取的内容输入搜索框
# browser.find_element_by_id("su").click()
# time.sleep(3)
# browser.quit()
'''
滚动条拖动
上下
'''
# #访问百度
# driver=webdriver.Ie()
# driver.get("http://www.baidu.com")
# driver.maximize_window()
# #搜索
# driver.find_element_by_id("kw").send_keys("selenium")
# driver.find_element_by_id("su").click()
# time.sleep(3)
# #将页面滚动条拖到底部
# js = "var q=document.body.scrollTop=10000" #谷歌浏览器 var q=document.body.scrollTop
# driver.execute_script(js)
# time.sleep(3)
# # 将滚动条移动到页面的顶部
# js="var q=document.documentElement.scrollTop=0" #其他浏览器 var q=document.documentElement.scrollTop
# driver.execute_script(js)
# time.sleep(3)
# driver.quit()
'''
使用selenium进行截图
'''
# driver.get_screenshot_as_file('1.png') # 截屏
# location = driver.find_element_by_id('xxx').location # 定位验证码位置
# size = driver.find_element_by_id('xxx').size # 获取验证码大小
# left = location['x'] # 对验证码进行截图
# top = location['y']
# right = location['x'] + size['width']
# bottom = location['y'] + size['height']
# a = Image.open("1.png")
# im = a.crop((left, top, right, bottom))
# im.save('1.png') # 保存验证码图片
'''
去除JS弹窗
'''
# js = "document.getElementById('photo').setAttribute('类名',' ')" # 去除弹窗
# driver.execute_script(js)
#
# js = "document.getElementById('aac007').removeAttribute('类名')" # 去除弹窗
# driver.execute_script(js)
'''
处理系统弹窗
'''
# FindWindow('类名','窗口标题') #寻找弹出的系统窗口
#
# PostMessage('系统窗口句柄', win32con.WM_CLOSE, 0, 0)
#
# SendMessage(系统窗口句柄, win32con.WM_SETTEXT, 控件ID, "文本框输入内容") #系统窗口输入信息
# selenium在frame框架下操作:
# driver.switch_to.frame('mainFrame') # 切换到frame下寻找元素
# driver.switch_to.default_content() # 退出frame
'''
生成LOG日志文件
'''
# import unittest
# from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
#
#
# class PythonOrgSearch(unittest.TestCase):
# def setUp(self):
# self.driver = webdriver.Chrome()
#
# def test_search_in_python_org(self):
# driver = self.driver
# driver.maximize_window()
# driver.get("http://www.python.org")
# self.assertIn("Python", driver.title)
# elem = driver.find_element_by_name("q")
# elem.send_keys("pycon")
# elem.send_keys(Keys.RETURN)
# assert "No results found." not in driver.page_source
#
# def tearDown(self):
# self.driver.close()
#
#
# if __name__ == "__main__":
# unittest.main()