#coding=utf-8
import time
from selenium import webdriver
dr = webdriver.Chrome()
# dr = webdriver.Ie()
dr.maximize_window()
dr.implicitly_wait(3)
dr.get("https://www.baidu.com")
# try:
# dr.find_element_by_id("kw")
# print ("test pass: ID found")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
# try:
# dr.find_element_by_tag_name("form")
# print ("test pass: form found")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
# try:
# # dr.find_element_by_link_text("新闻")
# dr.find_element_by_xpath(".//*[@id='u1']/a[text()='新闻']")
# print ("test pass: link text found")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
# try:
# dr.find_element_by_class_name("s_ipt")
# print ("test pass: element found by class name")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
#很多情况下,class利用要比id多,如果class中出现了太长的字符,和可变化的数字,那么请回到用XPath定位方法。
# try:
# dr.find_element_by_name("wd")
# print ("test pass: element found by name")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
# try:
# dr.find_element_by_css_selector("#su")
# print ("test pass: element found by css")
#
# except Exception as e:
# print ("Exception found",format(e))
#
# dr.quit()
#文本清除
# dr.find_element_by_id("kw").send_keys("Selenium")
#
# try:
# dr.find_element_by_id("kw").clear()
# print ("test pass: clean successful")
# except Exception as e:
# print ("Exception found",format(e))
# dr.quit()
# import random
#
# num = 2
# while num > 0:
# try:
# num = random.randint(60, 600)
# print num
# time.sleep(num)
# dr.refresh()
# print ("test pass: refresh successful")
# except Exception as e:
# print ("Exception found",format(e))
# #dr.quit()
#前进与后退功能
# time.sleep(2)
# ele_news = dr.find_element_by_link_text("新闻")
# ele_news.click()
# time.sleep(2)
# dr.back()
# time.sleep(2)
# dr.forward()
# time.sleep(2)
# dr.quit()
#获取浏览器版本与url
from selenium.webdriver.common.keys import Keys
# time.sleep(1)
# dr.find_element_by_link_text("新闻").click()
# time.sleep(1)
#
# print (dr.current_url)
# print (dr.capabilities['version'])
# print (dr.title)
#
# dr.quit()
# time.sleep(1)
# ele = dr.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't')
# time.sleep(1)
# dr.quit()
# dr.get("https://news.baidu.com")
# dr.implicitly_wait(8)
#
# for i in dr.find_elements_by_xpath("//*/input[@type='radio']"):
# i.click()
# print (dr.get_window_size())
# time.sleep(1)
# dr.set_window_size(1024,768)
# time.sleep(1)
# print (dr.get_window_size())
# dr.quit()
# try:
# assert u"百度一下" in dr.title
# print ("pass")
#
# except Exception as e:
# print ("Exception found",format(e))
# dr.quit()
# if u"百度一下,你就知道" == dr.title:
# print ("pass")
# print (dr.title)
# else:
# print (" fail test")
# dr.quit()
#获取元素上的文字
# time.sleep(1)
# dr.find_element_by_xpath(".//*[@id='u1']/a[7]").click()
# time.sleep(1)
#
# dr.find_element_by_xpath(".//*[@id='TANGRAM__PSP_10__submit']").click()
#
# try:
# error_message = dr.find_element_by_xpath(".//*[@id='TANGRAM__PSP_10__error']").is_displayed()
# print ("test pass")
# except Exception as e:
# print ("test fail",format(e))
# dr.quit()
#建议方法二
# time.sleep(1)
# dr.find_element_by_xpath(".//*[@id='u1']/a[7]").click()
# error_message = dr.find_element_by_xpath(".//*[@id='TANGRAM__PSP_10__error']").text
# try:
# error_message == u'请您填写手机/邮箱/用户名'
# print ("test pass")
# except Exception as e:
# print ("test fail",format(e))
#
# dr.quit()
#验证控件是否被选中
# dr.get("https://news.baidu.com")
# try:
# dr.find_element_by_xpath("//*[@id='news']").is_selected()
# print ("test pass")
# except Exception as e:
# print ("test fail" ,format(e))
# dr.quit()
#获取页面元素大小
# from selenium.webdriver.common.keys import Keys
#
# search_btn = dr.find_element_by_id("kw")
# print (search_btn.size)
# #ele = dr.find_element_by_tag_name("body")
# #ele.send_keys(Keys.CONTROL + 'a')
# search_btn.send_keys("selenium")
# time.sleep(2)
# search_btn.send_keys(Keys.CONTROL+'a')
# search_btn.send_keys(Keys.BACKSPACE)
# dr.quit()