[英]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.