Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k 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.9k 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!

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training course!

Browse Categories

...