How to use selenium-webdriver and python to open multiple web pages in separate tabs in your browser… here is a solution to the problem.
How to use selenium-webdriver and python to open multiple web pages in separate tabs in your browser
I want to use Python to open multiple local html in the same browser window using the Selenium Webdriver. I tried the following in a Jupyter notebook:
from selenium import webdriver
1page = "file://<path for 1.html>"
2page = "file://<path for 2.html>"
firefox_path = 'C:\geckodriver.exe'
driver = webdriver. Firefox(executable_path= firefox_path)
driver.get(1page)
# For opening 2nd HTML in another Tab
driver.execute_script('''window.open('''+ 2page + ''',"_blank"); ''')
Running the code above causes me the following error:
JavascriptException: Message: Error: Access to 'file://<path of 2.html>' from script denied
How can I mitigate this error?
Solution
To open multiple URLs/webpages in separate tabs in your browser, you can use the following solution:
Code block:
from selenium import webdriver first_page = "http://www.google.com" second_page = "https://www.facebook.com/" options = webdriver. ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') driver=webdriver. Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get(first_page) driver.execute_script("window.open('" + second_page +"');" )
Browser snapshot: