• selenium 基础(一)


    selenium安装

    pip install selenium

    selenium操作浏览器原理

    早期selenium 1.0 用的selenium RC, 后来selenum2集合了selenium1.0 + webdriver,selenium RC被webdriver替换。通过webdriver,测试脚本(例如python)可以方便的通过API操作浏览器页面元素,包括打开,关闭,最大化,最小化,元素定位,元素单击等等等。但是selenium操作浏览器还需要一个驱动程序,不同的浏览器如filefox,chrome所需要的驱动程序不一样,就算是同款浏览器,因为浏览器内部提供的原生自动化接口API不同,也需要适配不同版本的驱动程序,不然就有可能出现调用接口失败的情况。

    webdriver按照server-client经典设计模式设计,client可以理解为测试脚本,selenium支持多种语言(java,python,ruby,php等),server端可以理解为浏览器,client和server的通信根据the WebDriver Wire协议告诉服务端我们希望浏览器接下来做什么事情。

    驱动下载

    Chrome:  https://sites.google.com/a/chromium.org/chromedriver/downloads
    Edge:  https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
    Firefox:  https://github.com/mozilla/geckodriver/releases
    Safari:  https://webkit.org/blog/6900/webdriver-support-in-safari-10/
    

    一个selenium简单的例子:(相应驱动要放在path环境变量中)

    from selenium import webdriver
    browser = webdriver.Chrome()
    url = 'http://www.baidu.com'
    browser.get(url)
    

    浏览器基础操作

    方法比较多,先整理一些,有需要后面再补充进来(有些参考了webdriver.py和webelement.py)

    新建一个driver,初始化要操作的浏览器

    driver = webdriver.Chrome()
    

    访问url

    driver.get(url)
    

    最大化窗口

    driver.maximize_window()
    

    设置窗口宽,高

    driver.set_window_size(width,height)
    

    页面操作

    browser.back()
    browser.forward()
    browser.refresh()
    browser.close() 关闭当前窗口
    browser.quit()  退出驱动并关闭每个关联窗口
    

    页面信息

    browser.title    
    browser.current_url
    browser.current_window_handle 返回当前窗口句柄
    driver.window_handles 所有窗口handle
    

    frame切换

    switch_to_frame()
    switch_to_window()
    

    对话框

    switch_to_alert()
    

    控件填写信息,也可以是文件上传

    send_keys()
    

    回车

    send_keys(Keys.RETURN)
    

    判断元素是否可见

    is_displayed()
    

    操作cookie

    get_cookies()
    get_cookie(name)
    delete_cookie(name)
    delete_all_cookies()
    add_cookie(cookie_dict)
    

    显式等待和隐式等待
    显式等待会让WebDriver等待满足一定的条件以后再进一步的执行。 而隐式等待让Webdriver等待一定的时间后再才是查找某元素。

    显式等待

    WebDriverWait(driver, 10).untile(EC.visibility_of_element_located((By.CLASS_NAME, "logo_sogou")))
    

    隐式等待

    implicitly_wait(10)
    

    屏幕截屏

    save_screeshot(filepath)
    

    元素查找

    id查找

    find_element_by_id("xxx")
    

    name查找

    find_element_by_name("xxx")
    

    class查找

    find_element_by_class_name("xxx")
    

    css查找

    find_element_by_css_selector('.s_ipt')
    

    XPath查找

    类似于xml定位一样,html的标签也可以用这类方式来查找,而且更健壮

    find_element_by_xpath("/html/body/form[1]")
    find_element_by_xpath("//form[1]")   html页面中第一个form元素
    find_element_by_xpath("//div[@id='search_ext']")
    find_element_by_xpath("//div[@class='ipt_wrap']/span[1]")
    find_element_by_xpath("//input[@name='continue'][@type='button']")
    

    超链接查找

    find_element_by_link_text('Continue')  完全匹配
    find_element_by_partial_link_text('Conti')   部分匹配
    

    一次查找返回多个元素(list)

    find_elements_by_name
    find_elements_by_xpath
    find_elements_by_link_text
    

    参考文章:

    http://selenium-python-zh.readthedocs.io/en/latest/index.html

  • 相关阅读:
    msp430项目编程57
    msp430项目编程56
    msp430项目编程55
    msp430项目编程54
    msp430项目编程53
    msp430项目编程52
    msp430项目编程51
    msp430项目编程50
    msp430项目编程47
    msp430项目编程46
  • 原文地址:https://www.cnblogs.com/crazymanpj/p/8627679.html
Copyright © 2020-2023  润新知