最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Selenium執(zhí)行JavaScript腳本的方法示例

 更新時(shí)間:2020年12月31日 08:31:27   作者:測試開發(fā)小記  
這篇文章主要介紹了Selenium執(zhí)行JavaScript腳本的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

JavaScript是運(yùn)行在客戶端(瀏覽器)和服務(wù)器端的腳本語言,允許將靜態(tài)網(wǎng)頁轉(zhuǎn)換為交互式網(wǎng)頁??梢酝ㄟ^ Python Selenium WebDriver 執(zhí)行 JavaScript 語句,在Web頁面中進(jìn)行js交互。那么js能做的事,Selenium應(yīng)該大部分也能做。WebDriver是模擬終端用戶的交互,所以就不能點(diǎn)擊不可見的元素,有時(shí)可見元素也不能點(diǎn)擊。在這些情況下,我們就可以通過WebDriver 執(zhí)行JavaScript來點(diǎn)擊或者執(zhí)行頁面元素。本文將介紹如何使用 WebDriver執(zhí)行 JavaScript語句。

Web元素定位及操作

使用execute_script() 執(zhí)行 JavaScript 代碼,有兩種方法實(shí)現(xiàn)元素操作

方法1:文檔級別操作

直接使用JavaScript實(shí)現(xiàn)元素定位和動(dòng)作執(zhí)行,主要方法有:

document.getElementById
document.getElementsByClassName
document.getElementsByName
document.getElementsByTagName
document.getElementsByTagNameNS

測試示例:

  • 打開百度一下
  • 輸入框輸入”test“
  • 點(diǎn)擊百度一下

python代碼:

def test_baidu(self):
  self.driver.get("http://www.baidu.com")
  self.driver.execute_script('document.getElementById("kw").value = "test"')
  time.sleep(2)
  self.driver.execute_script('document.getElementById("su").click()')
  time.sleep(2)

在執(zhí)行過程中,WebDriver 將 JavaScript 語句注入到瀏覽器中,然后腳本將執(zhí)行。這個(gè)注入 JavaScript 有自己的名稱空間,不會(huì)干擾實(shí)際網(wǎng)頁中的 JavaScript運(yùn)行。

方法2:元素級別操作

可以先使用WebDriver獲取想要操作的元素,然后使用JavaScript執(zhí)行操作。

input_ele = driver.find_element_by_id("su") 
driver.execute_script("arguments[0].click();", input_ele)

python代碼:

def test_baidu2(self):
  self.driver.get("http://www.baidu.com")
  input_ele = self.driver.find_element_by_id("kw")
  self.driver.execute_script("arguments[0].value = 'test';", input_ele)
  time.sleep(2)
  baidu_ele = self.driver.find_element_by_id("su")
  self.driver.execute_script("arguments[0].click();", baidu_ele)
  time.sleep(2)

可以在語句中使用多個(gè) JavaScript動(dòng)作:

username = driver.find_element_by_xpath("http://*[@id='username']")
password = driver.find_element_by_xpath("http://*[@id='password']")
driver.execute_script("arguments[0].value = 'admin';arguments[1].value = 'admin';", username, password)

獲取返回值

可以返回JavaScript的執(zhí)行結(jié)果:

driver.execute_script("return document.getElementById('kw').value")
driver.execute_script("return document.title;") # 返回網(wǎng)頁標(biāo)題

滑動(dòng)

在 Web自動(dòng)化測試 | ActionChains、TouchAction 中介紹了TouchAction類中scroll_from_element()也可以滑動(dòng)頁面。

滑動(dòng)到瀏覽器底部

document.documentElement.scrollTop=10000
window.scrollTo(0, document.body.scrollHeight)

滑動(dòng)到瀏覽器頂部

document.documentElement.scrollTop=0
window.scrollTo(document.body.scrollHeight,0)

更改元素屬性

大部分時(shí)間控件都是 readonly屬性,需要手動(dòng)去選擇對應(yīng)的時(shí)間。自動(dòng)化測試中,可以使用JavaScript代碼取消readonly屬性。

測試頁面: https://www.12306.cn/index/

測試步驟:

  • 打開測試頁面
  • 修改出發(fā)日期
  • 斷言日期是否修改成功

python測試代碼:

def test_datettime(self):
  self.driver.get("https://www.12306.cn/index/")
  # 取消readonly屬性
  self.driver.execute_script("dat=document.getElementById('train_date'); dat.removeAttribute('readonly')")  
  self.driver.execute_script("document.getElementById('train_date').value='2020-10-01'")
  time.sleep(3)
  now_time = self.driver.execute_script("return document.getElementById('train_date').value")
  assert '2020-10-01' == now_time

總結(jié)

Selenium WebDriver 執(zhí)行 JavaScript代碼是一個(gè)非常強(qiáng)大的功能,可以實(shí)現(xiàn)WebElement 接口所有功能,甚至更多的功能。比如在web性能測試中可以調(diào)用Web API接口window.performance來測試Web性能。

到此這篇關(guān)于Selenium執(zhí)行JavaScript腳本的方法示例的文章就介紹到這了,更多相關(guān)Selenium執(zhí)行JavaScript腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

志丹县| 南投县| 盐津县| 霍城县| 土默特右旗| 方正县| 屯昌县| 深水埗区| 西藏| 云林县| 山东省| 西乌珠穆沁旗| 旬邑县| 甘谷县| 云林县| 土默特右旗| 开平市| 三江| 麻江县| 沙洋县| 四子王旗| 彰化县| 休宁县| 广宁县| 印江| 朝阳县| 北碚区| 沽源县| 汝城县| 清苑县| 邢台市| 建始县| 南川市| 施甸县| 出国| 江源县| 缙云县| 新野县| 宁波市| 丽水市| 南和县|