1、介紹下selenium經常使用的apijavascript
*** Settings ***
Library SeleniumLibrary
Resource baidu業務.txt
Resource UI分層.txtcss
*** Test Cases ***
baidu_case
打開瀏覽器 https://www.baidu.com chrome
輸入框 robotframework
點擊百度搜索按鈕
等待時間
驗證標題 robotframework_百度搜索
關閉瀏覽器java
selenium_api_id定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#經過id定位文本框
Input Text id = kw selenium
#點擊百度搜索按鈕
Click Element id = su
log 搜索成功
#關閉瀏覽器
Close Browserjquery
selenium_api_name定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#經過name定位文本框
Input Text name = wd selenium
#點擊百度搜索按鈕
Click Element su
log 搜索成功
#關閉瀏覽器
Close Browserchrome
selenium_api_link_text定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#經過link點擊新聞連接
Click Link link = 新聞
log 跳轉新聞首頁
#關閉瀏覽器
Close Browserapi
selenium_api_css定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#css簡寫定位 id-># class->.
#經過css定位文本框
Comment Input Text css=#kw selenium
#點擊百度搜索按鈕
Comment Click Element css=#su
#經過css屬性定位
Comment Input Text css=input#kw selenium
Comment Input Text css=input[id='kw'] selenium
#點擊百度搜索按鈕
Comment Click Element css=input[id='su']
#css層級定位
Input Text css=form#form>span>input selenium
Comment Input Text css=.quickdelete-wrap>input#kw selenium
#點擊百度搜索按鈕
Click Element su
log 搜索成功
#關閉瀏覽器
Close Browser瀏覽器
selenium_api_xpath定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#經過xpath屬性定位
Comment Input Text //input[@id='kw'] selenium
#點擊百度搜索按鈕
Comment Click Element //input[@id='su']
#xpath層級定位
Input Text //form[@id='form'/span/input selenium
Comment Input Text //span[@class=bg s_ipt_wr quickdelete-wrap/input selenium
#點擊百度搜索按鈕 #經過and定位
Click Element //input[@id="su"and@type="submit"]
log 搜索成功
#關閉瀏覽器
#有關contains函數 #點擊新聞連接
Click Element //a[contains(text(),"新聞")] #contains定位文本中的函數
Close Browseride
selenium_api_jquery定位
#打開瀏覽器
Open Browser http://www.baidu.com gc
#經過jquery定位文本
Execute Javascript $("input#kw").val('selenium')
Execute Javascript $("input#su").click()
#關閉瀏覽器
Close Browser函數
selenium_api_瀏覽器的相關操做
#打開瀏覽器
Open Browser http://www.baidu.com gc
#點擊新聞連接
Click Element link=新聞
sleep 2
#返回百度首頁
Go Back
#再一次訪問新聞連接
Go To http://news.baidu.com/
#刷新頁面
Reload Page
#設置窗口的長和寬
Set Window Size 600 800
#獲取窗口的長和寬
${weight} ${height} Get Window Size
log ${weight}
log ${height}
Close Browserui
selenium_api_設置元素等待時間
#打開瀏覽器
Open Browser http://www.baidu.com gc
#強制等待
sleep 2
#隱性等待
Set Browser Implicit Wait 3
#點擊新聞連接
Click Element link=新聞
#最大化窗口
Maximize Browser Window
#等待某個元素出現
Wait Until Page Contains Element link=高級搜索
log 成功
Close Browser
selenium_api_獲取文本和標題
#打開瀏覽器
Open Browser http://www.baidu.com gc
#強制等待
sleep 2
Input Text kw selenium
Click Button su
sleep 5
#獲取文本
${text} Get Text css=#container > div.head_nums_cont_outer.OP_LOG > div > div.nums > span #打印出百度爲您找到相關結果約27,800,000個
log ${text}
#獲取標題
${title} Get Title
log ${title}
selenium_api_斷言的使用
#打開瀏覽器
Open Browser http://www.baidu.com gc
#強制等待
sleep 2
Input Text kw selenium
Click Button su
sleep 5
#獲取文本
${text} Get Text css=#container > div.head_nums_cont_outer.OP_LOG > div > div.nums > span #打印出百度爲您找到相關結果約27,800,000個
Page Should Contain 百度爲您找到相關結果約27,800,000個
Should Contain ${text} 百度爲您找到相關結果約27,800,000個
selenium_api_iframe表單操做
Open Browser http://mail.126.com gc
Sleep 3
Click Element lbNormal
Select Frame id=x-URS-iframe #進入iframe
Input Text name=email taoooo
log 切換iframe成功
Unselect Frame #退出iframe
selenium_api_執行javascript
#打開瀏覽器
Open Browser http://www.baidu.com gc
Input Text id = kw selenium
Click Element id = su
#獲取窗口的長和寬
${weight} ${height} Get Window Size
#執行Execute Javascript
Execute Javascript window.scrollTo(0.2500) #縱向從0-2500
sleep 3
Set Window Size 400 500
Execute Javascript window.scrollTo(0.2500) #橫向從0-2500
sleep 3
Close All Browsers
selenium_api_多窗口切換
#打開瀏覽器
Open Browser http://www.baidu.com gc
Maximize Browser Window
Sleep 3
Input Text kw 滲透吧
Click Element su
Sleep 3
Click Element //*[@id='1']/h3/a/em Get Window Size
#new 切換到新的窗口
Select Window new
sleep 3
Click Link link=進入貼吧 500
log 加載完畢
sleep 3
Select Window title=滲透吧_百度搜索
log 切換回首窗口
Close All Browsers
selenium_api_鼠標懸停
#打開瀏覽器
Open Browser http://www.baidu.com gc
Set Browser Implicit Wait 3
Maximize Browser Window
Mouse Over tj_briicon #懸掛到更多產品上
Sleep 5
Mouse Down tj_nuomi #點擊百度糯米點擊不鬆開
Sleep 5
Mouse Up tj_nuomi #點擊百度糯米釋放
Sleep 5
Mouse Over css=span.arrow-down-line #懸掛到深圳
Sleep 5
#點擊北京
Click Link link=北京
2、selenium api 在ride中截圖以下