splinter icon indicating copy to clipboard operation
splinter copied to clipboard

execute_script a chunk of javascript?

Open ldong opened this issue 12 years ago • 3 comments

Does it support a chunk of codes to be executed? In python i.e.

script = '''
var elements = $('CSS_SELECTOR').text().trim().split(' \n');
var length = elements.length;
var target = 'Stuff'
var exists = false;
for(var i = 0; i < length; ++i){
   exists = elements[i].trim() === target? true : false;
   if(exists){
       break;
   }
}
'''
browser.execute_script(script);

Then I received a lot of errors WebDriverException: Message: u'{"errorMessage":"Parse error" ...)

ldong avatar May 30 '14 15:05 ldong

Another example,

script = "var num = 2+2;"
context.browser.execute_script(script)
length = context.browser.evaluate_script('num')

length is appear to be None, and ErrorMSG is errorMessage":"Can\'t find variable: length"

ldong avatar May 30 '14 15:05 ldong

After series of debugging. I figured out how it works.

It seems it failed on trim()

i.e.

var elements = $('CSS_SELECTOR').text().trim().split(' \n');

So what I figured out is, return the text of the DOM, then manipulate it in python using miniDOM and stuff like that.

However, I am looking for a more proper solution that can be used directly in JavaScript.

ldong avatar May 30 '14 16:05 ldong

Closing this issue, since there's nothing we can fix here. Any issues with Webdriver parsing javascript should be raised with Selenium, not splinter.

javascript-error avatar Oct 25 '21 20:10 javascript-error