zoukankan      html  css  js  c++  java
  • selenium_基本用法

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门

    https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6EmUbbW&id=564564604865

     

     
     
     
    from selenium import webdriver
    browser=webdriver.Firefox()
    browser.get('http://mail.yahoo.com')
    emailElem=browser.find_element_by_id('login-username')
    emailElem.send_keys('not_my_real_email')
    passwordElem=browser.find_element_by_id('login-passwd')
    passwordElem.send_keys('12345')
    passwordElem.submit() #把用户名和密码显示到框架上

    如果没有passwordElem.submit() ,就不会显示用户名和密码
    图片





     from selenium import webdriver
    browser = webdriver.Firefox()
    try:
        elem = browser.find_element_by_class_name('bookcover') #程序执行速度慢
        print('Found <%s> element with that class name!' % (elem.tag_name))
    except:
        print('Was not able to find an element with that name.')
        
    linkElem=browser.find_element_by_link_text('Read It Online')
     
    linkElem.click() #执行速度很慢
     

    图片


    #特别注意属性值大小写敏感:Keys,Firefox,HOME,END等等
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys #Keys 等导入对象大小写敏感
    browser=webdriver.Firefox()     #Firefox大小写敏感
    browser.get('http://nostarch.com')
    htmlElem=browser.find_element_by_tag_name('html')
    htmlElem.send_keys(Keys.END) #scrolls to bottom
    #htmlElem.send_keys(Keys.HOME)  #scrolls to top

    图片




    #特别注意属性值大小写敏感:Keys,Firefox,HOME,END等等
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys #Keys 等导入对象大小写敏感
    browser=webdriver.Firefox()     #Firefox大小写敏感
    browser.get('http://nostarch.com')
    htmlElem=browser.find_element_by_tag_name('html')
    htmlElem.send_keys(Keys.END) #scrolls to bottom
    #htmlElem.send_keys(Keys.HOME)  #scrolls to top
     
    linkElem=browser.find_element_by_link_text('Rootkits and Bootkits')
    linkElem.click()    #点击Rootkits and Bootkits链接
    browser.back()    #后退网页
    browser.forward()  #前进网页
    browser.refresh()  #刷新网页
    browser.quit()    #退出浏览器
     
     
     
     

    Table 11-3. Selenium’s WebDriver Methods for Finding Elements

    Method name

    WebElement object/list returned

    browser.find_element_by_class_name(name)
    browser.find_elements_by_class_name(name)

    Elements that use the CSS class name

    browser.find_element_by_css_selector(selector)
    browser.find_elements_by_css_selector(selector)

    Elements that match the CSS selector

    browser.find_element_by_id(id)
    browser.find_elements_by_id(id)

    Elements with a matching id attribute value

    browser.find_element_by_link_text(text)
    browser.find_elements_by_link_text(text)

    <a> elements that completely match the textprovided

    browser.find_element_by_partial_link_text(text)
    browser.find_elements_by_partial_link_text(text)

    <a> elements that contain the text provided

    browser.find_element_by_name(name)
    browser.find_elements_by_name(name)

    Elements with a matching name attribute value

    browser.find_element_by_tag_name(name)
    browser.find_elements_by_tag_name(name)

    Elements with a matching tag name (case insensitive; an <a> element is matched by 'a'and 'A')

    Table 11-4. WebElement Attributes and Methods

    Attribute or method

    Description

    tag_name

    The tag name, such as 'a' for an <a> element

    get_attribute(name)

    The value for the element’s name attribute

    text

    The text within the element, such as 'hello' in <span>hello</span>

    clear()

    For text field or text area elements, clears the text typed into it

    is_displayed()

    Returns True if the element is visible; otherwise returns False

    is_enabled()

    For input elements, returns True if the element is enabled; otherwise returns False

    is_selected()

    For checkbox or radio button elements, returns True if the element is selected; otherwise returns False

    location

    A dictionary with keys 'x' and 'y' for the position of the element in the page

     
     
     
     
     

    Table 11-5. Commonly Used Variables in the selenium.webdriver.common.keysModule

    Attributes

    Meanings

    Keys.DOWNKeys.UPKeys.LEFTKeys.RIGHT

    The keyboard arrow keys

    Keys.ENTERKeys.RETURN

    The ENTER and RETURN keys

    Keys.HOMEKeys.ENDKeys.PAGE_DOWN,Keys.PAGE_UP

    The homeendpagedown, and pageup keys

    Keys.ESCAPEKeys.BACK_SPACEKeys.DELETE

    The ESC, BACKSPACE, and DELETE keys

    Keys.F1Keys.F2,..., Keys.F12

    The F1 to F12 keys at the top of the keyboard

    Keys.TAB

    The TAB key

     
  • 相关阅读:
    电商总结(六)系统容量预估
    Nginx 和 IIS 实现动静分离
    聊一聊PV和并发
    RabbitMQ学习系列(四): 几种Exchange 模式
    RabbitMQ学习系列(三): C# 如何使用 RabbitMQ
    RabbitMQ学习系列(二): RabbitMQ安装与配置
    RabbitMQ学习系列(一): 介绍
    推荐:《部落 一呼百应的力量》
    Solr学习总结(八)IK 中文分词的配置和使用
    常用的数据统计Sql 总结
  • 原文地址:https://www.cnblogs.com/webRobot/p/5464990.html
Copyright © 2011-2022 走看看