zoukankan      html  css  js  c++  java
  • selenium_python学习

    虫师selenium_python 学习:原文档 下载:http://download.csdn.net/detail/fnngj/6041329

    具体参看selenium_python API : http://selenium.googlecode.com/git/docs/api/py/api.html#common

    #coding=utf-8
    '''
    Created on 2014-5-30

    @author: jyp
    '''
    from selenium import webdriver
    import os
    import time
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.action_chains import ActionChains

    '''
    元素定位,添加等待时间,浏览器操作,操作测试对象
    from selenium import webdriver
    import time
    driver = webdriver.Firefox()    
    driver.get("http://www.baidu.com")

    driver.maximize_window()
    #driver.set_window_size(1000, 800)
    #driver.set_window_position(22, 33)
    # 通过id,可以
    driver.find_element_by_id('kw1').send_keys("selenium")

    # 可以
    #driver.find_element_by_xpath("//input[@id='kw1']").send_keys("selenium")

    # 可以
    #driver.find_element_by_link_text("贴 吧").click()

    # 可以
    #driver.find_element_by_partial_link_text("贴").click()
    driver.implicitly_wait(2)
    time.sleep(5)
    # 不可以
    #driver.find_element_by_name("wd").send_keys("selenium")
    # 清空form内容
    driver.find_element_by_id("kw1").clear()
    # 不可以
    #driver.find_element_by_tag_name("input").send_keys("selenium")

    # 可以
    #driver.find_element_by_css_selector("#kw1").send_keys("selenium")
    #driver.find_element_by_class_name("btn").click()
    # 通过click
    driver.find_element_by_id('su1').click()
    # 通过submit
    driver.find_element_by_id('su1').submit()

    time.sleep(10)
    print '后退'
    driver.back()
    time.sleep(10)
    print '前进'
    driver.forward()

    '''
    '''
    键盘事件模拟
    from selenium import webdriver
    import time

    driver = webdriver.Firefox()
    url = "https://passport.csdn.net/account/login"
    driver.get(url)
    time.sleep(5)
    username = driver.find_element_by_id("username")
    # 清除表单原有信息
    #username.clear()
    username.send_keys(Keys.TAB)
    driver.implicitly_wait(5)
    username.send_keys("956180488@qq.com")
    driver.implicitly_wait(5)
    pswd = driver.find_element_by_id("password")
    pswd.send_keys("123456")
    driver.implicitly_wait(5)
    submit = driver.find_element_by_class_name('logging')
    #submit.click()
    submit.send_keys(Keys.ENTER)

    '''
    '''
    # 模拟键盘组合应用
    from selenium import webdriver
    import time

    driver = webdriver.Firefox()
    url = 'http://www.baidu.com'
    driver.get(url)
    driver.maximize_window()
    keywords = driver.find_element_by_id("kw1")
    keywords.send_keys('selenim')
    time.sleep(5)
    keywords.send_keys(Keys.CONTROL, 'a')
    time.sleep(5)
    keywords.send_keys(Keys.CONTROL, 'x')
    time.sleep(5)
    keywords.send_keys(u'姬永攀')
    time.sleep(5)
    clickButton = driver.find_element_by_id('su1')
    clickButton.send_keys(Keys.ENTER)
    time.sleep(10)
    '''

    '''
    # 模拟鼠标事件
    # 右键单击事件

    from selenium import webdriver
    import time

    driver = webdriver.Firefox()
    url = "https://passport.csdn.net/account/login"
    driver.get(url)
    time.sleep(5)
    username = driver.find_element_by_id("username")
    # 清除表单原有信息
    username.clear()
    driver.implicitly_wait(5)
    username.send_keys("956180488@qq.com")
    driver.implicitly_wait(5)

    pswd = driver.find_element_by_id("password")

    # 右键单击鼠标
    rightclick = ActionChains(driver)
    rightclick.context_click(pswd).perform()

    # 鼠标双击事件 doubleclick(element)
    # 鼠标拖放事件 drag_and_drop(element, target) // element ,target 分别为原位置和目标位置
    '''
    '''
    # 定位一组元素
    from selenium import webdriver
    import os
    import time

    driver = webdriver.Firefox()
    #file_path = 'file:///' + os.path.abspath('test.html')
    file_path = 'file:///C:/Users/jyp/Desktop/test.html'
    #os.path.abspath('C:/Users/jyp/Desktop/test.html')
    driver.get(file_path)
    # 方法一
    #inputs = driver.find_elements_by_tag_name('input')
    #for input in inputs:
    #    if input.get_attribute('type') == 'checkbox':
    #        input.click()
    #    time.sleep(5)

    # 方法二
    checkboxs = driver.find_elements_by_css_selector('input[type=checkbox]')
    for checkbox in checkboxs:
        checkbox.click()
        time.sleep(3)
    print len(checkboxs)
    # 最后一个chcecbox去掉
    checkboxs.pop().click()
    print len(checkboxs)
    time.sleep(3)
    '''
    '''
    # 多层框架定位 (frame)
    driver = webdriver.Firefox()
    url = "file:///C:/Users/jyp/Desktop/test.html"
    driver.get(url)
    driver.implicitly_wait(10)
    driver.switch_to_frame('f1')
    driver.switch_to_frame('f2')
    driver.find_element_by_id('kw1').send_keys(u"姬永攀")
    time.sleep(5)
    driver.find_element_by_id('su1').click()
    time.sleep(3)
    driver.quit()
    '''

    '''
    # 层级定位
    driver = webdriver.Firefox()
    url = "file:///C:/Users/jyp/Desktop/test.html"
    driver.get(url)
    link1 = driver.find_element_by_link_text('Link1')
    link1.click()
    WebDriverWait(driver, 10).until(lambda the_driver: the_driver.find_element_by_id('dropdown1').is_displayed(), 'link1 displayed')
    menu1 = driver.find_element_by_id('dropdown1')
    item1 = menu1.find_element_by_link_text('Action')

    webdriver.ActionChains(driver).move_to_element(item1).perform()
    time.sleep(10)
    driver.quit()
    '''
    '''
    # 文件上传功能
    driver = webdriver.Firefox()
    url = "file:///C:/Users/jyp/Desktop/test.html"
    driver.get(url)
    upload = driver.find_element_by_name('file')
    upload.send_keys("D:\vmlinuz")
    time.sleep(10)
    driver.quit()
    '''


    '''
    # 邮箱上传文件功能////////////未成功,好难定位
    driver = webdriver.Firefox()
    url = "http://mail.163.com/"
    driver.get(url)
    driver.maximize_window()
    time.sleep(3)
    username = driver.find_element_by_id('idInput')
    username.clear()
    username.send_keys("yongpan66")
    time.sleep(3)
    pswd = driver.find_element_by_id('pwdInput')
    pswd.send_keys('pan0211')
    submit1 = driver.find_element_by_id('loginBtn')
    submit1.click()

    driver.implicitly_wait(20)
    writeletter = driver.find_element_by_xpath('/html/body/div/nav/div/ul/li[2]/span[2]')

    writeletter.click()
    print 'click write letter'
    time.sleep(5)
    #receiver = driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/div/section/header/div/div/div/div[2]')
    #receiver = driver.find_element_by_xpath('//input[@id=_mail_emailinput_2_388]')
    #receiver.send_keys('956180488@qq.com')
    time.sleep(5)
    theme = driver.find_element_by_class_name('nui-editableAddr-ipt')
    theme.send_keys('test selenium upload')
    time.sleep(5)
    addfile = driver.find_element_by_class_name('O0')
    addfile.send_keys('testupload.txt')

    driver.switch_to_frame("APP-editor-iframe")
    time.sleep(5)
    lettercontent = driver.find_element_by_class_name('nui-scroll')
    lettercontent.send_keys(u'你好')
    time.sleep(10)
    sendletter = driver.find_element_by_id('_mail_icon_47_193')
    sendletter.click()
    time.sleep(30)
    driver.quit()
    '''

    '''
    # 下拉框选择
    driver = webdriver.Firefox()
    url = "file:///C:/Users/jyp/Desktop/test.html"
    driver.get(url)
    driver.maximize_window()
    time.sleep(5)
    select1 = driver.find_element_by_id('ShippingMethod')
    item1 = select1.find_element_by_xpath('//option[@value=9.03]')
    item1.click()
    '''
    '''
    # 下拉框选择, 切换到弹出框(警告框)
    driver = webdriver.Firefox()
    url = "http://www.baidu.com"
    driver.get(url)
    driver.maximize_window()
    driver.find_element_by_link_text(u'搜索设置').click()
    select1 = driver.find_element_by_id('nr')
    time.sleep(5)
    select1.find_element_by_xpath('//option[@value=20]').click()
    time.sleep(5)
    driver.find_element_by_xpath('//input[@value="保存设置"]').click()
    time.sleep(5)
    driver.switch_to_alert().accept()
    time.sleep(5)
    driver.find_element_by_id('kw1').send_keys(u'陈阳sb')
    time.sleep(5)
    driver.find_element_by_id('su1').click()
    time.sleep(10)
    driver.quit()
    '''


    '''
    # 执行js脚本
    driver = webdriver.Firefox()
    driver.get("http://email.163.com/#from=163nav_icon")
    #给用户名的输入框标红
    js="var q=document.getElementById("userNameIpt");q.style.border="1px solid red";"
    #调用 js
    driver.execute_script(js)
    time.sleep(3)
    driver.find_element_by_id("userNameIpt").send_keys("username")
    driver.find_element_by_id("pwdInput").send_keys("password")
    driver.find_element_by_id("btnSubmit").click()
    time.sleep(3)
    driver.quit()
    '''

    '''
    # 控制浏览器滚动条
    #访问百度
    driver=webdriver.Firefox()
    driver.get("http://www.baidu.com")
    driver.maximize_window()
    #搜索
    driver.find_element_by_id("kw1").send_keys("selenium")
    driver.find_element_by_id("su1").click()
    time.sleep(3)
    #将页面滚动条拖到底部
    js="var q=document.documentElement.scrollTop=10000"
    driver.execute_script(js)
    time.sleep(3)
    #将滚动条移动到页面的顶部
    js="var q=document.documentElement.scrollTop=0"
    driver.execute_script(js)
    time.sleep(3)
    driver.quit()

    '''

  • 相关阅读:
    ByteBuffer使用实例
    Fiddler抓包显示请求时延
    手机wifi连上Fiddler后无网络问题解决
    git
    git
    Autofac使用
    Redis实战
    Redis实战
    Redis实战
    Redis实战
  • 原文地址:https://www.cnblogs.com/yongpan666/p/3762434.html
Copyright © 2011-2022 走看看