zoukankan      html  css  js  c++  java
  • 浏览器的各种操作

        # 5.1、页面刷新
    # driver.refresh()
    # 5.2、设置全屏模式
    # driver.maximize_window()
    # 5.3、设置屏幕大小
    # driver.set_window_size(800,600)
    # 5.4、截屏
    # driver.get_screenshot_as_file(r"D:WebDriverday01 est.png")
    # 5.5、前进后退
    # driver.forward()
    # driver.back()
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()

    driver.get("http://www.baidu.com")
    driver.set_window_size(600,800) # 设置屏幕大小
    driver.find_element_by_link_text("新闻").click()
    time.sleep(3)
    driver.back() # 后退到百度首页
    time.sleep(3)
    driver.forward() # 前进到新闻页
    driver.get_screenshot_as_file(r"D:WebDriverday01 est_jieping.png") # 截屏
    time.sleep(3)

    driver.quit()
    '''

    # 5.6、简单元素操作
    # 文本清除--->clear()
    # driver.find_element_by_id().clear()
    # 提交表单--->submit()
    # driver.find_element_by_id().submit()
    # 获取元素的尺寸、文本、属性、是否可见
    # driver.find_element_by_id("kw").size
    # driver.find_element_by_id("s-bottom-layer-right").text
    # driver.find_element_by_id("kw").get_attribute("type")
    # driver.find_element_by_id("kw").is_displayed()
    '''
    from selenium import webdriver
    import time

    driver=webdriver.Chrome()
    driver.get("http://www.youdao.com/")

    driver.find_element_by_id("translateContent").clear() # 清楚文本
    time.sleep(3)
    driver.find_element_by_id("translateContent").send_keys("自动化") # 输入文本
    time.sleep(3)
    driver.find_element_by_id("translateContent").submit() # 提交内容
    time.sleep(3)
    driver.back() # 后退
    print(driver.find_element_by_id("translateContent").size) # 获取元素尺寸
    print(driver.find_element_by_class_name("copy-right").text) # 获取元素文本
    print(driver.find_element_by_id("translateContent").get_attribute("type")) # 获取属性值
    print(driver.find_element_by_id("translateContent").is_displayed()) # 设置该元素是否用户可见

    driver.quit()
    '''

    # 5.7、鼠标操作
    # 执行所有 ActionChains 中存储的行为
    # perform()
    # 鼠标单击按钮
    # driver.find_element_by_id("su").click()
    # 右键点击
    # ActionChains(driver).context_click(driver.find_element_by_id("su")).perform()
    # 鼠标双击
    # ActionChains(driver).double_click(driver.find_element_by_partial_link_text("登")).perform()
    # 鼠标悬停
    # ActionChains(driver).move_to_element(driver.find_element_by_link_text("设置")).perform()
    # 拖动
    # drag_and_drop(source,target)
    '''
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.implicitly_wait(10)

    time.sleep(3)
    driver.find_element_by_id("su").click() # 点击
    time.sleep(2)
    ActionChains(driver).context_click(driver.find_element_by_id("su")).perform() # 右击
    time.sleep(2)
    ActionChains(driver).double_click(driver.find_element_by_partial_link_text("登")).perform() # 双击
    time.sleep(2)
    driver.find_element_by_id("TANGRAM__PSP_4__closeBtn").click()
    ActionChains(driver).move_to_element(driver.find_element_by_link_text("设置")).perform() # 悬停
    time.sleep(3)

    driver.quit()
    '''

    # 5.8、键盘的操作
    # 键盘输入
    # driver.find_element_by_css_selector("[id='kw']").send_keys("测试工程师")
    # 键盘回车
    # driver.find_element_by_id("TANGRAM__PSP_10__submit").send_keys(Keys.ENTER)
    # 键盘全选
    # driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys(Keys.CONTROL,'a')
    # 键盘删除一个字母
    # driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys(Keys.BACKSPACE)
    # 键盘输入空格键
    # driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys(Keys.SPACE)
    '''
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.implicitly_wait(10)

    driver.find_element_by_id("kw").send_keys("sele") # 向输入框输入内容
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys(Keys.SPACE) # 输入空格
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys(Keys.BACKSPACE) # 删除一个字母
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"a") # 全选
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys(Keys.BACKSPACE)
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys("自动化")
    driver.find_element_by_id("su").send_keys(Keys.ENTER) # 键盘回车
    time.sleep(2)

    driver.quit()
    '''

    # 5.9、获取验证信息:title URL text
    # 获取页面的title
    # driver.title
    # 获取当前页面的URL
    # driver.current_url
    # 获取登录名的用户信息
    # driver.find_element_by_id("spnUid").text
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("https://email.163.com/")
    driver.implicitly_wait(10)

    driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div[2]/div/div[1]/iframe"))
    driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/input").send_keys("*****")
    time.sleep(2)
    driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/form/div/div[3]/div[2]/input[2]").send_keys("*****")
    time.sleep(2)
    driver.find_element_by_id("dologin").click()
    time.sleep(2)
    print(driver.title)
    print(driver.current_url)
    print(driver.find_element_by_id("spnUid").text)

    driver.quit()
    '''

    # 6.0、设置元素等待
    # 显式等待
    # WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located((By.ID,'kw'))).send_keys("selenium")
    # 隐式等待
    # driver.implicitly_wait(10)
    # 休眠方法
    # time.sleep()
    '''
    from selenium import webdriver
    import time
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.common.by import By

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.implicitly_wait(10) # 隐式等待

    WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located((By.ID,"kw"))).send_keys("selenium")

    time.sleep(3) # 休眠方法

    driver.quit()
    '''

    # 6.1、多表单切换
    # driver.switch_to.frame()
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("https://email.163.com/")
    driver.implicitly_wait(10)

    # 表单切换
    driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div[2]/div/div[1]/iframe"))
    driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/form/div/div[1]/div[2]/input").send_keys("*****")
    time.sleep(2)
    driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/form/div/div[3]/div[2]/input[2]").send_keys("****")
    time.sleep(2)
    driver.find_element_by_id("dologin").click()
    time.sleep(2)

    driver.quit()
    '''

    # 6.2、多窗口的切换
    # driver.switch_to.window(handle)
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.implicitly_wait(10)

    baidu_window = driver.current_window_handle # 获取当前百度搜索窗口句柄

    driver.find_element_by_link_text("登录").click()
    driver.find_element_by_link_text("立即注册").click()

    all_window = driver.window_handles # 获取所有窗口句柄
    for handle in all_window:
    if handle != baidu_window:
    driver.switch_to.window(handle) # 切换窗口
    print("请注册")
    driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("你好")
    driver.find_element_by_id("TANGRAM__PSP_4__phone").send_keys("15325321545")
    driver.find_element_by_id("TANGRAM__PSP_4__password").send_keys("1fdfd23456")
    driver.find_element_by_id("TANGRAM__PSP_4__verifyCode").send_keys("5525")
    driver.find_element_by_id("TANGRAM__PSP_4__isAgree").click()
    time.sleep(3)
    driver.find_element_by_id("TANGRAM__PSP_4__submit").click()
    elif handle == baidu_window:
    driver.switch_to.window(handle)
    driver.find_element_by_xpath("/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[3]/p[2]").click()
    driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("15178849203")
    driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys("maojun0501")
    driver.find_element_by_id("TANGRAM__PSP_10__submit").click()

    time.sleep(3)

    driver.quit()
    '''

    # 6.3、警告框处理
    # driver.switch_to.alert()
    '''
    在 WebDriver 中处理 JavaScript 所生成的 alert、confirm 以及 prompt 是很简单的。具体做法是使用
    switch_to.alert方法定位到 alert/confirm/prompt。然后使用 text/accept/dismiss/send_keys 按需进行操做。
    text 返回 alert/confirm/prompt 中的文字信息。
    accept 点击确认按钮。
    dismiss 点击取消按钮,如果有的话。
    send_keys 输入值,这个 alertconfirm 没有对话框就不能用了,不然会报错。
    '''
    '''
    from selenium import webdriver
    import time
    from selenium.webdriver.common.action_chains import ActionChains

    driver = webdriver.Chrome() # 实例化浏览器
    driver.get("http://www.baidu.com") # 打开url地址

    ele = driver.find_element_by_link_text("设置") # 鼠标悬停设置链接
    ActionChains(driver).move_to_element(ele).perform()

    driver.find_element_by_class_name("setpref").click() # 打开搜索设置
    time.sleep(3)
    driver.find_element_by_css_selector("#gxszButton > a.prefpanelgo").click() # 保存设置
    driver.switch_to.alert.accept() # 接收弹窗

    driver.quit()
    '''

    # 6.4、文件上传
    # send_keys实现上传
    '''
    from selenium import webdriver
    import time
    import os

    driver = webdriver.Chrome()

    file_path = 'file:///'+ os.path.abspath('upfile.html') # 打开上传功能页面
    driver.get(file_path)

    driver.find_element_by_name("file").send_keys("E:/upload.txt") # 定位上传按钮,添加本地文件

    time.sleep(3)
    driver.quit()
    '''

    # 6.5、操作cookie
    '''
    有时候我们需要验证浏览器中是否存在某个 cookie,因为基于真实的 cookie 的测试是无法通过白盒和
    集成测试完成的。WebDriver 提供了操作 Cookie 的相关方法可以读取、添加和删除 cookie 信息。  webdriver 操作 cookie 的方法有:
    get_cookies() 获得所有 cookie 信息
    get_cookie(name) 返回有特定 name 值有 cookie 信息
    add_cookie(cookie_dict) 添加 cookie,必须有 name 和 value 值
    delete_cookie(name) 删除特定(部分)的 cookie 信息
    delete_all_cookies() 删除所有 cookie 信息
    下面通过 get_cookies()来获取当前浏览器的 cookie 信息。
    '''
    '''
    from selenium import webdriver
    import time
    import os

    driver = webdriver.Chrome() # 实例化浏览器
    driver.get("http://www.youdao.com")

    # cookie = driver.get_cookies() # 获取cookie信息

    driver.add_cookie({'name':'key-aaaaaaaa','value':'value-bbbbbbb'}) # 像cookie的name和value添加会话信息
    # driver.delete_cookie('JSESSIONID') # 删除key为'JSESSIONID'的cookie
    for co in driver.get_cookies():
    print("%s -> %s"%(co['name'],co['value']))

    driver.quit()
    '''

    # 6.6、调用javascript
    '''
    js="var q=document.documentElement.scrollTop=10000"
    js_ = "var q=document.documentElement.scrollTop=0"
    driver.execute_script(js)
    '''
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")

    driver.find_element_by_id("kw").send_keys("selenium") # 搜索
    driver.find_element_by_id("su").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()
    '''

    # 6.9、窗口截图
    # driver.get_screenshot_as_file()
    '''
    from selenium import webdriver
    import time

    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")

    try:
    driver.find_element_by_id("kw_error").send_keys("selenium")
    driver.find_element_by_id("su").click()
    except:
    driver.get_screenshot_as_file("E:/baidu_error.jpg")

    driver.quit()
    '''

    # 6.9、关闭窗口
    # driver.close()

    # 7.0、验证码处理
    '''
    去掉验证码
    设置万能码
    验证码识别技术
    记录cookie

    '''
  • 相关阅读:
    女子腰背疼痛案
    老人心悸心膝部无力屈伸不利案
    经方生姜泻心汤临床应用发挥
    电话求诊易误治
    女子乳房结块案
    小儿手足口案
    门纯德老先生经验
    男子肋部掣痛案
    加味潜降汤治疗阴虚阳亢之眩晕(来自网络)
    三叉神经痛与芎胡六虫汤
  • 原文地址:https://www.cnblogs.com/siyu0123/p/12766008.html
Copyright © 2011-2022 走看看