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

  背景
  很多同學(xué)在工作中是沒(méi)有selenium的實(shí)戰(zhàn)環(huán)境的,因此自學(xué)的同學(xué)會(huì)感到有力無(wú)處使,想學(xué)習(xí)但又不知道怎么練習(xí)。其實(shí)學(xué)習(xí)新東西的道理都是想通的,那是反復(fù)練習(xí)。這里乙醇會(huì)給出一些有用的,也富有挑戰(zhàn)的練習(xí),幫助大家去快速掌握和使用selenium webdriver。多用才會(huì)有感觸。
  練習(xí)
  首先去www.qq.com的首頁(yè)把話題的標(biāo)題和url拿到
  然后去weibo.com登陸,登陸后發(fā)一條微博,內(nèi)容是今題話題的標(biāo)題和url
  用到的知識(shí)點(diǎn)
  自動(dòng)登錄。微博登錄的時(shí)候有可能會(huì)有驗(yàn)證碼,所以自動(dòng)登錄什么的是極好的;這里建議用profile進(jìn)行自動(dòng)登錄;
  爬蟲(chóng)知識(shí)。用webdriver去也頁(yè)面上爬一些內(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()
  常見(jiàn)錯(cuò)誤
  xp和win7下面chrome 的profile路徑是不一樣的
  Windows XP:%USERPROFILE%Local SettingsApplication DataGoogleChromeUser Data
  Windows Vista/Windows 7/Windows 8:%LOCALAPPDATA%GoogleChromeUser Data

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