欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

Web元素定位及操作

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

方法1:文檔級(jí)別操作

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

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

測(cè)試示例:

  • 打開(kāi)百度一下
  • 輸入框輸入”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í)行過(guò)程中,WebDriver 將 JavaScript 語(yǔ)句注入到瀏覽器中,然后腳本將執(zhí)行。這個(gè)注入 JavaScript 有自己的名稱空間,不會(huì)干擾實(shí)際網(wǎng)頁(yè)中的 JavaScript運(yùn)行。

方法2:元素級(jí)別操作

可以先使用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)

可以在語(yǔ)句中使用多個(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)頁(yè)標(biāo)題

滑動(dòng)

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

滑動(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)去選擇對(duì)應(yīng)的時(shí)間。自動(dòng)化測(cè)試中,可以使用JavaScript代碼取消readonly屬性。

測(cè)試頁(yè)面: https://www.12306.cn/index/

測(cè)試步驟:

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

python測(cè)試代碼:

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性能測(cè)試中可以調(diào)用Web API接口window.performance來(lái)測(cè)試Web性能。

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

相關(guān)文章

最新評(píng)論