This opens a new tab in Firefox browser:
from selenium import webdriver
browser = webdriver.Firefox()
browser.execute_script('''window.open("https://www.google.com/","_blank");''')
As a reference, here is the Selenium documentation:
**execute_script**(script, *args)
Synchronously Executes JavaScript in the current window/frame.
Args:
script: The JavaScript to execute.
*args: Any applicable arguments for your >JavaScript.
Usage:
driver.execute_script(‘return document.title;’)
I have a link in a String which I wish to open in a new tab using javascript (via execute_script).
Tried the following but it gives an error:
link = 'https://www.bing.com/'
java_script = '\'\'\'window.open(\"' + link + '\",\"_blank\");\'\'\''
browser.execute_script(script)
Error:
selenium.common.exceptions.JavascriptException: Message: SyntaxError: unexpected token: string literal