zoukankan      html  css  js  c++  java
  • selenium基本操作

    #coding=utf-8
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait,Select
    import selenium.webdriver.support.ui as ui
    from selenium.webdriver.common.action_chains import ActionChains

    import time,os

    #打开浏览器
    driver=webdriver.Firefox()

    #关闭浏览器
    driver.quit() #关闭窗口,退出webdriver,释放与driver server之间的连接
    driver.close() #关闭当前浏览器窗口

    #最大化浏览器
    driver.maximize_window()

    #设置浏览器大小 1.容易跟基于图像对比的工具进行结合 2.不同浏览器大小下访问测试站点
    driver.set_window_size(400,600)

    #访问链接
    url="http://www.baidu.com"
    driver.get(url)

    #打印当前页的title及url
    driver.title
    driver.current_url

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

    #简单的对象定位
    driver.find_element

    #定位一组对象
    driver.find_elements()

    #层级定位,通过父元素定位到子孙元素
    menu = driver.find_element_by_id('dropdown1').find_element_by_link_text('Another action')

    #操作测试对象
    driver.find_element_by_id().click()
    driver.find_element_by_id().clear()
    driver.find_element_by_id().send_keys()

    #send keys模拟按键输入
    driver.find_element_by_id().send_keys((Keys.CONTROL,'a'))
    driver.find_element_by_id().send_keys('watir','-','webdriver',Keys.SPACE,'better')

    #处理button group
    buttons = driver.find_element_by_class_name('btn-group').find_elements_by_class_name('btn')
    for btn in buttons:
    if btn.text == 'second': print ('find second button')

    #处理button dropdown
    #1.点击下拉菜单 2.找到dropdown-menu父元素 3.找到better than
    driver.find_element_by_link_text('Info').click()
    WebDriverWait(driver,10).until(lambda the_driver: the_driver.find_element_by_class_name('dropdown-menu').is_displayed())
    menu = driver.find_element_by_class_name('dropdown-menu').find_element_by_link_text('better than')

    #处理navs,类似tab的导航栏
    # 方法1:层级定位,先定位ul再定位li
    driver.find_element_by_class_name('nav').find_element_by_link_text('About').click()
    # 方法2: 直接定位link
    driver.find_element_by_link_text('Home').click()


    #分页处理
    # 获得所有分页的数量 -2是因为要去掉上一个和下一个
    total_pages = len(driver.find_element_by_class_name('pagination').find_elements_by_tag_name('li')) - 2
    print ("total page is %s" %(total_pages))
    # 获取当前页面的url以及当前页面是第几页
    current_page = driver.find_element_by_class_name('pagination').find_element_by_class_name('active')
    print ("current page is %s" %(current_page.text))

    #处理对话框
    # 打开对话框
    driver.find_element_by_id('show_modal').click()
    wait = ui.WebDriverWait(driver, 10)
    wait.until(lambda dr: dr.find_element_by_id('myModal').is_displayed())
    # 点击对话框中的链接
    # 由于对话框中的元素被蒙板所遮挡,直接点击会报 Element is not clickable的错误
    # 所以使用js来模拟click
    # 在watir-webdriver中只需要fire_event(:click)就可以了
    link = driver.find_element_by_id('myModal').find_element_by_id('click')
    driver.execute_script('$(arguments[0]).click()', link)
    # 关闭对话框
    buttons = driver.find_element_by_class_name('modal-footer').find_elements_by_tag_name('button')
    buttons[0].click()

    #获取测试对象的属性及内容
    link = driver.find_element_by_id('tooltip')
    title=link.get_attribute('title')
    text=link.text

    #获取测试对象的css属性
    link = driver.find_element_by_id('tooltip')
    color=link.value_of_css_property('color')
    font=driver.find_element_by_tag_name('h3').value_of_css_property('font')

    #获取测试对象的状态
    #是否存在:find_element_by_xxx捕获异常
    try:
    driver.find_element_by_id('none')
    except:
    print ('element does not exist')
    #是否被选中:element.is_selected()
    radio = driver.find_element_by_name('radio')
    radio.click()
    radio.is_selected()
    #是否灰化;element.is_enabled()
    text_field = driver.find_element_by_name('user')
    text_field.is_enabled()
    #是否显示 :element.is_displayed()
    driver.execute_script('$(arguments[0]).hide()', text_field)
    text_field.is_displayed()

    #处理表单元素
    #使用send_keys方法往多行文本框和单行文本框赋值;
    #使用click方法选择checkbox
    #使用click方法选择radio
    #使用click方法点击button
    #使用click方法选择option,从而达到选中select下拉框中某个具体菜单项的效果

    #执行js
    # 在页面上直接执行js
    driver.execute_script('$("#tooltip").fadeOut();')
    # 在已经定位的元素上执行js
    button = driver.find_element_by_class_name('btn')
    driver.execute_script('$(arguments[0]).fadeOut()', button)

    #处理alert/confirm/prompt
    # 点击链接弹出alert
    driver.find_element_by_id('tooltip').click()
    try:
    alert = driver.switch_to_alert()
    #alert.send_keys() 如果是prompt
    alert.accept()
    #alert.dismiss()
    except:
    print ('no alerts display')

    #wait
    # 等待页面完成某些操作
    driver.find_element_by_id('btn').click()
    wait = ui.WebDriverWait(driver, 10)
    wait.until(lambda dr: dr.find_element_by_class_name('label').is_displayed())

    #定位frame中的元素
    # 先到f1再到f2
    driver.switch_to_frame('f1')
    driver.switch_to_frame('f2')
    # 往f2中的百度关键字文本框中输入内容
    driver.find_element_by_id('kw').send_keys('watir-webdriver')
    # 直接跳出所有frame
    driver.switch_to_default_content()
    # 再到f1
    driver.switch_to_frame('f1')
    driver.find_element_by_link_text('click').click()

    #action
    #key_down。模拟按键按下
    #key_up。模拟按键弹起
    #click
    #send_keys
    #double_click。鼠标左键双击
    #click_and_hold。鼠标左键点击住不放
    #release。鼠标左键弹起,可以与click_and_hold配合使用
    #move_to_element。把鼠标移动到元素的中心点
    #content_click。鼠标右键点击
    #drag_and_drop。拖拽
    element = driver.find_element_by_link_text('xxxxx')
    hov = ActionChains(driver).move_to_element(element)
    hov.perform()

    #上传文件,找到上传文件的对象
    driver.find_element_by_name('file').send_keys('./upload_file.md')

    #下载
    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList",2)
    fp.set_preference("browser.download.manager.showWhenStarting",False)
    fp.set_preference("browser.download.dir", os.getcwd())
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")
    browser = webdriver.Firefox(firefox_profile=fp)
    browser.get("http://pypi.python.org/pypi/selenium")
    browser.find_element_by_partial_link_text("selenium-2").click()

    #超时设置,implicit_wait
    ff = webdriver.Firefox()
    ff.implicitly_wait(10) # seconds

    #cookie实现自动登录
    driver.get_cookies()
    driver.delete_all_cookies()
    driver.add_cookie({'name': 'BAIDUID', 'value': 'xxxxxx'})
    driver.add_cookie({'name': 'BDUSS', 'value': 'xxxxxx'})
    driver.get(url)

    #定位select > option*x 结构元素
    #1
    driver.find_element_by_id("aaa").find_elements_by_tag_name("option")[1].click();
    #2
    driver.find_element_by_xpath("//select[@id='aaa']").find_element_by_xpath("//option[@value='5PM']").click()
    #3
    select = Select(driver.find_element_by_id("aaa"))
    select.deselect_all()
    select.select_by_visible_text("Edam")


    #css定位 优点:定位速度比xpath快, 语法更简洁
    #1.使用class属性定位,先指定一个Html标签,然后加一个“.'加上class的值
    driver.find_element_by_css_selector(”input.login")
    #2.使用id属性定位,先指定一个Html标签,然后加一个“#'加上id的值
    driver.find_element_by_css_selector(”input#login")
    #3.使用name或alt属性定位
    driver.find_element_by_css_selector(”input[name=login]")
    driver.find_element_by_css_selector(”input[alt=login]")
    #4.当属性不足时,可以使用多个属性来定位
    driver.find_element_by_css_selector(”input[type='submit'][value='login']")

    #5.使用属性名称选择器定位元素
    driver.find_element_by_css_selector(”img[alt]"
    driver.find_element_by_css_selector(”img:not[alt]")


    #6.使用属性名称匹配
    driver.find_element_by_css_selector(”img[alt]")

    driver.find_element_by_css_selector(”img:not[alt]")
    #7.部分属性值的匹配
    driver.find_element_by_css_selector(”input[id^='ctrl']") #id以ctrl开头的元素
    driver.find_element_by_css_selector(”input[id$='ctrl']") #id以ctrl结尾的元素
    driver.find_element_by_css_selector(”input[id*='ctrl']") #id包含ctrl的元素

    #xpath定位
    #1.使用相对路径
    driver.find_element_by_xpath(”//input")
    #2.使用索引
    driver.find_element_by_xpath(”//input【2】")
    #3.使用xpath及属性值
    driver.find_element_by_xpath(”//input[@id='username']")
    driver.find_element_by_xpath(”img[@alt='username']")
    driver.find_element_by_xpath(”//input[@type='submit'][@value='login']")
    driver.find_element_by_xpath(”//input[@type='submit' and @value='login']")
    #4.部分属性值匹配
    driver.find_element_by_xpath(”//input[starts-with(@id,'ctrl')]")
    driver.find_element_by_xpath(”//input[ends-with(@id,'ctrl')]")
    driver.find_element_by_xpath(”//input[contains(@id,'ctrl')]")

    #5.使用值来匹配任意属性及元素
    driver.find_element(By.xpath("//input[@*='username']"))

    #6.使用xpath轴来定位元素
    选择当前节点所有的父类元素,包括祖先元素
    ancestor:driver.find_element(By.xpath("//td[text()='Product']/ancestor::table"))
    选择当前节点所有子元素
    descendant:driver.find_element(By.xpath("//table/descendant::td/input"))
    选择当前元素结束标签后的所有元素
    following:driver.find_element(By.xpath("//td[text()="Product"]/following::tr"))
    选择当前元素后的兄弟元素
    following-sibling:driver.find_element(By.xpath("//td[text()="Product"]/following-sibling::td"))
    选取文档中当前节点的开始标签之前的所有节点
    preceding:driver.find_element(By.xpath("//td[text()="Product"]/preceding::tr"))
    选择当前节点之前的所有同级节点
    preceding-sibling:driver.find_element(By.xpath("//td[text()="Product"]/preceding-sibling::td"))
    
    
    #7.使用text()函数
    driver.find_element(By.xpath("//td[contains(text(),'Item')]"))
    使用精确文本定位
    driver.find_element(By.xpath("//td[.='Item']))


     
     







     
    
    
    
    
     
    
    
     
     
     
     





     

     
     
     



    
    
     
     
     
     
     
     
  • 相关阅读:
    系统架构设计师软考总结
    统一用户认证系统CUAS实现要点
    Activiti使用总结
    性能优化总结篇
    CVE-2016-5734-phpmyadmin-4.0.x-4.6.2-代码执行
    WooYun-2016-199433 -phpmyadmin-反序列化-getshell
    CVE-2015-1635-HTTP.SYS远程执行代码
    泛微OA 多版本存在命令执行
    CVE-2019-20372-Nginx error_page 请求走私
    通达OA 页面敏感信息-2013/2015版本
  • 原文地址:https://www.cnblogs.com/quxikun/p/7251350.html
Copyright © 2011-2022 走看看