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

    Selenium实战脚本集—新浪微博发送QQ每日焦点
    http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/201549150822.html

      背景
      很多同学在工作中是没有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


  • 相关阅读:
    Windows7安装SQL Server 2008图解
    【Android病毒分析报告】
    linux source命令学习
    SQL学习笔记——SQL初入门,Ubuntu下MySQL的安装
    挂断电话的实现(即类似于电话号码黑名单)
    35+雪花Logos设计灵感
    jdk8预览
    PL/SQL --> 动态SQL调用包中函数或过程
    VMware 彻底删除虚拟机操作系统的方法
    Ubuntu 16.04 LTS: apt-get update 失败处理 Aborted (core dumped)
  • 原文地址:https://www.cnblogs.com/graceting/p/5034026.html
Copyright © 2011-2022 走看看