您好,欢迎来到知库网。
搜索
您的当前位置:首页Python webdriver API 总结

Python webdriver API 总结

来源:知库网

1、浏览器最大化

driver.maximize_window()

2、设置浏览器的宽和高

driver.set_window_size(400,800)

3、控制浏览器前进和倒退

driver.forward()

driver.back()

4、简单元素的定位

以下方法返回的结果都是列表

find_element_by_id()----------------------------------根据标签的id定位

find_element_by_name()-----------------------------根据标签的name定位

find_element_by_class_name()--------------------根据class name定位

find_element_by_tag_name()-----------------------根据标签的名字定位,重复率高,不好定位

find_element_by_link_text()--------------------------根据文字链接定位,文字全部匹配

find_element_by_partial_link_text()----------------根据文字链接定位,文字部分匹配

find_element_by_xpath()------------------------------根据xpath定位

find_element_by_css_selector()--------------------根据CSS选择器定位

5、登录常用方法

      clear ---------------------------------------清除元素的内容,如果可以的话

      send_keys------------------------------- 在元素上模拟按键输入 

      click ----------------------------------------单击元素

      submit -------------------------------------提交表单

6、webelement常用方法

size-------------返回元素的尺寸

size = driver.find_element_by_id("kw").size

text---------------获取元素的文本

text=driver.find_element_by_id("cp").text

get_attribute(name)------------------获得属性值

attr=driver.find_element_by_id("kw").get_attibute('type')

is_displayed()---------------------------------设置该元素是否用户可见,返回结果为FALSE或TRUE

result=driver.find_element_by_id("kw").is_displayed()

7、鼠标事件ActionChains类

引入的包

ActionChains的执行原理

当调用ActionChains方法的时候不会立即执行,而是将所有的操作暂时存储在一个队列中,当调用perform()的方法时候,队列会按照放入的先后顺序依次执行。

ActionChains的书写方法

ActionChains的方法

1)鼠标点击

context_click(rightButtonElement)----------------------------->鼠标右击

ActionChains(driver).context_click(rightClickElement).perform()

double_click(doubleClickElement)------------------------------>双击

ActionChains(driver).double_click(doubleClickElement).perform()

click(clickElement)------------------------------------------------------>单击

ActionChains(driver).click(clickElement).perform()

click_and_hold----------------------------------------------------------->按住左键不动

ActionChains(driver).click_and_hold(clickholdelement).perform()

2)鼠标移动

move_by_offset(xoffset,yoffset)-------------------------------------->鼠标移动到距离当前位置(x,y)

ActionChains(driver).move_by_offset(xoffset,yoffset).perform()

move_to_element(moveElement)---------------------------------->鼠标移动到某个元素

ActionChains(driver).move_to_element(moveElement).perform()

3)鼠标拖动

drag_and_drop(dragElement,targetElement)------------------------->将鼠标拖动到某个元素然后放开

drag_and_drop_by_offset(source,xoffset,yoffset)----------------->将鼠标拖动到某个坐标然后放开

8、键盘事件Keys类

引入的包

常用方法

1)组合键

send_keys(Keys.CONTROL,'a')          #全选

send_keys(Keys. CONTROL,'c')          #复制

send_keys(Keys. CONTROL,'v')          #粘贴

send_keys(Keys. CONTROL,'x')          #剪切

2)非组合键

send_keys(Keys.ENTER)                  #回车键

send_keys(Keys.BACK_SPACE)   #删除键

send_keys(Keys.SPACE)                 #空格键

send_keys(Keys.TAB)                       #制表符

send_keys(Keys.ESCAPE)              #回退键

send_keys(Keys.F5)                           #刷新

9、打印信息

1)获取当前页面的title

title=driver.title

2)获取当前加载页面的URL

url=driver.current_url

10、设置等待时间

1)强制等待

import time

time.sleep(5)    #括号内的是秒数,设置固定休眠时间

2)隐形等待

driver.implicitly(10)

隐的等待一个元素被发现,或一个命令完成如果超出了设置时间的则抛出异常。

Implicit Waits默认是等待时间是0,同时隐性等待是对driver起作用,所以只要设置一次即可,没有必要到处设置

3)显性等待

WebDriverWait()

引入包:

from selenium.webdriver.support.ui import WebDriverWait

用法:

element=WebDriverWait(driver, 10).until(lambda driver :driver.find_element_by_id("kw"))

11、定位一组对象

find_elements_by

12、浏览器多窗口处理

1)current_window_handle获得当前窗口句柄

nowhandle=driver.current_window_handle

2)window_handles获得所有窗口句柄

handles=driver.window_handles

3)switch_to_window(handle)切换窗口

driver.switch_to_window(nowhandle)

4)close()关闭当前窗口

driver.close()

13、处理js弹出对话框alert、confirm和prompt

处理思路:

通过switch_to_alert()定位到对话框,使用text、accept、dismiss、send_keys()操作对话框

1)处理alert对话框

alert=switch_to_alert()     #切换到alert对话框

print(alert.text)                 #输出alert对话框的文本信息

alert.accept()                   #点击确定

2)处理confirm对话框

confirm=switch_to_alert()    #切换到confirm对话框

print(confirm.text)                #输出confirm对话框的文本信息

confirm.accept()                  #点击确定

confirm.dismiss()                 #点击取消 

3)处理prompt对话框(可输入)

prompt=switch_to_alert()    #切换到confirm对话框

print(prompt.text)                #输出confirm对话框的文本信息

prompt.accept()                  #点击确定

prompt.dismiss()                  #点击取消

prompt.send_keys("2)         #输入框输入2

14、下拉菜单处理

思路:

首先定位到下拉菜单,再定位到选项options,再对选项做操作

selenium 很人性化的给提供了一个Select的模块,供处理下来菜单,首先我们需要导入Select,通过from selenium.webdriver.support.select import Select来导入。

Select中提供几个用于定位的option的方法

1)选择列表(常用方法)

select_by_index(self,index)

根据index属性查找匹配元素并选择,index从0开始

S = Select(driver.find_element_by_name('cars')).select_by_index(0)

#实例化Select,按索引选择option。索引以0开始

select_by_value(self,value)

根据value属性查找匹配元素并选择

select_by_text(self,text)

根据text文本值查找匹配元素并选择

first_selected_option

选择第一个option选项

2)清除列表

deselect_by_index(self, index)              #以index属性值来查找匹配的元素并取消选择;

deselect_by_value(self, value)              #以value属性值来查找该option并取消选择;

deselect_by_visible_text(self, text)   #以text文本值来查找匹配的元素并取消选择;

deselect_all(self)                                    #将所有选择清除;

3)选项

options    #以列表形式返回属于此select标签的所有option

options = driver.find_element_by_id("cars").options  #获取所有选项

for option in options:

         print(option.text)

all_selected_options #全部选择了的option的列表

first_selected_option #第一个被选中的option元素如果select没有multiple值,此时获取值为当前选择的option

15、上传和下载

1)上传

思路:如果下载标签是input类型的,首先定位到下载元素,点击,然后输入文件保存路径

driver.find_element_by_xpath(upload).send_keys(filepath)

2)下载

chrome下载

Firefox下载

16、调用js

execute_script(script, *args)

在当前窗口/框架同步执行javaScript

17、控制浏览器滚动条

1)chrome

将页面滚动条拖到底部

js = "var q=document.body.scrollTop=10000"

driver.excute_script(js)

将页面滚动条拖到顶部

js = "var q=document.body.scrollTop=0"

driver.excute_script(js)

2)非chrome

将页面滚动条拖到底部

js = "var q=document.documentElement.scrollTop=10000"

driver.excute_script(js)

将页面滚动条拖到顶部

js = "var q=document.documentElement.scrollTop=0"

driver.excute_script(js)

18、cookie处理

 webdriver操作cookie的方法:

get_cookies()

cookies=driver.get_cookies(0

get_cookie(name)

add_cookie(cookie_dict)

delete_cookie(name)

delete_all_cookies()

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务