Selenium
第三方库资源,需要下载 pip3 install selenium
需要配置path环境变量才可以使用. 下载驱动文件 Chromedriver
, 将本文件放到Python3
的环境下
1.基本使用
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
brower = webdriver.Chrome() # 声明浏览器对象
try:
brower.get("https://www.baidu.com") # 发起get请求
input = brower.find_element_by_id("kw") # 通过id找到id为kw的元素
input.send_keys("python") # 向元素中敲入一些自己的键
input.send_keys(Keys.ENTER) # 敲入回车
wait = WebDriverWait(brower, 10) # 调用等待时间
wait.until(EC.presence_of_element_located((By.ID, "content_left"))) # 等待元素id为"content_left的元素加载出来"
print(brower.current_url) # url地址
print(brower.get_cookies()) # cookies
print(brower.page_source) # 网页源代码
finally:
brower.close() # 关闭浏览器
声明浏览器对象
from selenium import webdriver
brower = webdriver.Chrome() # 声明浏览器对象
brower = webdriver.Firefox()
brower = webdriver.Edge()
brower = webdriver.PhantomJS()
brower = webdriver.Safari()
访问页面
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.baidu.com")
print(browser.page_source)
browser.close()
查找元素
单个元素
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.taobao.com")
input_first = browser.find_element_by_id("q") # 查找id为q的元素
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
通用方法
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get("https://www.taobao.com")
input_first = browser.find_element(By.ID, "q") # 查找id为q的元素
print(input_first)
browser.close()
多个元素
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.taobao.com")
lis = browser.find_elements_by_css_selector('.service-bd li')
print(lis)
browser.close()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
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
from selenium import webdriver
browser = webdriver.Chrome()
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("btn-search")
button.click()
更多操作: https://selenium-python.readthedocs.io/api.html
元素交互动作
将动作附加到动作链当中串行执行
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
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()
更多操作: https://selenium-python.readthedocs.io/api.html
执行JavaScript
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.zhihu.com/explore")
browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
browser.execute_script("alert('To Bottom')")
获取元素信息
获取属性
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.zhihu.com/explore")
logo = browser.find_element_by_id("zh-top-link-logo")
print(logo)
print(logo.get_attribute('class'))
browser.close()
获取文本值
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.zhihu.com/explore")
input = browser.find_element_by_class_name("zu-top-add-question")
print(input.text)
browser.close()
获取ID, 位置, 标签名, 大小
from selenium import webdriver
url = "https://www.zhihu.com/explore"
browser = webdriver.Chrome()
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) # 获取大小
browser.close()
Frame
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome()
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)
等待
隐式等待
当使用了隐式等待执行测试的时候,如果webDriver没有在DOM中找到元素,将继续等待
超出设定时间后侧跑出找不到元素的异常, 换句话说,当查找元素货元素并没有立即出现的时候,
隐式等待则在等待一段时间查找DOM,默认时间是0
from selenium import webdriver
browser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get("https://www.zhihu.com/explore")
input = browser.find_element_by_class_name("zu-top-add-question")
print(input)
显示等待
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.Chrome()
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_located 元素不可见
- 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
- 更多操作:
https://selenium-python.readthedocs.io/api.html
前进后退
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.baidu.com/")
time.sleep(1)
browser.get("https://www.taobao.com/")
time.sleep(1)
browser.get("https://www.jd.com/")
time.sleep(1)
browser.back() # 后退
time.sleep(2)
browser.forward() # 前进
browser.close()
Cookies
from selenium import webdriver
browser = webdriver.Chrome()
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.close()
选项卡管理
import time
from selenium import webdriver
browser = webdriver.Chrome()
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.jd.com")
time.sleep(2)
browser.switch_to_window(browser.window_handles[0])
browser.get("https://www.taobao.com")
异常处理
from selenium import webdriver
browser = webdriver.Chrome()
browser.find_element_by_id("hello")
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
browser = webdriver.Chrome()
try:
browser.get("https: www.jd.com")
except TimeoutException:
print("Time Out")
try:
browser.find_element_by_id("hello")
except NoSuchElementException:
print("No Element")
finally:
browser.close()
详细文档: https://selenium-python.readthedocs.io/api.html