{"id":760,"date":"2023-02-26T16:10:36","date_gmt":"2023-02-26T08:10:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=760"},"modified":"2023-04-29T16:59:27","modified_gmt":"2023-04-29T08:59:27","slug":"python-selenium-multi-window-switch","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-multi-window-switch\/","title":{"rendered":"Python Selenium\u591a\u7a97\u53e3\u5207\u6362"},"content":{"rendered":"<p>\u5728\u9875\u9762\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u6709\u65f6\u5019\u70b9\u51fb\u67d0\u4e2a\u94fe\u63a5\u4f1a\u5f39\u51fa\u65b0\u7684\u7a97\u53e3\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u5207\u6362\u5230\u65b0\u6253\u5f00\u7684\u7a97\u53e3\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002WebDriver\u63d0\u4f9b\u4e86<code>switch_to.window()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nimport time\n\ndriver = webdriver.Firefox()\ndriver.implicitly_wait(10)\ndriver.get(&quot;http:\/\/www.baidu.com&quot;)\n\n# \u83b7\u5f97\u767e\u5ea6\u641c\u7d22\u7a97\u53e3\u53e5\u67c4\nsreach_windows = driver.current_window_handle\n\ndriver.find_element_by_link_text(&#039;\u767b\u5f55&#039;).click()\ndriver.find_element_by_link_text(&quot;\u7acb\u5373\u6ce8\u518c&quot;).click()\n\n# \u83b7\u5f97\u5f53\u524d\u6240\u6709\u6253\u5f00\u7684\u7a97\u53e3\u7684\u53e5\u67c4\nall_handles = driver.window_handles\n\n# \u8fdb\u5165\u6ce8\u518c\u7a97\u53e3\nfor handle in all_handles:\n    if handle != sreach_windows:\n        driver.switch_to.window(handle)\n        print(&#039;now register window!&#039;)\n        driver.find_element_by_name(&quot;account&quot;).send_keys(&#039;username&#039;)\n        driver.find_element_by_name(&#039;password&#039;).send_keys(&#039;password&#039;)\n        time.sleep(2)\n        # \u2026\u2026\n\ndriver.quit()<\/code><\/pre>\n<p>\u5728\u672c\u4f8b\u4e2d\u6240\u6d89\u53ca\u7684\u65b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>current_window_handle\uff1a\u83b7\u5f97\u5f53\u524d\u7a97\u53e3\u53e5\u67c4<\/li>\n<li>window_handles\uff1a\u8fd4\u56de\u6240\u6709\u7a97\u53e3\u7684\u53e5\u67c4\u5230\u5f53\u524d\u4f1a\u8bdd<\/li>\n<li>switch_to.window()\uff1a\u7528\u4e8e\u5207\u6362\u5230\u76f8\u5e94\u7684\u7a97\u53e3\uff0c\u4e0e\u4e0a\u4e00\u8282\u7684<code>switch_to.frame()<\/code>\u7c7b\u4f3c\uff0c\u524d\u8005\u7528\u4e8e\u4e0d\u540c\u7a97\u53e3\u7684\u5207\u6362\uff0c\u540e\u8005\u7528\u4e8e\u4e0d\u540c\u8868\u5355\u4e4b\u95f4\u7684\u5207\u6362<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9875\u9762\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u6709\u65f6\u5019\u70b9\u51fb\u67d0\u4e2a\u94fe\u63a5\u4f1a\u5f39\u51fa\u65b0\u7684\u7a97\u53e3\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u5207\u6362\u5230\u65b0\u6253\u5f00\u7684\u7a97\u53e3\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002WebDriver\u63d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-760","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=760"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/760\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}