3

I want to open mulitple local htmls within same browser window using Selenium Webdriver using Python. I have tried following in 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 above code lead me to the following error:

JavascriptException: Message: Error: Access to 'file://<path of 2.html>' from script denied

How to mitigate this error?

abhi1610
  • 721
  • 13
  • 28

1 Answers1

3

To open multiple URLs / webpages in seperate TABs within a 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 :

multiple_tabs

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for the Reply. But, how about local html pages? – abhi1610 May 25 '18 at 09:35
  • `get()` method accepts _URL_ as `String`. So you can use `get()` through passing `local html pages` just like the way we have passed `first_page` and `second_page` as variables to `get()` – undetected Selenium May 25 '18 at 09:39
  • Are you seeing any difference in the solution apart from what I commented? Where are you exactly stuck? Are you seeing any error? Update the question with your _code trial_ and error stack trace please. – undetected Selenium May 25 '18 at 09:54
  • I have tried what you have suggested. It opens the said tabs in new window instead of current window. Also it shows me `WebDriverException: Message: unknown error: call function result missing 'value' (Session info: chrome=66.0.3359.181) (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 x86_64)` – abhi1610 May 25 '18 at 09:59
  • @abhi1610 The error `call function result missing 'value'` is a common error stemming out of **incompatible version** of the binaries you are using and have no relation with this question/answer. Follow the discussion [Selenium WebDriverException: unknown error: call function result missing 'value' while calling sendkeys method](https://stackoverflow.com/questions/49219594/selenium-webdriverexception-unknown-error-call-function-result-missing-value/49219750#49219750) to solve it. – undetected Selenium May 25 '18 at 10:12