WebDriver API 详解-中

WebDriver API 详解

这里列了一些WebDriver API 的详细步骤,共3部分,初级,中级,高级供参考,也可以直接使用


文章目录

  • WebDriver API 详解
  • 一、初级API详解
  • 二、中级API详解
    • 1.操作单选下拉列表-遍历所有选项并打印选项文本和选项值
    • 2.通过三种方法选择下拉列表
    • 3.断言单选列表选项值
  • 总结


一、初级API详解

二、中级API详解

1.操作单选下拉列表-遍历所有选项并打印选项文本和选项值

代码如下(示例):

#encoding=utf-8

from selenium import webdriver
import time

driver = webdriver.Chrome("D:\lupu_ui_autocation\driver\chromedriver.exe")
#被测试网页的HTML代码如下: 保存在d:\\select.html
'''


    


'''

#操作单选下拉列表

def test_print_select_text():
    global driver
    visit_uil = r"d:\\select.html"
    driver.get(visit_uil)
    select = driver.find_element_by_name('fruit') #通过name属性找到下拉列表元素
    all_options = select.find_elements_by_tag_name('option')  #获取所有标签名为option的元素,是个list
    for option in all_options:
        print('选项显示文本为:', option.text)  #获取文本
        print('选项值为:', option.get_attribute('value'))  #获取value属性
        option.click()
        time.sleep(2)
    driver.quit()
if __name__ == "__main__":
    test_print_select_text()

2.通过三种方法选择下拉列表

代码如下(示例):

#选择下拉列表元素的三种方法
#encoding=utf-8

from selenium.webdriver.support.ui import Select
from selenium import webdriver
import time

driver = webdriver.Chrome("D:\lupu_ui_autocation\driver\chromedriver.exe")

def test_operate_drop_list():
    global driver
    visit_uil = r"d:\\select.html"
    driver.get(visit_uil)
    #使用XPATH定位方式获取select页面元素对象
    select_element = Select(driver.find_element_by_xpath('//select'))
    #打印默认选中项的文本
    print(select_element.first_selected_option.text)
    #获取所有选择项的页面元素对象,是个list
    all_options = select_element.options
    print(len(all_options))   #打印选项的总数
    # is_enabled() 判断元素是否可操作  is_selected()判断元素是否被选中
    if all_options[1].is_enabled() and not all_options[1].is_selected():
        #方法一,通过序号选择元素,序号从0开始,select_by_index(1)选择第2个元素
        select_element.select_by_index(1)
        #select_element.all_selected_options[]获取所有被选中的对象组成的列表对象
        print(select_element.all_selected_options[0].text)
        assert select_element.all_selected_options[0].text =='西瓜', "断言西瓜失败"
    time.sleep(2)
    #方法二,通过选项的显示文本选择文本为‘猕猴桃’的选项
    select_element.select_by_visible_text('猕猴桃')
    assert select_element.all_selected_options[0].text =='猕猴桃','断言猕猴桃失败'

    time.sleep(2)
    #方法三,通过选项的value属性值选择value=‘shanzha’选项
    select_element.select_by_value('shanzha')
    print(select_element.all_selected_options[0].text)
    assert select_element.all_selected_options[0].text =='山楂','断言山楂失败'
    driver.quit()

if __name__ == "__main__":    
    test_operate_drop_list()

3.断言单选列表选项值

代码如下(示例):

#encoding=utf-8

from selenium.webdriver.support.ui import Select  #导入Select模块
from selenium import webdriver
import operator

driver = webdriver.Chrome("D:\lupu_ui_autocation\driver\chromedriver.exe")
#断言单选列表选项值

def test_check_select_text():
    global driver
    visit_uil = r"d:\\select.html"
    driver.get(visit_uil)
    #使用XPATH定位方式获取select页面元素对象
    select_element = Select(driver.find_element_by_xpath('//select'))
    #获取所有选择项的页面元素对象,是个list
    all_options = select_element.options
    #声明一个list对象,存储下拉列表中所期望出现的文字内容
    expect_options_list = ['桃子', '西瓜', '橘子', '猕猴桃', '山楂', '荔枝']
    #使用map()函数获取页面中下拉列表展示的选项内容组成的列表对象,转换成列表
    actual_options_list = list(map(lambda option: option.text, all_options))
    #断言期望列表与实际列表是否完全一致
    assert expect_options_list==actual_options_list,'断言相等失败'
    driver.quit()

if __name__ == "__main__":
    test_check_select_text()

总结

未完

你可能感兴趣的:(WebDriver,API,python,selenium,api)