博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Selenium 库的基本用法
阅读量:5259 次
发布时间:2019-06-14

本文共 6758 字,大约阅读时间需要 22 分钟。

Selenium库的基本使用

 

  1.基本使用

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitbrower = webdriver.Chrome()try:    brower.get('https://www.baidu.com')    input = brower.find_element_by_id('kw')#查找id为kw的元素    input.send_keys('Python')#向input文本框输入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()

  2.声明浏览器对象

from selenium import webdriverbrower = webdriver.Chrome()brower = webdriver.Firefox()brower = webdriver.Edge()brower = webdriver.PhanromJS()brower = webdriver.Safari()

  3.访问页面

from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://ww.taobao.com')print(brower.page_source)brower.colse()

  4.查找元素

  单个元素

from selenium import webdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')input_first=browser.find_element_by_id('q')input_second = browser.find_element_by_css_selector('#q')input_third = browser.find_element_by_xpath('//*[@id="q"]')print(input_first,input_second,input_third)browser.close()
结果:
 
 
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') print(input_first) browser.close() 结果:
 

  多个元素

from selenium import webdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')list=browser.find_elements_by_css_selector('.service-bd li')print(list)browser.close()from selenium import webdriverbrowser = webdriver.Chrome()browser.get('https://www.taobao.com')list=browser.find_elements(By.CSS_SELECTOR,'.service-bd li')print(list)browser.close()

  5.元素交互操作

from selenium import webdriverimport timebrowser = 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()

  6.交互动作

from selenium import webdriverfrom selenium.webdriver import ActionChainsbrower=webdriver.Chrome()url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"brower.get(url)brower.switch_to.frame('iframeResult')source=brower.find_element_by_css_selector('#draggable')target=brower.find_element_by_css_selector('#droppable')action=ActionChains(brower)action.drag_and_drop(source,target)action.perform()

  7.执行JavaScript

from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')brower.execute_script('window.scrollTo(0,document.body.scrollHeight)')brower.execute_script('alert("To Bottom")')

  8.获取属性

from selenium import webdriverfrom selenium.webdriver import ActionChainsbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)logo = brower.find_element_by_id('zh-top-link-logo')print(logo)print(logo.get_attribute('class')) 获取:
zu-top-link-logo

  9.获取文本值

from selenium import webdriverbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)input=brower.find_element_by_class_name('zu-top-add-question')print(input.text) 结果:
提问

  10.获取ID,位置,标签名,大小

from selenium import webdriverbrower = webdriver.Chrome()url='https://www.zhihu.com/explore'brower.get(url)input=brower.find_element_by_class_name('zu-top-add-question')print(input.id)print(input.location)print(input.tag_name)print(input.size) 结果:
0.5790966028649505-1{'x': 760, 'y': 7}button{'height': 32, 'width': 66}

  11.Frame

import timefrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionbrower = webdriver.Chrome()url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"brower.get(url)brower.switch_to.frame('iframeResult')source=brower.find_element_by_css_selector('#draggable')print(source)try:    logo=brower.find_element_by_class_name('logo')except NoSuchElementException:    print('No Logo')brower.switch_to.parent_frame()logo=brower.find_element_by_class_name('logo')print(logo)print(logo.text)结果:
No Logo
RUNOOB.COM

  12.隐式等待

from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')brower.implicitly_wait(10)input=brower.find_element_by_class_name('zu-top-add-question')print(input)结果:

  13.显示等待

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitbrower = webdriver.Chrome()brower.get('https://www.taobao.com')wait=WebDriverWait(brower,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)结果:

  14.前进后退

from selenium import webdriverimport timebrower = webdriver.Chrome()brower.get('https://www.baidu.com')brower.get('https://www.taobao.com')brower.get('https://www.python.org')brower.back()time.sleep(1)brower.forward()brower.close()

  15.Cookies

from selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.zhihu.com/explore')print(brower.get_cookies())brower.add_cookie({
'name':'name','domain':'www.zhihu.com','value':'germey'})print(brower.get_cookies())brower.delete_all_cookies()print(brower.get_cookies())

  16.选项卡管理

import timefrom selenium import webdriverbrower = webdriver.Chrome()brower.get('https://www.baidu.com')brower.execute_script('window.open()')print(brower.window_handles)brower.switch_to_window(brower.window_handles[1])brower.get('https://www.taobao.com')time.sleep(1)brower.switch_to_window(brower.window_handles[0])brower.get('https://www.python.org') 结果:
['CDwindow-4971CB8E7903DFAE9CBA82A6A5F05DB2', 'CDwindow-652D8829B41E67E7F7E8C2E146F8C353']

  17.异常处理

from selenium import webdriverbrower=webdriver.Chrome()brower.get('https://www.baidu.com')brower.find_element_by_id('hello')
from selenium import webdriverfrom selenium.common.exceptions import TimeoutException,NoSuchElementExceptionbrower=webdriver.Chrome()try:    brower.get('https://www.baidu.com')except TimeoutException:    print('Time Out')try:    brower.find_element_by_id('hello')except NoSuchElementException:    print('No Element')finally:    brower.close()结果:No Element

 

转载于:https://www.cnblogs.com/xiao-xue-di/p/11119623.html

你可能感兴趣的文章
Java 数组实例
查看>>
Java 方法实例
查看>>
Java 异常处理
查看>>
Java 目录
查看>>
Java 数据结构
查看>>
MYSQL5.7.24编译安装
查看>>
mysql启动过程
查看>>
ORACLE的启动过程
查看>>
ORACLE 清理SYSAUX表空间
查看>>
postgressql启动与关闭
查看>>
sqlserver数据库的启动
查看>>
浅析Kubernetes资源管理-资源预留
查看>>
机器学习在360私有云容器服务上的实践
查看>>
【Flink】数据流编程模型
查看>>
应用Python来计算排列中的逆序数个数
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
关于拷贝构造函数与赋值构造函数的深刻解析
查看>>
Sping注解:注解和含义
查看>>