我是Python和Splinter的新手。我阅读了Splinter上的文档,但它没有提及Selenium库中的Switch_to的任何内容。
当我运行此代码时,我会收到错误。
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
错误:
追溯(最近的最新通话(: 文件"/users/splinter/switch_tabs.py",第13行,in browser.driver.switch_to.window(Window1( 文件>"/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/selenium/selenium/webdriver/remote/switch_to.py",第112行,在窗口中 self._w3c_window(window_name( 文件"/library/frameworks/python.framework/2.7/lib/python2.7/site-packages/selenium/webdriver/reemote/remote/switch_to.py.py",第123行,在_w3c_window中 send_handle(window_name( 文件"/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/remote/switch_to.py.py",第119行,在send_handle中 self.__driver.execute(command.switch_to_window,{'handle':h}( file"/library/frameworks/python.framework/2.7/lib/python2.7/site-packages/selenium/webdriver/reemote/webdriver/webdriver.py.py",第310行, 响应= self.command_executor.execute(driver_command,params( file"/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/remote/remote_connection.py pline eccute ececute data = utils.dump_json(参数( 文件"/library/frameworks/python.framework/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/remote/utils.py",第34行,在dump_json中 返回json.dumps(json_struct( file"/library/frameworks/python.framework/2.7/lib/python2.7/json/json/pt_s> init> py",第244行,在转储中 返回_default_encoder.encode(obj( 文件"/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py",第207行, 块= self.iterencode(o,_one_shot = true( 文件"/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py",第270行,在iterencode中 返回_iterencode(O,0( 文件"/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py",第184行,默认 升级类型(repr(o( "不是JSON可序列化"( typeError:https://www.google.com.ph/?gfe_rd=cr&dcr=0&ei = iismwogqdo2tx4q9gbgppop&gbgp&gws_rd = sssl>不是json serializable [在93.137s完成] Linter 严重提供者说明线
欢迎任何想法。谢谢!
尝试此(工作(:
# 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)