我的测试人生........

【转】Selenium实战脚本集(1)—新浪微博发送QQ每日焦点

上一篇 / 下一篇  2015-04-10 23:18:46 / 个人分类:Selenium

原文来自:http://www.51testing.com/html/03/n-2427103.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:\Users\Administrator\AppData\Local\Google\Chrome\User 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()
  视频详解
  todo
  常见错误
  xp和win7下面chrome 的profile路径是不一样的
  Windows XP:%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\
  Windows Vista/Windows 7/Windows 8:%LOCALAPPDATA%\Google\Chrome\User Data\

TAG:

 

评分:0

我来说两句

Open Toolbar