美文网首页Crawler
selenuim/python3简单使用

selenuim/python3简单使用

作者: 疯帮主 | 来源:发表于2018-11-10 23:40 被阅读0次

    使用之前得安装好对应浏览器版本的驱动
    chrome:https://sites.google.com/a/chromium.org/chromedriver/downloads

    打开浏览器

    from selenium import webdriver
    # 启动一个chrome浏览器
    browser = webdriver.Chrome()
    

    你会看到浏览器是这样的


    chrome
    # 关闭浏览器
    browser.close()
    

    其他浏览器:

    • webdriver.Firefox()
    • webdriver.Safari()
    • webdriver.Ie()
    • webdriver.Opera()
    • webdriver.Edge()

    发送请求

    browser = webdriver.Chrome()
    browser.get("http://www.python.org")
    
    image.png

    输入字符,回车

    from selenium.webdriver.common.keys import Keys
    # 通过id查找单个元素,多个元素用find_elements_by_class
    input_ = browser.find_element_by_id('id-search-field')
    # 给元素输入selenium
    input_.send_keys('selenium')
    # 回车
    input_.send_keys(Keys.ENTER)
    
    python/selenium

    页面源码

    browser.page_source
    

    获取cookies

    browser.get_cookies()
    
    Out:
    [{'domain': '.python.org',
      'expiry': 1604929261,
      'httpOnly': False,
      'name': '__utma',
      'path': '/',
      'secure': False,
      'value': '32101439.1472864258.1541857261.1541857261.1541857261.1'},
     {'domain': '.python.org',
      'httpOnly': False,
      'name': '__utmc',
      'path': '/',
      'secure': False,
      'value': '32101439'},
     {'domain': '.python.org',
      'expiry': 1541859061,
      'httpOnly': False,
      'name': '__utmb',
      'path': '/',
      'secure': False,
      'value': '32101439.1.10.1541857261'},
     {'domain': '.python.org',
      'expiry': 1557625261,
      'httpOnly': False,
      'name': '__utmz',
      'path': '/',
      'secure': False,
      'value': '32101439.1541857261.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'},
     {'domain': '.python.org',
      'expiry': 1541857861,
      'httpOnly': False,
      'name': '__utmt',
      'path': '/',
      'secure': False,
      'value': '1'}]
    

    当前url

    browser.current_url
    
    Out:
    'https://www.python.org/'
    

    简单交互

    • element.send_keys('hello')输入hello
    • element.clear()清空收入框
    • element.send_keys('Keys.ENTER')回车键
    • button.click()点击按钮

    跳转框架

    browser.switch_to_frame('framename')跳到子框架
    browser.switch_to.parent_frame()跳回父框架

    拖拽

    # 导入机械臂,个人叫法
    from selenium.webdriver import ActionChains
    browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    browser.switch_to_frame('iframeResult')
    source = browser.find_element_by_id('draggable')
    target = browser.find_element_by_id('droppable')
    # 实例化一个机械臂
    active = ActionChains(browser)
    # 设定目标
    active.drag_and_drop(source, target)
    # 执行
    active.perform()
    
    image.png

    执行JavaScript

    # 弹窗
    browser.execute_script('alert("python data")')
    # 调用js达到下拉进度条
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    

    获取属性

    nav = browser.find_element_by_id('mainnav')
    print(nav.get_attribute('class'))
    
    Out:
    python-navigation main-navigation do-not-print
    
    get_attribute

    获取文本

    print(nav.text)
    div = browser.find_element_by_class_name('slide-copy')
    print(div.find_element_by_tag_name('h1').text)
    
    Out:
    About
    Downloads
    Documentation
    Community
    Success Stories
    News
    Events
    
    Functions Defined
    

    获取元素信息

    print(div.location)
    print(div.size)
    print(div.tag_name)
    print(div.id)
    
    Out:
    {'x': 0, 'y': 109}
    {'height': 0, 'width': 0}
    div
    0.44500430680309555-2
    

    隐式等待

    # 等待10秒,默认0
    browser.implicitly_wait(10)
    

    显示等待

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # 等待浏览器
    wait = WebDriverWait(browser, 10)
    # 查找元素
    input_ = wait.until(EC.presence_of_element_located((By.ID, 'q')))
    # 查找按钮
    button = wait.until(EC.element_located_to_be_selected((By.CSS_SELECTOR, '#par')))
    

    前进后退

    • browser.back()
    • browser.forward()
    • browser.sleep(1)睡眠

    cookie

    • browser.add_cookie({})
    • browser.delete_cookie("")
    • browser.delete_all_cookies()

    选项卡管理

    # 打开新选项卡
    browser.execute_script('window.open()')
    # 跳转选项卡
    browser.switch_to_window(browser.window_handles[0])
    # 关闭好像实现不了
    browser.execute_script('window.close()')
    

    异常处理

    from selenium.common.exceptions import TimeoutException, NoSuchElementException
    

    参考文档:https://selenium-python.readthedocs.io/

    相关文章

      网友评论

        本文标题:selenuim/python3简单使用

        本文链接:https://www.haomeiwen.com/subject/bfjtfqtx.html