您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium實戰(zhàn)腳本集?新浪微博發(fā)送QQ每日焦點
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2015/4/9 15:04:53 ] 推薦標簽:Selenium 功能測試 腳本

  背景
  很多同學(xué)在工作中是沒有selenium的實戰(zhàn)環(huán)境的,因此自學(xué)的同學(xué)會感到有力無處使,想學(xué)習(xí)但又不知道怎么練習(xí)。其實學(xué)習(xí)新東西的道理都是想通的,那是反復(fù)練習(xí)。這里乙醇會給出一些有用的,也富有挑戰(zhàn)的練習(xí),幫助大家去快速掌握和使用selenium webdriver。多用才會有感觸。
  練習(xí)
  首先去www.qq.com的首頁把話題的標題和url拿到
  然后去weibo.com登陸,登陸后發(fā)一條微博,內(nèi)容是今題話題的標題和url
  用到的知識點
  自動登錄。微博登錄的時候有可能會有驗證碼,所以自動登錄什么的是極好的;這里建議用profile進行自動登錄;
  爬蟲知識。用webdriver去也頁面上爬一些內(nèi)容。用到的核心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

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd