python中执行js

Posted 金竹多多

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中执行js相关的知识,希望对你有一定的参考价值。

import execjs  
#执行本地的js  
  
def get_js():  
    # f = open("D:/WorkSpace/MyWorkSpace/jsdemo/js/des_rsa.js",‘r‘,encoding=‘UTF-8‘)  
    f = open("./js/des_rsa.js", r, encoding=UTF-8)  
    line = f.readline()  
    htmlstr = ‘‘  
    while line:  
        htmlstr = htmlstr + line  
        line = f.readline()  
    return htmlstr  
  
jsstr = get_js()  
ctx = execjs.compile(jsstr)  
print(ctx.call(enString,123456))  
class erp_canvas_driver( browserDriver):

    __inject_js_files__ = []

    def __init__(self):
        browserDriver.__init__(self)

        self.inject_js_files = [
            os.path.join(self.rootPath, lib/erp/javascripts/ERPOAL.js),
            os.path.join(self.rootPath, lib/erp/javascripts/ERPActors.js),
            os.path.join(self.rootPath, lib/erp/javascripts/browser_utils.js)
        ]

    def openPage(self, url=/ERP):
        if browserDriver.openPage(self, url):
            self.__initHelper__()

    def __injectJSCode__(self, path):
        js_lines = []
        file = open(os.path.abspath(path))
        for line in file:
            js_lines.append(line)

        self.browser.execute_script(\n.join(js_lines))

    def __initHelper__(self):
        for path in self.inject_js_files:
            self.__injectJSCode__(path)

        time.sleep(1)

    def navigateByButton(self, button_name):
        self.clickERPButton(button_name)
        time.sleep(5)
        try:
            WebDriverWait(self.browser, 10).until(
                EC.presence_of_element_located((By.ID, "obj1"))
            )
        finally:
            self.__initHelper__()

    def clickERPButton(self, name):
        code = mouseClick("{0}").format(name)
        self.browser.execute_script(code)

    def pressERPButton(self, name):
        code = mousePress("{0}").format(name)
        self.browser.execute_script(code)

    def releaseERPButton(self, name):
        code = mouseRelease("{0}").format(name)
        self.browser.execute_script(code)

    def waitDisplayUpdate(self, name, property, expect_value, timeout=20):
        n = min(int(timeout / 0.2), 50)
        for i in range(n):
            value = self.browser.execute_script(return ERPGetElementProperty("{0}", "{1}").format(name, property))
            if value == expect_value:
                return True
            time.sleep(0.2)
        return False

    def getElementProperty(self, name, property):
        return self.browser.execute_script(return ERPGetElementProperty("{0}", "{1}").format(name, property))

    def setElementProperty(self, name, property, value, isDigit=False):
        if isDigit:
            self.browser.execute_script(ERPSetElementProperty("{0}", "{1}", {2});.format(name, property, value))
        else:
            self.browser.execute_script(ERPSetElementProperty("{0}", "{1}", "{2}");.format(name, property, value))

 

以上是关于python中执行js的主要内容,如果未能解决你的问题,请参考以下文章

XSS:如何从 C# 中的字符串中删除 JS 片段?

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

angularJS使用ocLazyLoad实现js延迟加载

js常用代码片段(更新中)

Chrome-Devtools代码片段中的多个JS库

Node.js JavaScript 片段中的跳过代码