Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am working with a website and I need to run a couple js code with Selenium. To make things easier, I need to run functions declared in the website scripts.
For example, the website use a script file called document_handler.js with the following code:

 (function ($) {
     var getConversationId = function(){
         return $('input[name="conversationId"]').val()
     };
 })(jQuery);

In Selenium, if I run:

js_eval = driver.execute_script("return getConversationId()")

I get:

selenium.common.exceptions.WebDriverException: Message: getConversationId is not defined

And if I run:

js_eval = driver.execute_script("return $.getConversationId()")

I get:

selenium.common.exceptions.WebDriverException: Message: $.getConversationId is not a function

How can I load the website javascript files so I can use its functions inside Selenium? Or there is something wrong with my code?

share|improve this question
    
is it obterConversationId or getConversationId ? – webdeb Jul 11 at 20:42
1  
However, the function is in a closed scope, you cannot access it, even from the console.. – webdeb Jul 11 at 20:43
    
@webdeb fixed, it was a typo – Tales Pádua Jul 11 at 20:46
up vote 1 down vote accepted

If this is a script you have access to, you have to make the function available to the outer/global scope.. The simplest would be to assign it to the window object, and it should work.

(function ($) {
     window.getConversationId = function(){
         return $('input[name="conversationId"]').val()
     };
 })(jQuery);

OR this way, which is basically the same..

var getConversationId;
(function ($) {
     getConversationId = function(){
         return $('input[name="conversationId"]').val()
     };
 })(jQuery);
share|improve this answer
    
This is a Script loaded by the page, not written by me. I can download it and edit, but I am not sure how to make Selenium load the edited script and ignore the original – Tales Pádua Jul 11 at 20:54
    
Can this be wrapped in a JSE call? – JeffC Jul 11 at 20:59
    
@TalesPádua I would love to answer you the special selenium question with script replacing??, but I not an expert, you should open a new question and describe the problem more precisely, don't forget to vote and accept ;) – webdeb Jul 11 at 21:00
    
You can change the modified script only on the webserver but that does not seem intended. – Jeroen Heier Jul 11 at 21:00
    
I will try to change the script and inject it following this question – Tales Pádua Jul 11 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.