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


  • 相关阅读:
    butterknife异常提示:attribute value must be constant
    SharedPreferences第一次使用后HashMap将常驻内存
    js获取元素的innerText属性为什么为空
    针对focus和blur的Dom事件触发顺序
    android中View的GONE和INVISIBLE的原理
    HTML中div以及span等元素获取焦点
    android MotionEvent获得当前位置
    IE10 透明背景的div无法遮罩
    jquery中.attr('value')和.val()的区别
    __proto__和protaotype的区分
  • 原文地址:https://www.cnblogs.com/graceting/p/5034026.html
Copyright © 2011-2022 走看看