Alert

警告相关的实现

class selenium.webdriver.common.alert.Alert(driver)
    Bases: object
    允许使用alerts.
    使用这个类作为与alert互动的接口。它包括dismissing(忽略),accepting(接受),inputting(输入)和获取警告提示的文本信息的方法。
    接受/忽略警报信息:
        Alert(driver).accpet()
        Alert(driver).dismiss()

    往警报信息中输入:
        name_prompt = Alert(driver)name_prompt.send_keys("Willian Shakesphere")
        name_prompt.accept()

    读取警报信息作为验证:
        alert_text = Alert(driver).text self.assertEqual("Do you wish to quit?", alert_text)

    accept()
        接受可用的警报信息
        Usage:: Alert(driver).accept() # 确认一个警告提示框

    authenticate(username, password)
        发送用户名/密码到身份验证框(例如Basic HTTP Auth),隐性发送“clicks ok"
        用法:: driver.switch_to.alert.authenticate('cheese', 'secretGouda')
        参数: -username: 用户名对话框的字符串 -password: 密码对话框的字符串

    dismiss()
        忽略警报

    send_keys(keysToSend)
        Alert发送键
        Aargs: *keysToSend: 发送给Alert的文本

    text
        获取警报的文本

特殊键

该键实现如下   class selenium.webdriver.common.keys.Keys

    Bases: object
    一套特殊的键码:
    ADD=u'\ue025'
    ALT=u'\ue00a'
    ARROW_DOWN=u'\ue015'
    ARROW_RIGHT=u'\ue014'
    ARROW_UP=u'\ue013'
    BACKSPACE=u'\ue003'
    BACK_SPACE=u'\ue003'
    CHANCE=u'\ue001'
    CLEAR=u'\ue005'
    COMMAND=u'\ue03d'
    CONTROL=u'\ue009'
    DECIMAL=u'\ue028'
    DELETE=u'\ue017'
    DIVIDE=u'\ue029'
    DOWN=u'\ue015'
    END=u'\ue010'
    ENTER=u'\ue007'
    EQUALS=u'\ue019'
    ESCAPE=u'\ue00c'
    F1=u'\ue031'
    F10=u'\ue03a'
    F11=u'\ue03b'
    F12=u'\ue032'
    F2 = u'\ue032'
    F3 = u'\ue033'
    F4 = u'\ue034'
    F5 = u'\ue035'
    F6 = u'\ue036'
    F7 = u'\ue037'
    F8 = u'\ue038'
    F9 = u'\ue039'
    HELP = u'\ue002'
    HOME = u'\ue011'
    INSERT = u'\ue016'
    LEFT = u'\ue012'
    LEFT_ALT = u'\ue00a'
    LEFT_CONTROL = u'\ue009'
    LEFT_SHIFT = u'\ue008'
    META = u'\ue03d'
    MULTIPLY = u'\ue024'
    NULL = u'\ue000'
    NUMPAD0 = u'\ue01a'
    NUMPAD1 = u'\ue01b'
    NUMPAD2 = u'\ue01c'
    NUMPAD3 = u'\ue01d'
    NUMPAD4 = u'\ue01e'
    NUMPAD5 = u'\ue01f'
    NUMPAD6 = u'\ue020'
    NUMPAD7 = u'\ue021'
    NUMPAD8 = u'\ue022'
    NUMPAD9 = u'\ue023'
    PAGE_DOWN = u'\ue00f'
    PAGE_UP = u'\ue00e'
    PAUSE = u'\ue00b'
    RETURN = u'\ue006'
    RIGHT = u'\ue014'
    SEMICOLON = u'\ue018'
    SEPARATOR = u'\ue026'
    SHIFT = u'\ue008'
    SPACE = u'\ue00d'
    SUBTRACT = u'\ue027'
    TAB = u'\ue004'
    UP = u'\ue013

定位元素

有很多特征可以用于元素定位,实现如下:

class selenium.webdriver.common.by.By
    Bases:object

    CLASS_NAME='class name'

    CSS_SELECTOR='css selector'

    ID='id'

    LINK_TEXT='link text'

    NAME='name'

    PARTIAL_LINK_TEXT='partial link text'

    TAG_NAME='tag name'

    XPATH='xpath'

Desired Capabilities

Desired Capabilities实现如下:

class selenium.webdriver.common.desired_capabilities.desired_capabilities
    Bases: object

    设置默认支持的所需功能.
    以此为起点创建desired capbilities对象用于连接selenium服务器或gridremote webdrivers请求.
    例如:
        from selenium import webdriver
        selenium_grid_url = "http://198.0.0.1:4444/wd/hub"
        # Creat a desired capabilities object as a starting point. capabilities = 
        DesiredCapabilities.FIREFOX.copy() capabilities['platform'] = "WINDOWS"
        capabilities['version']="10"

        # Instantiate an instance of Remove WebDriver with the desired capabilities. driver = 
        webdriver.Remote(desired_capabilities=capabilities,
            command_executor=selenium_grid_url)

        注:DesiredCapabilities对象始终使用“.copy()",避免出现改变全局类实例的副作用。

        ANDROID={'platform':'ANDROID', 'browserName':'andorid','version':",'javascriptEnabled':True}

        CHROME={'platform': 'ANY', 'browserName':'chrome', 'version':", 'javascriptEnabled': True}

        EDGE={'platform':'WINDOWS', 'broserNAME':'MicrosoftEdge', 'version':"}

        FIREFOX 
        = {'platform': 'ANY', 'browserName':'firefox','version':",'marionette':False,'javascriptEnabled':True}

        HTMLUNIT={'platform':'ANY','browserName':'htmlunit','version':"}

        HTMLUNITWITHJS
        ={'platform':'ANY','browserName':'htmlunit','version':'firefox','javascriptEnabled':True}

        INTERNETEXPLORER
        ={'platform':'WINDOWS','browserName':'internet explorer','version':", 'javascriptEnabled':True}

        IPAD={'platform':'MAC', 'browserName':'iPad', 'version':", 'javascriptEnabled':True}

        IPHONE={'platform':'MAC', 'browserName':'iPhone', 'version':", 'javascriptEnabled':True}

        OPERA={'platform':'ANY', 'browserName':'opera', 'version':". 'javascriptEnabled':True}

        PHANTOMJS={'platform': 'ANY', 'browserName':'phantomjs', 'version':", 'javascriptEnabled':True}

        SAFART={'platform':'MAC', 'browserName':'safari','version':",'javascriptEnabled':True}

公共部分

Utils 方法。

selenium.webdriver.common.utils.find_connectable_ip(host, port=None)

    将hostname解析为ip,建议ipv4.
    之所以建议ipv4,不是因为只支持ipv4,是因为一些dirvers(如firfoxdriver)不支持ipv6连接。
    如果提供了可选的端口号,仅监听给定端口号的ip.
    参数:
        * host - hostname
        * port - 可选的端口号

    返回值:
        一个单一的ip地址,字符串类型。如果找到任意ipv4地址,就会返回。或则,找到任意的ipv6地址,也会返回。如果都没有找到,返回None.


selenium.webdriver.common.utils.free_port()

    确定一个使用套接字的空闲端口

selenium.webdriver.common.utils.is_connectable(port, host='localhost')

    尝试通过端口连接server,查看server是否在运行。
    参数:* port: 连接的端口

selenium.webdriver.common.utils.is_url_connectable(port)

    尝试通过端口连接到/status HTTP server,查看HTTPserver是否响应。
    参数: *port: 连接的端口

selenium.webdriver.common.utils.join_host_port(host, port)

    将hostname和port拼接到一起
    这是一个小的实现用于应对ipv6,如:
    _join_host_port('::1',80) == '[::1]:80'.

    参数:
       *host - hostname
       *port - 整数端口

    selenium.webdriver.common.utils.keys_to_typing(value)

        处理元素键入的值

Firefox WebDriver

class selenium.webdriver.firefox.webdriver.WebDriver(firefox_profile=None, firefox_binary=None,
timeout=30, capabilities=None, proxy=None, executable_path='wires', firefox_options=None)

    Base: selenium.webdriver.remote.webdriver.WebDriver
    quit()
       退出driver并每一个相关窗口

    set_context(context)

    NATIVE_EVENTS_ALLOWED=True

    firefox_profile

Chrome WebDriver

class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', prot=0,
chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)

    Bases: selenium.webdriver.remote.webdriver.WebDriver

    控制ChromeDriver,并允许drive browser.
    Chromedriver下载地址[http://chromedriver.storage.googleapis.com/index.html](http://chromedriver.storage.googleapis.com/index.html)

    create_options()

    launch_app(id)
       启动由id指定的Chrome app.

    quit()
       关闭浏览器和关闭启动ChromeDriver时启动的ChromeDriver.

Top^

上一篇Selenium主题7 下一篇Selenium主题9

by 李鹏