# Selenium
#
# 自动化测试工具,致辞多种浏览器
# 爬虫中主要用来解决JavaScript渲染的问题
# 安装
# pip install seleium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# try:
# browser.get('https://baidu.com')
# input = browser.find_element_by_id('kw')
# input.send_keys('Python')
# input.send_keys(Keys.ENTER) # 回车
# wati = WebDriverWait(browser, 10)
# wati.until(EC.presence_of_element_located((By.ID, 'su')))
# print(browser.current_url) # 当前页面的url
# print(browser.get_cookies()) # cokies列表
# print(browser.page_source) # 网页源代码
# finally:
# browser.close()
# # 声明浏览器对象
# # from selenium import webdriver
# # browser = webdriver.Chrome()
# # browser = webdriver.Firefox()
# # browser = webdriver.Edge()
# # browser = webdriver.PhantomJS()
# # browser = webdriver.Safari()
# # 访问页面
# from selenium import webdriver
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com')
# print(browser.page_source)
# browser.close()
# # 查找元素
# #
# # 单个元素
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com/')
# input_first = browser.find_element_by_id('q') # id
# input_second = browser.find_element_by_css_selector('#q') # css选择器
# input_third = browser.find_element_by_xpath('//*[@id="q"]') # xpath
# print(input_first, input_second, input_third)
# browser.close()
# # find_element_by_name
# # find_element_by_xpath
# # find_element_by_link_text
# # find_element_by_partial_link_text
# # find_element_by_tag_name
# # find_element_by_class_name
# # find_element_by_css_selector
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com')
# input_first = browser.find_element(By.ID, 'q')
# print(input_first)
# browser.close()
# # 多个元素
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://.www.taobao.com')
# lis = browser.find_elements_by_css_selector('.service-bd li') # 返回列表
# print(lis)
# browser.close()
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com')
# lis = browser.find_elements(By.CSS_SELECTOR, '.service-bd li') # 返回列表
# print(lis)
# browser.close()
# find_elements_by_name
# find_elements_by_xpath
# find_elements_by_link_text
# find_elements_by_partial_link_text
# find_elements_by_tag_name
# find_elements_by_class_name
# find_elements_by_css_selector
# 元素交互动作
# import time
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com')
# input = browser.find_element_by_id('q')
# input.send_keys('iPhone')
# time.sleep(1)
# input.clear()
# input.send_keys('iPad')
# button = browser.find_element_by_class_name('tb-bg')
# button.click()
# browser.close()
# 交互动作
#
# 将动作附加到动作链中串行执行
# from selenium.webdriver import ActionChains
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
# browser.get(url)
# browser.switch_to.frame('iframeResult')
# source = browser.find_element_by_css_selector('#draggable')
# target = browser.find_element_by_css_selector('#droppable')
# actions = ActionChains(browser)
# actions.drag_and_drop(source, target)
# actions.perform()
# browser.close()
# 执行JavaScript
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'https://www.zhihu.com/explore'
# browser.get(url)
# browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
# browser.execute_script('alert("To Bottom")')
# 获取元素信息
#
# 获取属性
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'https://www.zhihu.com/explore'
# browser.get(url)
# logo = browser.find_element_by_id('zh-top-link-logo')
# print(logo)
# print(logo.get_attribute('class'))
# browser.close()
# 获取文本值
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'https://www.zhihu.com/explore'
# browser.get(url)
# input = browser.find_element_by_class_name('zu-top-add-question')
# print(input.text)
# 获取ID 位置 标签名 大小
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'https://www.zhihu.com/explore'
# browser.get(url)
# input = browser.find_element_by_class_name('zu-top-add-question')
# print(input.id) # id
# print(input.location) # 位置
# print(input.tag_name) # 标签名
# print(input.size) # 大小
# Frame
#
# from selenium.common.exceptions import NoSuchElementException
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
# browser.get(url)
# browser.switch_to.frame('iframeResult')
# source = browser.find_element_by_css_selector('#draggable')
# print(source)
# try:
# logo = browser.find_element_by_class_name('logo')
# except NoSuchElementException:
# print('NO LOGO')
# browser.switch_to.parent_frame()
# logo = browser.find_element_by_class_name('logo')
# print(logo)
# print(logo.text)
# 等待
#
# 隐式等待
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.implicitly_wait(10) # 隐式等待
# browser.get('https://www.zhihu.com/explore')
# input = browser.find_element_by_class_name('zu-top-add-question')
# print(input)
# 显式等待
#
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.taobao.com/')
# wait = WebDriverWait(browser, 10)
# input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
# button = wait.until(EC.element_to_be_clickable(
# (By.CSS_SELECTOR, '.btn-search')))
# print(input, button)
# title_is 标题是某内容
# title_contains 标题彪悍某内容
# presence_of_element_located 元素加载出 传入定位远足,如(By.ID,'p')
# visibility_of_elements_located 元素可见,传入定位元组
# visibility_of 可见,传入元素对象
# presence_of_all_elements_located 所有元素加载出
# text_to_be_present_in_element 某个元素文本包含某文字
# text_to_be_present_in_element_value 某个元素值包含某文字
# frame_to_be_available_and_switch_to_it frame加载并切换
# invisibility_of_element_located 元素不可见
# element_to_be_clickable 元素可点击
# staleness_of 判断一个元素是否仍在DOM,可判断页面是否已经刷新
# selement_to_be_selected 元素可选择,传元素对象
# element_located_to_be_selected 元素可选择,传入定位元祖
# element_selection_state_to_be 传入元素对象以及状态,相等返回True,否则返回False
# selement_located_selection_state_to_be 传入定位远祖以及状态,相等返回True,否则返回False
# alert_is_present 是否出现alert
# 前进后题
import time
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.baidu.com/')
# browser.get('https://taobao.com/')
# browser.get('https://wwww.python.org/')
# browser.back()
# time.sleep(1)
# browser.forward()
# browser.close()
# Cookies
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.zhihu.com/explore')
# print(browser.get_cookies())
# browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com','value':'germey'})
# print(browser.get_cookies())
# browser.delete_all_cookies()
# print(browser.get_cookies())
# 选项卡管理
#
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.baidu.com')
# browser.execute_script('window.open()')
# print(browser.window_handles)
# browser.switch_to_window(browser.window_handles[1])
# browser.get('https://www.taobao.com')
# time.sleep(1)
# browser.switch_to_window(browser.window_handles[0])
# browser.get('https://python.org')
# 异常处理
# browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
# browser.get('https://www.baidu.com')
# browser.find_elements_by_id('hello')
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser = webdriver.Chrome('/Users/songhuan/path/chromedriver')
try:
browser.get('https://www.baidu.com')
except TimeoutException:
print('Time Out')
try:
browser.find_elememt_by_id('hello')
except NoSuchElementException:
print('No Element')
except Exception as e:
print(e)
finally:
browser.close()