{"id":759,"date":"2023-02-26T16:09:58","date_gmt":"2023-02-26T08:09:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=759"},"modified":"2023-04-29T17:00:04","modified_gmt":"2023-04-29T09:00:04","slug":"python-selenium-multi-form-switch","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-multi-form-switch\/","title":{"rendered":"Python Selenium\u591a\u8868\u5355\u5207\u6362"},"content":{"rendered":"<p>\u5728Web\u5e94\u7528\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230frame\/iframe\u8868\u5355\u5d4c\u5957\u9875\u9762\u7684\u5e94\u7528\uff0cWebDriver\u53ea\u80fd\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u5bf9\u5143\u7d20\u8bc6\u522b\u4e0e\u5b9a\u4f4d\uff0c\u5bf9\u4e8eframe\/iframe\u8868\u5355\u5185\u5d4c\u9875\u9762\u4e0a\u7684\u5143\u7d20\u65e0\u6cd5\u76f4\u63a5\u5b9a\u4f4d\u3002\u8fd9\u65f6\u5c31\u9700\u8981\u901a\u8fc7<code>switch_to.frame()<\/code>\u65b9\u6cd5\u5c06\u5f53\u524d\u5b9a\u4f4d\u7684\u4e3b\u4f53\u5207\u6362\u4e3aframe\/iframe\u8868\u5355\u7684\u5185\u5d4c\u9875\u9762\u4e2d\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-html\">&lt;html&gt;\n  &lt;body&gt;\n    ...\n    &lt;iframe id=&quot;x-URS-iframe&quot; ...&gt;\n      &lt;html&gt;\n         &lt;body&gt;\n           ...\n           &lt;input name=&quot;email&quot; &gt;<\/code><\/pre>\n<p>126\u90ae\u7bb1\u767b\u5f55\u6846\u7684\u7ed3\u6784\u5927\u6982\u662f\u8fd9\u6837\u5b50\u7684\uff0c\u60f3\u8981\u64cd\u4f5c\u767b\u5f55\u6846\u5fc5\u987b\u8981\u5148\u5207\u6362\u5230iframe\u8868\u5355\u3002<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;http:\/\/www.126.com&quot;)\n\ndriver.switch_to.frame(&#039;x-URS-iframe&#039;)\ndriver.find_element_by_name(&quot;email&quot;).clear()\ndriver.find_element_by_name(&quot;email&quot;).send_keys(&quot;username&quot;)\ndriver.find_element_by_name(&quot;password&quot;).clear()\ndriver.find_element_by_name(&quot;password&quot;).send_keys(&quot;password&quot;)\ndriver.find_element_by_id(&quot;dologin&quot;).click()\ndriver.switch_to.default_content()\n\ndriver.quit()<\/code><\/pre>\n<p><code>switch_to.frame()<\/code>\u9ed8\u8ba4\u53ef\u4ee5\u76f4\u63a5\u53d6\u8868\u5355\u7684id \u6216name\u5c5e\u6027\u3002\u5982\u679ciframe\u6ca1\u6709\u53ef\u7528\u7684id\u548cname\u5c5e\u6027\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4f4d\u3002<\/p>\n<pre><code class=\"language-python\">\u2026\u2026\n#\u5148\u901a\u8fc7xpth\u5b9a\u4f4d\u5230iframe\nxf = driver.find_element_by_xpath(&#039;\/\/*[@id=&quot;x-URS-iframe&quot;]&#039;)\n\n#\u518d\u5c06\u5b9a\u4f4d\u5bf9\u8c61\u4f20\u7ed9switch_to.frame()\u65b9\u6cd5\ndriver.switch_to.frame(xf)\n\u2026\u2026\ndriver.switch_to.parent_frame()<\/code><\/pre>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u5728\u8fdb\u5165\u591a\u7ea7\u8868\u5355\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>switch_to.default_content()<\/code>\u8df3\u56de\u6700\u5916\u5c42\u7684\u9875\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Web\u5e94\u7528\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230frame\/iframe\u8868\u5355\u5d4c\u5957\u9875\u9762\u7684\u5e94\u7528\uff0cWebDriver\u53ea\u80fd\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u5bf9\u5143\u7d20\u8bc6 [&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-759","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/759","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=759"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}