简体   繁体   English

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

[英]How to switch to window or tab in splinter webdriver python?

I am new to python and splinter.我是 python 和 splinter 的新手。 I read the docs on splinter but it doesn't mention anything about switch_to like in the selenium library.我阅读了关于 splinter 的文档,但它没有提到 selenium 库中关于 switch_to 的任何内容。

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文档- 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/webdri回溯(最近一次调用):文件“/Users/Splinter/switch_tabs.py”,第 13 行,在 browser.driver.switch_to.window(window1) 文件 >"/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”,第 123 行,在 _w3c_window send_handle(window_name) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- package/selenium/webdriver/remote/switch_to.py”,第 119 行,在 send_handle 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/版本/2.7/lib/python2.7/site-packages/selenium/webdri ver/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 S ver/remote/remote_connection.py”,第 464 行,在执行数据 = utils.dump_json(params) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver /remote/utils.py”,第34行,在dump_json返回json.dumps(json_struct)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/ INIT py”为,线244 , 在转储中返回 _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> 不是 JSON 可序列化的 [在 93.137s 中完成] Linter S everity Provider Description Line everity 提供者描述行

Any ideas are welcome.欢迎任何想法。 Thanks!谢谢!

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中为splinter设置自定义webdriver? - How to set up a custom webdriver for splinter in python? 如何在webdriver(Python)中打开一个新窗口或选项卡? - How to open a new window or tab in webdriver (Python)? python splinter webdriver无法启动 - python splinter webdriver can't start 如何在Python中使用Selenium在由不同WebDriver打开的不同Chrome浏览器窗口之间切换? - How to switch between different chrome browser window opened by different WebDriver using selenium in Python? python selenium。 下载后如何切换回上一个窗口/标签打开一个新标签 - python selenium . how to switch back to previous window/tab after a download opens up a new tab 如何使用Splinter Python登录Twitter - How to log into Twitter with Splinter Python 为什么 switch_to_window() 方法不适用于 Python 中的 selenium webdriver? - Why is switch_to_window() method not working for selenium webdriver in Python? 如何以编程方式登录,然后使用python切换到WebDriver - How to log in programmatically and then switch to WebDriver with python 在Python中使用Selenium WebDriver在新标签页/窗口中打开链接 - Using Selenium WebDriver in Python to Open Link in New Tab/Window 如何在python splinter中使用flask驱动程序 - how to use flask driver in python splinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM