Selenium快速入门(下)

Frame相关

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

browser = webdriver.Firefox()
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)

# 在frame里面找不到外面的元素
try:
    logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:
    print('NO LOGO')

# 切换回父级frame
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)

等待

  • 隐式等待
from selenium import webdriver

browser = webdriver.Firefox()
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.close()
  • 显示等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()
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_element_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_loacted     # 元素不可见
element_to_be_clickable       # 元素可点击
staleness_of    # 判断某个元素是都仍在DOM,可判断出页面是否刷新
element_to_be_selected    #元素可选择,传入元素对象
element_located_to_be_selected   # 元素可选择,传定位元组
element_selection_state_to_be    # 传入元素对象及其状态,相等返回True,否则返回False
element_located_selection_state_to_be   # 传入定位元组以及状态,相等返回True,否则返回False
alert_is_present      # 是否出现Alert

页面的前进和后退

import time
from selenium import webdriver

browser  = webdriver.Firefox()
browser.get('https://www.baidu.com/')
browser.get('https://www.taobao.com/')
browser.get('https://www.jd.com/')

browser.back()
time.sleep(1)
browser.forward()
browser.close()

cookies设置

from selenium import webdriver

browser  = webdriver.Firefox()
browser.get('https://www.zhihu.com')   # 以列表的形式返回
print(browser.get_cookies())
# 以追加的形式添加cookie
browser.add_cookie({'name':'name',
                    'domain':'www.zhihu.com',
                    'value':'germy'
                    })

print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

选项卡设置

import time
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')
print(browser.window_handles)

# 窗口的切换从0开始

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://www.jd.com')

异常处理

不使用异常处理

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.baidu.com')
browser.find_element_by_id('hello')  # 报错 selenium.common.exceptions.NoSuchElementException

使用异常处理

from  selenium import webdriver
from selenium.common.exceptions import TimeoutException,NoSuchElementException

browser = webdriver.Firefox()
try:
    browser.get('https://www.baidu.com')
except TimeoutException:
    print('Time out')
try:
    browser.find_element_by_id('hello')
except NoSuchElementException:
    print('No Elemment')
finally:
    browser.close()

你可能感兴趣的:(Selenium快速入门(下))