Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does execute_script() in Selenium does

browser.execute_script("window.open('about:blank', 'tab2');")
browser.switch_to.window("tab2")
browser.get('http://bing.com')

I was searching online ways to open a new tab using selenium in python, and the method of ctrl + t wasn't working on chrome so I stumbled on the above piece of code, however I am not able to understand what 'excute_script' does.

like image 739
Vikas Goudar Avatar asked Jul 11 '26 11:07

Vikas Goudar


1 Answers

execute_script method allows to execute JavaScript passed as string argument

Note that you can pass data from Python code into JavaScript code using arguments, e.g.

hello = "Hello"
friends = " friends"

browser.execute_script('alert(arguments[0], arguments[1]);', (hello,  friends))

enter image description here

like image 110
DonnyFlaw Avatar answered Jul 13 '26 23:07

DonnyFlaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!