zoukankan      html  css  js  c++  java
  • selenium实战脚本集(1)——新浪微博发送QQ每日焦点

    背景

    很多同学在工作中是没有selenium的实战环境的,因此自学的同学会感到有力无处使,想学习但又不知道怎么练习。其实学习新东西的道理都是想通的,那就是反复练习。这里乙醇会给出一些有用的,也富有挑战的练习,帮助大家去快速掌握和使用selenium webdriver。多用才会有感触。

    练习

    • 首先去www.qq.com的首页把今日话题的标题和url拿到
    • 然后去weibo.com登陆,登陆后发一条微博,内容就是今题话题的标题和url

    用到的知识点

    • 自动登录。微博登录的时候有可能会有验证码,所以自动登录什么的是极好的;这里建议用profile进行自动登录;
    • 爬虫知识。用webdriver去也页面上爬一些内容。用到的核心api是getAttribute
    • css选择器。微博的文本框用css选择器去定位比较方便;

    参考答案

    #coding: utf-8
    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    from time import sleep
    
    op = Options()
    op.add_argument('user-data-dir=C:UsersAdministratorAppDataLocalGoogleChromeUser Data')
    
    dr = webdriver.Chrome(chrome_options=op)
    
    dr.get('http://www.qq.com')
    today_top_link = dr.find_element_by_css_selector('#todaytop a')
    content = today_top_link.text
    url = today_top_link.get_attribute('href')
    print content
    print url
    
    dr.get('http://www.weibo.com')
    sleep(2)
    
    dr.find_element_by_css_selector('#v6_pl_content_publishertop .W_input').send_keys(content+url)
    dr.find_element_by_css_selector('#v6_pl_content_publishertop .btn_30px').click()
    sleep(2)
    dr.close()
    
    

    视频详解

    常见错误

    xp和win7下面chrome 的profile路径是不一样的

    • Windows XP:%USERPROFILE%Local SettingsApplication DataGoogleChromeUser Data
    • Windows Vista/Windows 7/Windows 8:%LOCALAPPDATA%GoogleChromeUser Data
  • 相关阅读:
    tp3.2和tp5的区别
    tp(调试模式,空操作,跨控制器调用,跨方法跳转--redirect(),框架语法,创建model模型)
    tp 连表查 分页 模糊查询
    thinkphp if和eq的区别
    js 面向对象例子
    JS字符串转换为JSON的四种方法笔记
    json在php中的使用之如何转换json为数组
    window.frames用法
    php js 数组排序
    控件常用
  • 原文地址:https://www.cnblogs.com/nbkhic/p/4386595.html
Copyright © 2011-2022 走看看