如何在splinter webdriver python中切换到窗口或选项卡?

时间:2018-02-20 10:52:35

标签: python selenium selenium-webdriver web-testing splinter

我是python和splinter的新手。我阅读了有关splinter的文档但是没有提到关于switch_to的任何内容,就像在selenium库中一样。

当我运行此代码时,我收到错误。

  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

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

错误:

  

追踪(最近一次通话):    文件“/Users/Splinter/switch_tabs.py”,第13行,in      browser.driver.switch_to.window(窗口1)    文件>“/ Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site-packages / selenium / webdriver / remote / switch_to.py”,第112行,在窗口中       self._w3c_window(window_name)     在_w3c_window中输入文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py”,第123行       send_handle(window_name)     在send_handle中输入文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py”,第119行       self._driver.execute(Command.SWITCH_TO_WINDOW,{'handle':h})     文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”,第310行,执行       response = self.command_executor.execute(driver_command,params)     文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py”,第464行,执行中       data = utils.dump_json(params)     在dump_json中输入文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/utils.py”,第34行       return json.dumps(json_struct)     转储文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py”,第244行       return _default_encoder.encode(obj)     文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py”,第207行,编码       chunks = self.iterencode(o,_ one_shot = True)     文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py”,第270行,在iterencode中       return _iterencode(o,0)     文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py”,第184行,默认情况下       提出TypeError(repr(o)+“不是JSON可序列化的”)   TypeError:https://www.google.com.ph/?gfe_rd = cr& dcr = 0& ei = iiSWWoGqDO2tX4q9gbgP& gws_rd = sssl>不是JSON可序列化的   [完成于93.137s]   短绒   严重性提供者描述行

欢迎任何想法。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试一下(有效):

# 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)