0 votes
1 view
in Devops and Agile by (24.5k points)

I'm using Selenium2 for some automated tests of my website, and I'd like to be able to get the return value of some JavaScript code. If I have a foobar() Javascript function in my webpage and I want to call that and get the return value into my Python code, what can I call to do that?

1 Answer

0 votes
by (62.1k points)

You can return values even if you don't have your snippet code written as a function like in the below example code, by just adding return var;  at the end will work, where var is the variable you want to return.

result = driver.execute_script('''cells = document.querySelectorAll('a');

URLs = []

console.log(cells);

[].forEach.call(cells, function (el) {

    if(el.text.indexOf("download") !== -1){

    //el.click();

    console.log(el.href)

    //window.open(el.href, '_blank');

    URLs.push(el.href)

    }

});

return URLs''')

 The result will contain the array that is URLs in this case. 

Hope this helps!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...