2

I am new to python and splinter. I read the docs on splinter but it doesn't mention anything about switch_to like in the selenium library.

When I run this code I get an error.

  from splinter import Browser

  browser = Browser("firefox")
  browser.visit('http://google.com')
  window1 = browser.windows[0] #google
  browser.execute_script("window.open('https://www.amazon.com/');")
  window2 = window1.next #amazon active window
  browser.driver.switch_to.window(window1) #switch back to google

Docs- http://splinter.readthedocs.io/en/latest/browser.html

ERROR:

Traceback (most recent call last): File "/Users/Splinter/switch_tabs.py", line 13, in browser.driver.switch_to.window(window1) File >"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py", line 112, in window self._w3c_window(window_name) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py", line 123, in _w3c_window send_handle(window_name) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py", line 119, in send_handle self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h}) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 310, in execute response = self.command_executor.execute(driver_command, params) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 464, in execute data = utils.dump_json(params) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/utils.py", line 34, in dump_json return json.dumps(json_struct) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 244, in dumps return _default_encoder.encode(obj) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode chunks = self.iterencode(o, _one_shot=True) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode return _iterencode(o, 0) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: https://www.google.com.ph/?gfe_rd=cr&dcr=0&ei=iiSMWoGqDO2tX4q9gbgP&gws_rd=ssl> is not JSON serializable [Finished in 93.137s] Linter Severity Provider Description Line

Any ideas are welcome. Thanks!

  • What is the error you're getting? Please add the full traceback to the question. – Norrius Feb 20 '18 at 10:54
  • @Norrius added it. :) – Karlo Abapo Feb 20 '18 at 13:51
  • In another answer it's recommended to run [multiple drivers/browsers](https://webcache.googleusercontent.com/search?q=cache:-UVqSto3oZIJ:https://stackoverflow.com/questions/18150593/selenium-multiple-tabs-at-once+&cd=1&hl=en&ct=clnk&gl=au) for testing with multiple tabs. – lloyd Feb 21 '18 at 07:38

1 Answers1

0

Try this (it's work):

# Get parent window
 parent_window = self.browser.driver.current_window_handle
# Open new page in child window
self.browser.driver.execute_script("window.open('http://google.com')")
 # Get list of all windows currently opened (parent + child)
 all_windows = self.browser.driver.window_handles
 # Get child window
 child_window = [window for window in all_windows if window != parent_window][0]
 # Switch to child window
 self.browser.driver.switch_to.window(child_window)
 # Close child window
 self.browser.driver.close()
 # Return to parent window
 self.browser.driver.switch_to.window(parent_window)