mango
mango

Reputation: 23

For Selenium, how would I send a multi-line command through the console of the browser through selenium

I'm using selenium on python right now and I was wondering how to execute multi line commands to the console I am doing this command:

script = "alert('Test Alert')"
driver.execute_script(script)

Although I'm not sure how to make it so I can paste a multi-line piece of code for it to execute in the console

Upvotes: 0

Views: 445

Answers (1)

Max Daroshchanka
Max Daroshchanka

Reputation: 2968

It's possible to execute the multiline command in the same way like single-line:

script = '''
    a = function(b, c) {
        return b + c;
    }
    
    return a(2, 2);
'''

print(driver.execute_script(script))

this will print 4.

However, if you'll run the script with alert, the browser will be blocked until the alert is closed.

Please note, for this script:

script = '''
    a = function(b, c) {
        return b + c;
    }
    alert('Test Alert');
    return a(2, 2);
'''

print(driver.execute_script(script))

this will print None, but return a(2, 2) line will be also executed;

Upvotes: 1

Related Questions