zoukankan      html  css  js  c++  java
  • selenium3 + python 操作浏览器基本方法

    from selenium import webdriver
    import time as t

    # driver = webdriver.Chrome()
    # driver.get("http://www.bing.com")
    # driver.maximize_window()
    # driver.implicitly_wait(30)
    # driver.find_element_by_id("sb_form_q").send_keys("selenium ")
    # t.sleep(5)
    # driver.find_element_by_id("sb_form_go").click()
    # tag_name = driver.find_elements_by_tag_name("input")
    # tag_name[0].send_keys("selenium ")
    # driver.quit()

    """测试当前的地址"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.bing.com")
    # driver.current_url
    # print(driver.current_url)
    # driver.quit()

    """测试出当前title"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.baidu.com")
    # driver.title
    # print(driver.title)
    # driver.quit()

    """测试浏览器前进后退"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.baidu.com")
    # t.sleep(3)
    # driver.get("http://www.bing.com")
    # t.sleep(3)
    # driver.back()
    # print("back后的地址:",driver.current_url)
    # t.sleep(3)
    # driver.forward()
    # print("前进后的地址:",driver.current_url)
    # t.sleep(3)
    # driver.quit()

    """多窗口操作"""
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    t.sleep(3)
    #对百度登录进行点击
    driver.find_element_by_link_text("登录").click()
    t.sleep(3)
    #点击用户名操作
    driver.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
    t.sleep(3)
    #获取当前窗口句柄
    nowHandle = driver.current_window_handle
    t.sleep(3)
    #点击当前窗口-->立即注册
    driver.find_element_by_link_text("立即注册").click()
    t.sleep(3)
    #获取所有当前句柄
    handles = driver.window_handles
    t.sleep(3)
    #对所有窗口进行循环 --> 进行判断
    for handle in handles:
    #判断不是当前句柄
    if handle != nowHandle:
    #跳转到注册的句柄
    driver.switch_to_window(handle)
    driver.find_element_by_name("userName").send_keys("1111")
    t.sleep(3)
    driver.close()
    t.sleep(3)
    #跳转到登录页面的句柄
    driver.switch_to_window(nowHandle)
    driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("123")
    t.sleep(3)
    driver.quit()


    from selenium import webdriver
    import time as t

    """clear的操作"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.baidu.com")
    # driver.maximize_window()
    # send = driver.find_element_by_id("kw")
    # send.send_keys("selenium")
    # t.sleep(3)
    # send.clear()
    # t.sleep(3)
    # driver.quit()

    """页面刷新操作"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.baidu.com")
    # driver.maximize_window()
    # t.sleep(3)
    # driver.refresh()
    # t.sleep(3)
    # driver.quit()

    """页面截图操作
    1.打开网站之后,也可以对屏幕截屏
    2.截屏后设置制定的保存路径+文件名称+后缀
    """
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    t.sleep(3)
    driver.get_screenshot_as_file("D:\Test\nice.png")

    """获取属性的值操作"""
    # driver = webdriver.Chrome()
    # driver.maximize_window()
    # driver.get("http://www.baidu.com")
    # such = driver.find_element_by_id("kw")
    # such.send_keys("selenium")
    # t.sleep(3)
    # print(such.get_attribute("value"))
    # print(such.get_attribute("title"))
    # driver.quit()

    """is_displayed 查看是否可见操作"""
    # driver = webdriver.Chrome()
    # driver.get("http://www.baidu.com")
    # driver.maximize_window()
    # such = driver.find_element_by_link_text("关于百度")
    # print("关于百度链接是否可见",such.is_displayed())
    # driver.quit()

    """is_enabled 是否可编辑操作"""
    # driver = webdriver.Chrome()
    # driver.maximize_window()
    # driver.get("http://www.baidu.com")
    # so = driver.find_element_by_id("kw")
    # print("百度输入框是否可编辑",so.is_enabled())
    # driver.quit()

    """is_selected 是否可选择"""
    # driver = webdriver.Chrome()
    # driver.get("https://mail.sina.com.cn/")
    # driver.maximize_window()
    # select = driver.find_element_by_id("store1")
    # print("新浪邮箱自动登录是否选择",select.is_selected())
    # driver.quit()


    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    import time as t

    driver = webdriver.Chrome()
    driver.maximize_window()
    #driver.get("http://sahitest.com/demo/selectTest.htm")
    # select_test = driver.find_element_by_id("s3Id")
    #实例化Select()
    # se = Select(select_test)
    #使用select 索引方法
    # se.select_by_index(1)
    # t.sleep(3)
    #使用select value 方法
    # se.select_by_value("o4val")
    # t.sleep(3)
    #使用select 文本方法
    # se.select_by_visible_text("With spaces")
    # t.sleep(3)
    # driver.quit()
  • 相关阅读:
    css中span元素的width属性无效果原因及多种解决方案
    CentOS、Ubuntu、Debian三个linux比较异同
    linux系统中/etc/syslog.conf文件解读
    /proc/interrupts 和 /proc/stat 查看中断的情况
    网卡优化RPS/RFS
    关于Linux网卡调优之:RPS (Receive Packet Steering)
    rsync详解之exclude排除文件
    Linux rsync 同步实践
    CentOS 6.3下rsync服务器的安装与配置
    解决linux下/etc/rc.local开机器不执行的原因
  • 原文地址:https://www.cnblogs.com/Teachertao/p/10964524.html
Copyright © 2011-2022 走看看