python自動(dòng)化測(cè)試selenium執(zhí)行js腳本實(shí)現(xiàn)示例
WebDriver有2個(gè)方法執(zhí)行Java Script腳本。
(1)同步執(zhí)行:execute_script
(2)異步執(zhí)行:execute_async_script
from selenium import webdriver
from time import sleep
class TestScriptss(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get(http://www.baidu.com)
#執(zhí)行js提示框,并點(diǎn)確定
def test_scripts(self):
self.driver.execute_script("alert('test')")
sleep(2)
self.driver.switch_to.alert.accept()
self.driver.quit()
#執(zhí)行腳本獲取當(dāng)前頁(yè)面標(biāo)題
def test_scripts(self):
js = "return document.title"
title = self.driver.execute_script(js)
print(title)
sleep(2)
self.driver.quit()
#格式化文本框
def test_format(self):
js = 'var q = document.getElementById("kw"); q.style.border="10px solid red"'
self.driver.execute_script(js)
sleep(5)
self.driver.quit()
#通過腳本實(shí)現(xiàn)頁(yè)面滾動(dòng)
def test_scroll(self):
self.driver.find_element_by_id('kw').send_keys('selenium test')
self.driver.find_element_by_id('su').click()
sleep(2)
#搜索結(jié)果頁(yè)面滾動(dòng)
js = 'window.scrollTo(0, document.body.scrollHeight)'
# 同步執(zhí)行
self.driver.execute_script(js)
#異步執(zhí)行
# self.driver.execute_async_script(js)
sleep(2)
self.driver.quit()
以上來(lái)自:極客時(shí)間課程:selenium自動(dòng)化測(cè)試學(xué)習(xí)總結(jié)。
以上就是python自動(dòng)化測(cè)試selenium執(zhí)行js腳本實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于selenium測(cè)試框架的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信