自动化九:selenium高级API

selenium高级APIcss

driver = webdriver.Chrome(executable_path='D:\\chromedriver.exe')

#  窗口最大化web

driver.maximize_window()

# 前进与后退(安全的:就是等待页面加载完了以后才往下面继续执行)chrome

driver.back()  # 前进
driver.forward()  # 后退

# 强制刷新api

driver.refresh()

# 查看浏览器的坐标浏览器

driver.get_window_position()

# 设置浏览器的坐标安全

driver.set_window_position(200, 400)

# 获取浏览器的大小app

driver.get_window_size()
#结果:{'width': 1050, 'height': 708}

# 改变浏览器的大小ide

driver.set_window_size(100,100)

#  获取title测试

driver.title


# 获取网页原码字体

driver.page_source

# 获取当前页面的url

driver.current_url

# 获取当前窗口的句柄,# 切换句柄(切换页面)

handle = driver.current_window_handle
driver.switch_to.window(handle)

# 获取浏览器打开的全部页面的句柄

all_handles = driver.window_handles

# 关闭当前句柄(窗口)

driver.close()

# 获取元素的信息

newsElement = driver.find_element_by_xpath('//*[@id=config]')
print("元素的标签名称:", newsElement.tag_name)
print("元素的size:", newsElement.size)
print("元素的文本:", newsElement.text)
print("元素的连接:", newsElement.get_attribute('href'))
print("元素的连接:", newsElement.get_property('href'))

# 判断元素是否显示在页面上

div2 = driver.find_element_by_id("div2")
print(div2.is_displayed())

# 判断元素是否可操做

ele1 = driver.find_element_by_id('input')
ele1.is_enabled()

# 元素执行js

driver.execute_script('document.getElementById("input").disabled=false;')

# 获取输入框输入的内容

elel = driver.find_element_by_id('kw')
elel.send_keys('自动化')
print(elel.get_attribute('value'))
print(elel.get_attribute('name'))  # 获取属性name的值

# 获取输入框的长、宽、字体属性

driver.get('http:\\www.baidu.com')
elel = driver.find_element_by_id('kw')
print(elel.value_of_css_property('height'))
print(elel.value_of_css_property('width'))
print(elel.value_of_css_property('font-family'))

#  清空输入框

driver.get('http:\\www.baidu.com')
elel = driver.find_element_by_id('kw')
elel.send_keys('zidonghua')
elel.clear()  # 清空输入框

# 输入

driver.get('http:\\www.baidu.com')
elel = driver.find_element_by_id('kw')
elel.send_keys('zidonghua')  # 输入

# 单击

elel.click()

# 双击

from selenium.webdriver import ActionChains
action_Chains = ActionChains(driver)
action_Chains.double_click(elel).perform()

#  下拉框选择

# 方式一
select = driver.find_element_by_name('fruit')
all_options = select.find_element_by_tag_name("option")
for option in all_options:
    if option.text == "猕猴桃":
        option.click()
    time.sleep(2)

# 方式二
from selenium.webdriver.support.ui import Select
select_ele = Select(driver.find_element_by_xpath("//select"))
print(select_ele.first_selected_option.text)  # 打印默认选中项的文本
all_options = select_ele.options  # 获取全部选择项的页面元素对象
if all_options[1].is_enabled() and not all_options[1].is_selected():
    # 经过须要来选择第二个元素,序号从0开始
    select_ele.select_by_index(1)
    print(select_ele.all_selected_options[0].text)  # 打印已选中项的文本
# 方法二:经过选项显示的文本选择文本为“猕猴桃”选项
select_ele.select_by_visible_text("猕猴桃")
assert select_ele.all_selected_options[0].text == "猕猴桃"  # 断言选中的是不是猕猴桃

time.sleep(2)
# 方法三:经过选项的value属性值选择value="shanzha"
select_ele.select_by_value("shanzha")
assert select_ele.all_selected_options[0].text == "山楂"  # 断言选中的是个是山楂

# 断言下拉框包含的指定选项

from selenium.webdriver.support.ui import Select
select_element = Select(driver.find_element_by_xpath("//select"))
actual_options = select_element.options
expect_optionsList = ['桃子',"西瓜","橘子","猕猴桃","山楂","荔枝"]
actual_optionsList = list(map(lambda option:option.text, actual_options))
assert expect_optionsList == actual_optionsList

# 操做多选的选择列表

from selenium.webdriver.support.ui import Select
select_element = Select(driver.find_element_by_xpath("//select"))
select_element.select_by_visible_text("山楂")  # 经过文本选择“山楂”选项
select_element.select_by_value("mihoutao")  # 经过value属性值选择value="mihoutao"的选项
select_element.select_by_index(1)  # 经过index选择
for option in select_element.all_selected_options:  # 打印全部的选中项文本
    print(option.text)

select_element.deselect_by_visible_text("荔枝")  # 经过文本方式取消选项
select_element.deselect_by_index(1)  # 经过index的方式取消选项
select_element.deselect_by_value("juzi")  # 经过value属性的方式取消选项

select_element.deselect_all()  # 取消全部已选中项

# 操做能够输入的下拉列表(不支持Chrome和firefox)

from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("select").clear()  # 清空选项框
driver.find_element_by_id("select").send_keys('c')  # 输入字母,模糊查询
driver.find_element_by_id("select").send_keys(Keys.ARROW_DOWN)  # 向下移动
driver.find_element_by_id("select").send_keys(Keys.ARROW_DOWN)  # 向下移动
driver.find_element_by_id("select").send_keys(Keys.ENTER)  # 确认选择

# 单选框

berryRadio = driver.find_elements_by_xpath("//input[@value='berry']")  # 找到草莓选项
berryRadio.click()  # 点击选择草莓
assert berryRadio.is_selected()  # 断言草莓选项是否被选中
if berryRadio.is_selected():
    watermelonRadio = driver.find_elements_by_xpath("//input[@value='watermelon']")
    watermelonRadio.click()
    assert not berryRadio.is_selected()  # 由于是单选,选择了西瓜,断言判断草莓选项是不是未选中状态
time.sleep(2)

# 复选框

berryCheckBox = driver.find_elements_by_xpath('//input[@value="berry"]')
berryCheckBox.click()
assert berryCheckBox.is_selected()

if berryCheckBox.is_selected():
    berryCheckBox.click()
    assert not berryCheckBox.is_selected()

checkBoxList = driver.find_elements_by_xpath("input[@name='fruit']")
for box in checkBoxList:
    if not box.is_selected():
        box.click()

# 截屏一(selenium自带的,只截浏览器的屏幕)

result = driver.get_screenshot_as_file("D:\\baidu.png")

# 截屏二(操做系统的截屏,截取整个屏幕的)

driver.get('http:\\www.baidu.com')
from PIL import ImageGrab
im = ImageGrab.grab()
im.save("D:\\baidu.png")

# 拖拽页面元素

# 第一个元素的位置
initiablPosition = driver.find_element_by_id('draggable')
# 第二个元素的位置
targetPosition = driver.find_element_by_id('draggable2')
# 第三个元素的位置
dragElement = driver.find_element_by_id('draggable3')
from selenium.webdriver import ActionChains
"""
建立一个新的ActionChains,将webdriver实例对象driver做为参数值传入
而后经过webdriver 实例执行用户操做
"""
action_chains = ActionChains(driver)
action_chains.drag_and_drop(initiablPosition, targetPosition).perform()  # 将页面上第一个能被拖拽的元素拖拽到第二个元素的位置
for i in range(5):  # 将页面上第三个能被拖拽的袁术,向右下拖动10个像素,共拖5次
    action_chains.drag_and_drop_by_offset(dragElement, 10, 10).perform()
    time.sleep(2)

# 模拟键盘操做

driver.get('http:\\www.sogo.com')
from selenium.webdriver.common.keys import Keys
import time
# 经过ID获取搜索输入框的页面元素
query = driver.find_element_by_id('query')
query.send_keys(Keys.F12)  # 经过webdriver实例发送一个F12键
time.sleep(3)
query.send_keys(Keys.F12)  # 再次经过webdriver实例模拟发送一个F12键
query.send_keys("selenium")  # 在搜索框中,输入 selenium
query.send_keys(Keys.ENTER)  # 经过webdriver实例模拟发送一个回车键
time.sleep(3)

# 模拟组合键操做

#encoding=utf-8
import unittest
import time
import chardet
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
# 导入模拟组合按键须要的包
import win32api
import win32con
import time

VK_CODE ={
    'enter':0x0D,
    'ctrl':0x11,
    'a':0x41,
    'v':0x56,
    'x':0x58
    }

#键盘键按下
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
#键盘键抬起
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)


 
class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #启动IE浏览器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")
        
    def test_simulationCombinationKeys(self):
        url = "http://www.sogou.com"
        # 访问搜狗首页
        self.driver.get(url)
        # 找到搜索输入框元素
        searchBox = self.driver.find_element_by_id("query")
        # 将焦点切换到搜索输入框中
        searchBox.click()
        # 搜索输入框中输入“光荣之路自动化测试”
        searchBox.send_keys(u"光荣之路自动化测试")
        # 稍微等待几秒,防止太快串命令
        time.sleep(3)
        # 模拟Ctrl + a,选中输入框中全部的内容
        keyDown('ctrl')
        keyDown('a')
        # 释放Ctrl + a组合键
        keyUp('a')
        keyUp('ctrl')
        # 模拟Ctrl + x剪切所选中的内容
        keyDown('ctrl')
        keyDown('x')
        keyUp('x')
        keyUp('ctrl')
        # 访问百度首页
        self.driver.get("http://www.baidu.com")
        # 将焦点切换到搜索输入框中
        self.driver.find_element_by_id("kw").click()
        # 模拟Ctrl + v组合键,进行粘贴
        keyDown("ctrl")
        keyDown("v")
        keyUp('v')
        keyUp('ctrl')
        # 模拟回车键
        keyDown('enter')
        keyUp('enter')
        time.sleep(5)



    def tearDown(self):
        # 退出IE浏览器
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()




'''
VK_CODE = {
    'backspace': 0x08,
    'tab': 0x09,
    'clear': 0x0C,
    'enter': 0x0D,
    'shift': 0x10,
    'ctrl': 0x11,
    'alt': 0x12,
    'pause': 0x13,
    'caps_lock': 0x14,
    'esc': 0x1B,
    'spacebar': 0x20,
    'page_up': 0x21,
    'page_down': 0x22,
    'end': 0x23,
    'home': 0x24,
    'left_arrow': 0x25,
    'up_arrow': 0x26,
    'right_arrow': 0x27,
    'down_arrow': 0x28,
    'select': 0x29,
    'print': 0x2A,
    'execute': 0x2B,
    'print_screen': 0x2C,
    'ins': 0x2D,
    'del': 0x2E,
    'help': 0x2F,
    '0': 0x30,
    '1': 0x31,
    '2': 0x32,
    '3': 0x33,
    '4': 0x34,
    '5': 0x35,
    '6': 0x36,
    '7': 0x37,
    '8': 0x38,
    '9': 0x39,
    'a': 0x41,
    'b': 0x42,
    'c': 0x43,
    'd': 0x44,
    'e': 0x45,
    'f': 0x46,
    'g': 0x47,
    'h': 0x48,
    'i': 0x49,
    'j': 0x4A,
    'k': 0x4B,
    'l': 0x4C,
    'm': 0x4D,
    'n': 0x4E,
    'o': 0x4F,
    'p': 0x50,
    'q': 0x51,
    'r': 0x52,
    's': 0x53,
    't': 0x54,
    'u': 0x55,
    'v': 0x56,
    'w': 0x57,
    'x': 0x58,
    'y': 0x59,
    'z': 0x5A,
    'numpad_0': 0x60,
    'numpad_1': 0x61,
    'numpad_2': 0x62,
    'numpad_3': 0x63,
    'numpad_4': 0x64,
    'numpad_5': 0x65,
    'numpad_6': 0x66,
    'numpad_7': 0x67,
    'numpad_8': 0x68,
    'numpad_9': 0x69,
    'multiply_key': 0x6A,
    'add_key': 0x6B,
    'separator_key': 0x6C,
    'subtract_key': 0x6D,
    'decimal_key': 0x6E,
    'divide_key': 0x6F,
    'F1': 0x70,
    'F2': 0x71,
    'F3': 0x72,
    'F4': 0x73,
    'F5': 0x74,
    'F6': 0x75,
    'F7': 0x76,
    'F8': 0x77,
    'F9': 0x78,
    'F10': 0x79,
    'F11': 0x7A,
    'F12': 0x7B,
    'F13': 0x7C,
    'F14': 0x7D,
    'F15': 0x7E,
    'F16': 0x7F,
    'F17': 0x80,
    'F18': 0x81,
    'F19': 0x82,
    'F20': 0x83,
    'F21': 0x84,
    'F22': 0x85,
    'F23': 0x86,
    'F24': 0x87,
    'num_lock': 0x90,
    'scroll_lock': 0x91,
    'left_shift': 0xA0,
    'right_shift ': 0xA1,
    'left_control': 0xA2,
    'right_control': 0xA3,
    'left_menu': 0xA4,
    'right_menu': 0xA5,
    'browser_back': 0xA6,
    'browser_forward': 0xA7,
    'browser_refresh': 0xA8,
    'browser_stop': 0xA9,
    'browser_search': 0xAA,
    'browser_favorites': 0xAB,
    'browser_start_and_home': 0xAC,
    'volume_mute': 0xAD,
    'volume_Down': 0xAE,
    'volume_up': 0xAF,
    'next_track': 0xB0,
    'previous_track': 0xB1,
    'stop_media': 0xB2,
    'play/pause_media': 0xB3,
    'start_mail': 0xB4,
    'select_media': 0xB5,
    'start_application_1': 0xB6,
    'start_application_2': 0xB7,
    'attn_key': 0xF6,
    'crsel_key': 0xF7,
    'exsel_key': 0xF8,
    'play_key': 0xFA,
    'zoom_key': 0xFB,
    'clear_key': 0xFE,
    '+': 0xBB,
    ',': 0xBC,
    '-': 0xBD,
    '.': 0xBE,
    '/': 0xBF,
    '`': 0xC0,
    ';': 0xBA,
    '[': 0xDB,
    '\\': 0xDC,
    ']': 0xDD,
    "'": 0xDE,
    '`': 0xC0
}
'''
相关文章
相关标签/搜索