creating and executing a Javascript function with Selenium

前端 未结 1 1898
说谎
说谎 2021-01-03 03:35

I am trying to create and execute a JavaScript function with Selenium. I am doing it like this:

js_func = \"\"\"
     function blah(a, b, c) {
        .
             


        
相关标签:
1条回答
  • 2021-01-03 04:24

    It's just how Selenium executes JavaScript:

    The script fragment provided will be executed as the body of an anonymous function.

    In effect, your code is:

    (function() {
        function blah(a, b, c) {
            ...
        }
    })();
    
    (function() {
        blah(1, 2, 3);
    });
    

    And due to JavaScript's scoping rules, blah doesn't exist outside of that anonymous function. You'll have to make it a global function:

    window.blah = function(a, b, c) {
        ...
    }
    

    Or execute both scripts in the same function call.

    0 讨论(0)
提交回复
热议问题