浏览器

1.火狐浏览器

br = webdriver.Firefox()
#最大化窗口
br.maximize_window()
br.get('http://baidu.com')

  

2.谷歌浏览器

br = webdriver.Chrome()
#最大化窗口
br.maximize_window()
br.get('http://baidu.com')

  

3.谷歌浏览器并且设置指定的下载目录,后面断言是否下载到本地

options=webdriver.ChromeOptions()
path=os.path.abspath("..")#表示当前所处的文件夹上一级文件夹的绝对路径
filepath=path+"\\PullFile"
prefs={'profile.default_content_settings.popups':0,'download.default_directory':filepath}
options.add_experimental_option('prefs',prefs) br=webdriver.Chrome(chrome_options=options)
br.maximize_window()
br.get(http://www.baidu.com)

  

4.PhantomJS浏览器,无界面的执行用例~

br = webdriver.PhantomJS()
br.maximize_window()
br.get("http://www.baidu.com")

  

4.1 谷歌无头浏览器,相当于PhantomJS

            chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu')
driver=webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()

  

操作

1.点击动作

driver.find_element_by_id(Location_element).click()
driver.find_element_by_name(Location_element).click()
driver.find_element_by_xpath(Location_element).click()
driver.find_element_by_css_selector(Location_element).click()

2.输入动作,也可以作为上传文件使用

driver.find_element_by_id(Location_element).send_keys(Input_content)
driver.find_element_by_name(Location_element).send_keys(Input_content)
driver.find_element_by_xpath(Location_element).send_keys(Input_content)
driver.find_element_by_css_selector(Location_element).send_keys(Input_content)

 

3.选择select下拉框的值, Input_content给下拉框option的value值

Select(driver.find_element_by_xpath(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_name(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_id(Location_element)).select_by_value(Input_content)
Select(driver.find_element_by_css_selector(Location_element)).select_by_value(Input_content)

  

4.强制等待

time.sleep(2)

  

5.智能等待,一直等元素出来再操作, 30是设置的超时时间

driver.implicitly_wait(30)

  

6.悬浮操作

_ele_key = driver.find_element_by_id(Location_element)  #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_name(Location_element) #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_xpath(Location_element) #目标元素
ActionChains(driver).move_to_element(_ele_key).perform() #悬浮 _ele_key = driver.find_element_by_css_selector(Location_element)#目标元素
ActionChains(driver).move_to_element(_ele_key).perform()#悬浮

  

7.js弹窗确认/取消/输入值确认或取消

        #  js弹窗确认/取消/输入值确认或取消   Location_element指定义的操作,Input_content指需要输入的文本
def _prompts_js_key(self,driver,Location_element, Input_content): # 拿到页面alert
dialog_box = driver.switch_to_alert()
time.sleep(2)
# 这个判断给需要输入的提示框
if Location_element == u'确认' and Input_content != '':
text = str(Input_content)
dialog_box.send_keys(Input_content)
time.sleep(2)
dialog_box.accept()
time.sleep(2) if Location_element == u'取消' and Input_content != '':
dialog_box.dismiss()
time.sleep(2) #这两个判断是单纯只有确认 取消或确认的提示床
elif Location_element == u'确认' and Input_content == '':
dialog_box.accept()
time.sleep(2) elif Location_element == u'取消' and Input_content == '':
dialog_box.dismiss()
time.sleep(2) else:
pass

  

8.Autoit通过执行exe上传文件

paths=test._Autoit_file(exe_path)
time.sleep(1)
os.system(paths)
time.sleep(1)

  

9.切入iframe  #给iframe的元素(支持id name xpath...)

time.sleep(1)
driver.switch_to.frame(driver.find_element_by_xpath(Location_element)) #给iframe的元素
time.sleep(1)

  

10.切出iframe

driver.switch_to_default_content()

 

11.断言 预期和实际对比下

        def _find_value_key(self,driver,Positioning_mode,Location_element,Input_content,output):

            if Positioning_mode == self.Location_xpath:
#取实际元素的值
value = driver.find_element_by_xpath(Location_element).text
value = value.encode("utf-8")
value = str(value)
# text=str(text)
test=StringManipulation
Input_content = test._filter_value(Input_content) if isinstance(Input_content, int):
Input_content = str(Input_content)
Input_content = Input_content + 'X'
value = value + 'X' if isinstance(Input_content, float):
Input_content = str(Input_content)
Input_content = Input_content + 'X'
value = value + 'X' text = Input_content.encode("utf-8")
text = str(text) if value == text:
print(output,"-----TRUE")
ActualResults=('True')
return ActualResults
else:
print '--------------------------------------'
print ('预期结果',text)
print ('实际结果',value)
print('')
print ('实际结果和预期不匹配')
print '--------------------------------------'
driver.quit()
ActualResults='False'
return ActualResults
else:
print("find目前只用xpath定位方式")

  

12.回车操作

driver.find_element_by_id(Location_element).send_keys(Keys.ENTER)

  

13.清除文本框值操作

driver.find_element_by_id(Location_element).clear()

  

14.写个方法,在下载文件后,判断又没有下载到本地

# 得到目录文件
def file_name(self, file_dir):
for root, dirs, name in os.walk(file_dir):
return name # 查看是否有文件
def list_none(self, value):
if value:
return 'True'
else:
return 'False' # 断言是否下载文件成功
def find_file_key(self,driver,output): test_exc=StringManipulation._pull_file() files = self.file_name(test_exc)
list_value = self.list_none(files) if list_value == 'True': filename = test_exc + files[0]
os.remove(filename)
print(output,"-----TRUE")
ActualRresults='True'
print '下载成功,文件名是', files[0], '因为需要初始化,正在删除此文件.....'
return ActualRresults else:
print '指定的目录没有查询到下载的文件' ActualResults='False'
driver.quit()
return ActualResults

  

15.当时间选择框不可输入,那么改下js的写进去

        # 当时间控件不可输入时,需要用js去除removeAttribute属性再把值写进去
#Positioning_mode==定位方式,Location_element==元素,Input_content输入内容(时间)
def _time_js_input(self,driver,Positioning_mode,Location_element,Input_content): time.sleep(1)
elements=r"'"+Location_element+"'"
if Positioning_mode==self.Location_id:
jsa = "document.getElementById("+elements+").removeAttribute('readonly')"
driver.execute_script(jsa)
if isinstance(Input_content,float):
Input_content=int(Input_content)
Input_content=str(Input_content)
driver.find_element_by_id(Location_element).send_keys(Input_content)
time.sleep(1)
else:
driver.find_element_by_id(Location_element).send_keys(Input_content)
time.sleep(1)
else:
print('对于时间空间暂时先写id方法,后面用到其他在Underlyingkeyword.py维护')

  

后续有其他操作再补吧~

[python]selenium常用的操作的更多相关文章

  1. python 历险记(三)— python 的常用文件操作

    目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...

  2. Python之常用文件操作

    Python之常用文件操作

  3. selenium常用命令--操作页面元素及获取元素内容整理

    selenium常用命令之操作页面元素及获取元素内容的事件整理 例子:  /**id <input type="text" id="phone" name ...

  4. selenium - 常用元素操作

    # 3.常用元素操作 # 元素对象的获取ele = driver.find_element_by_XXX('定位表达式') # 获取元素的文本内容(返回值为元素的文本)ele.text # 获取元素的 ...

  5. selenium - 常用页面操作

    # 2.常用页面操作 # 访问某一个页面url = 'http://www.baidu.com'driver.get(url) # 获取页面的标题title = driver.titleprint(t ...

  6. python selenium常用基本方法---H5和键盘鼠标操作

    一.模拟手机打开页面(H5测试) from selenium import webdriver mobile_emulation = {'deviceName':'iPhone X'} options ...

  7. python selenium鼠标键盘操作(ActionChains)

    用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件--ActionChains sele ...

  8. python selenium模拟滑动操作

    selenium.webdriver提供了所有WebDriver的实现,目前支持FireFox.phantomjs.Chrome.Ie和Remote quit()方法会退出浏览器,而close()方法 ...

  9. selenium - 常用等待操作

    # 4. 等待操作 # 强制等待from time import sleepsleep(10) # 隐性等待# 设置最长等待时间,在这个时间在只要有个时间点加载完成,则执行下一步代码,比sleep智能 ...

随机推荐

  1. TI AM335x ARM Cortex-A8工业级核心板,工业网关、工业HMI等用户首选

    创龙科技近期推出了ti AM335x ARM Cortex-A8工业级核心板,它拥有高性能.低功耗.低成本.接口丰富等优势,成为了工业网关.工业HMI等用户的首要选择.另外,核心板采用邮票孔连接方式, ...

  2. 踩坑系列《六》Spring增加事务处理遇到异常解决办法

    当在对数据进行增删改操作时,需要用到事务的处理,所以在业务层中加入@Transactional注解,但是在执行业务操作的前面遇到异常,因此对异常进行抛出,但是数据又诡异地成功保存到数据库了. 解决方法 ...

  3. Tomcat各种日志的关系与catalina.out文件的分割

    Tomcat 各日志之间的关系 一图胜千言! 其他日志如localhost.{yyyy-MM-dd}.log.localhost-access.{yyyy-MM-dd}.log是context的名称, ...

  4. Go语言核心36讲(Go语言基础知识四)--学习笔记

    04 | 程序实体的那些事儿(上) 还记得吗?Go 语言中的程序实体包括变量.常量.函数.结构体和接口. Go 语言是静态类型的编程语言,所以我们在声明变量或常量的时候,都需要指定它们的类型,或者给予 ...

  5. FastAPI 学习之路(十一)请求体 - 嵌套模型

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  6. 学了ES6,还不会Promise的链式调用?🧐

    前言 本文主要讲解promise的链式调用的方法及其最终方案 应用场景 假如开发有个需求是先要请求到第一个数据,然后根据第一个数据再去请求第二个数据,再根据第二个数据去请求第三个数据...一直到最后得 ...

  7. 2021.10.7考试总结[NOIP模拟71]

    信心赛,但炸了.T3SB错直接炸飞,T4可以硬算的组合数非要分段打表求阶乘..T2也因为一个细节浪费了大量时间.. 会做难题很好,但首先还是要先把能拿的分都拿到. T1 签到题 结论:总可以做到对每个 ...

  8. 2021.8.14考试总结[NOIP模拟39]

    T1 打地鼠 全场就俩人没切,还有一个是忘关$freopen$了. $code:$ 1 #include<bits/stdc++.h> 2 #define rin register sig ...

  9. mbps和MB/s是怎么换算的

    Mbps即"传输速率",也叫"带宽".去营业厅开网线的时候会问开几兆的宽带,这里说的"几兆的宽带"就是指多少Mbps,但是Mbps和MB/s ...

  10. 前端大牛带你了解JavaScript 函数式编程

    前言 函数式编程在前端已经成为了一个非常热门的话题.在最近几年里,我们看到非常多的应用程序代码库里大量使用着函数式编程思想. 本文将略去那些晦涩难懂的概念介绍,重点展示在 JavaScript 中到底 ...