一、声明浏览器对象
注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入
from selenium import webdriver
#webdriver可以认为是浏览器的驱动器,要驱动浏览器必须用到webdriver,支持多种浏览器,这里以Chrome为例
browser = webdriver.Chrome()
二、访问页面并获取网页html
from selenium import webdriver
browser = webdriver.Chrome()
print(browser.page_source)#browser.page_source是获取网页的全部htmlbrowser.close()
三、查找元素
单个元素
from selenium import webdriver
browser = webdriver.Chrome()
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()
常用的查找方法
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()
input_first = browser.find_element(BY.ID,'q')#第一个参数传入名称,第二个传入具体的参数print(input_first)
browser.close()
多个元素,elements多个s
input_first = browser.find_elements_by_id('q')
四、元素交互操作-搜索框传入关键词进行自动搜索
from selenium import webdriver
import time
browser = webdriver.Chrome()
input = browser.find_element_by_id('q')#找到搜索框
input.send_keys('iPhone')#传送入关键词
time.sleep(5)
input.clear()#清空搜索框
input.send_keys('男士内裤')
button = browser.find_element_by_class_name('btn-search')#找到搜索按钮button.click()
更多操作: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement#可以有属性、截图等等
五、交互动作,驱动浏览器进行动作,模拟拖拽动作,将动作附加到动作链中串行执行
from selenium import webdriver
from selenium.webdriver import ActionChains#引入动作链
browser = webdriver.Chrome()
browser.get(url)
browser.switch_to.frame('iframeResult')#切换到iframeResult框架
source = browser.find_element_by_css_selector('#draggable')#找到被拖拽对象
target = browser.find_element_by_css_selector('#droppable')#找到目标
actions = ActionChains(browser)#声明actions对象actions.drag_and_drop(source, target)
actions.perform()#执行动作
六、执行JavaScript
有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript
from selenium import webdriver
browser = webdriver.Chrome()
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(url)
logo = browser.find_element_by_id('zh-top-link-logo')#获取网站logoprint(logo)
print(logo.get_attribute('class'))
browser.close()
获取文本值
from selenium import webdriver
browser = webdriver.Chrome()
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)#input.text文本值browser.close()
# 获取Id,位置,标签名,大小from selenium import webdriver
browser = webdriver.Chrome()
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.id)#获取idprint(input.location)#获取位置print(input.tag_name)#获取标签名print(input.size)#获取大小browser.close()
八、Frame操作
frame相当于独立的网页,如果在父类网frame查找子类的,则必须切换到子类的frame,子类如果查找父类也需要先切换
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome()
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)#等待十秒加载不出来就会抛出异常,10秒内加载出来正常返回
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()
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
详细内容:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions
十一、前进后退-实现浏览器的前进后退以浏览不同的网页
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.back()
time.sleep(1)
browser.forward()
browser.close()
十二、Cookies
from selenium import webdriver
browser = webdriver.Chrome()
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())
选项卡管理 增加浏览器窗口
import time
from selenium import webdriver
browser = webdriver.Chrome()
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
十三、异常处理
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:
except TimeoutException:
print('Time Out')
try:
browser.find_element_by_id('hello')
except NoSuchElementException:
print('No Element')
finally:
browser.close()
- selenium常用命令--操作页面元素及获取元素内容整理
selenium常用命令之操作页面元素及获取元素内容的事件整理 例子: /**id <input type="text" id="phone" name ...
- Selenium Web 自动化 - Selenium常用API
Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作 1.1 用webdriver打开一个浏览器 1.2 最大化浏览器&关闭浏览器 ...
- [Python爬虫] 之六:Selenium 常用控件用法
Selenium 常用控件用法 1.文本框 上图中,如何定位搜索文本框,并输入搜索内容进行搜索 首先:利用方法 find_element_by_xpath定位元素:inputElements = se ...
- python3+selenium常用语法汇总
Selenium常用语法总结 一.Selenium常用定位语法 1.元素定位 (1)ID定位元素: find_element_by_id(‘’) (2)通过元素的类名称定位元素: find_eleme ...
- jQuery中的常用内容总结(一)
jQuery中的常用内容总结(一) 前言 不好意思(✿◠‿◠),由于回家看病以及处理一些其它事情耽搁了,不然这篇博客本该上上周或者上周写的:同时闲谈几句:在这里建议各位开发的童鞋,如果有疾病尽快治 ...
- jQuery中的常用内容总结(三)
jQuery中的常用内容总结(三) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7571998.html 内容提要 选择器(第一节) 选择器的扩展方法(第一节) ...
- jQuery中的常用内容总结(二)
jQuery中的常用内容总结(二) 转载请注明地址: http://www.cnblogs.com/funnyzpc/p/7571993.html 前言 距离上次博客更新已经有二十来天了(●′ω`●) ...
- python基础(常用内容)
python基础(常用内容) 机器数: 一个数在计算机中的二进制表示形式就是机器数. 例如: +3用机器数表示就用<00000011>表示 -3用机器数表示就用<10000011&g ...
- selenium常用的类库、对应的方法和属性
selenium常用的类库.对应的方法和属性
随机推荐
- SpringBoot的RestController vs @ResponseBody + @Controller
@Controller和@RestController的区别?官方文档:@RestController is a stereotype annotation that combines @Respon ...
- Spark入门(1-3)Spark的重要概念
1.什么是弹性分布式数据集? Spark提出了RDD(Resilient Distributed Datasets)这么一个全新的概念,RDD弹性分布式数据集是并行.容错的分布式数据结构:可以将RDD ...
- ssh整合之二hibernate单独搭建
1.首先我们需要去拷贝我们的hibernate所需的jar包 这里还需要加入我们C3P0的jar包,因为我们hibernate中使用的C3P0连接池 2. 编写我们的关系映射文件Customer.c ...
- JavaScript作用域那些事
作用域 (1).作用域也叫执行环境(execution context)是JavaScript中一个重要的概念.执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为.在JavaScript ...
- python/基础输出输入用法
输出及输入的简单用法 print print,中文意思是打印,在python里它不是往纸上打印,而是打印在命令行,或者叫终端.控制台里面.print是python里很基本很常见的一个操作,它的操作对象 ...
- SpringMVC(四):@RequestMapping结合org.springframework.web.filter.HiddenHttpMethodFilter实现REST请求
1)REST具体表现: --- /account/1 HTTP GET 获取id=1的account --- /account/1 HTTP DELETE 删除id=1的account ...
- QT 实现在QLabel上画图
QT之所以不能再任意控件上绘图是因为QT的事件过滤器把控件的绘图事件给过滤了. 在paintevent()函数中,通常需要设置QPainter对象,创建QPainter对象的同时需要指定绘图设备,即继 ...
- POJ-1751 Highways---确定部分边的MST
题目链接: https://vjudge.net/problem/POJ-1751 题目大意: 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多 ...
- jQuery获取浏览器参数
当我们需要获取浏览器参数是,我们可以使用jQuery进行获取,具体方法如下: 例如获取参数的地址:http://www.test.com?a="111"&b="2 ...
- C# webBrowser 控件赋值
string body = PostWebRequest(txtURL.Text, textBox2.Text); if (webBrowser1.ReadyState != WebBrowserRe ...