{"id":761,"date":"2023-02-26T16:11:18","date_gmt":"2023-02-26T08:11:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=761"},"modified":"2023-04-29T16:59:51","modified_gmt":"2023-04-29T08:59:51","slug":"python-selenium-warning-box-process","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-warning-box-process\/","title":{"rendered":"Python Selenium\u8b66\u544a\u6846\u5904\u7406"},"content":{"rendered":"<p>\u5728WebDriver\u4e2d\u5904\u7406JavaScript\u6240\u751f\u6210\u7684<code>alert<\/code>\u3001<code>confirm<\/code>\u4ee5\u53ca<code>prompt<\/code>\u5341\u5206\u7b80\u5355\uff0c\u5177\u4f53\u505a\u6cd5\u662f\u4f7f\u7528 <code>switch_to.alert()<\/code> \u65b9\u6cd5\u5b9a\u4f4d\u5230 <code>alert\/confirm\/prompt<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>text\/accept\/dismiss\/send_keys<\/code>\u7b49\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>text\uff1a\u8fd4\u56de alert\/confirm\/prompt \u4e2d\u7684\u6587\u5b57\u4fe1\u606f<\/li>\n<li>accept()\uff1a\u63a5\u53d7\u73b0\u6709\u8b66\u544a\u6846<\/li>\n<li>dismiss()\uff1a\u89e3\u6563\u73b0\u6709\u8b66\u544a\u6846<\/li>\n<li>send_keys(keysToSend)\uff1a\u53d1\u9001\u6587\u672c\u81f3\u8b66\u544a\u6846<\/li>\n<\/ul>\n<p>Javascript\u5f39\u7a97\u662f\u4e0d\u80fd\u901a\u8fc7\u524d\u7aef\u5de5\u5177\u5bf9\u5176\u8fdb\u884c\u5b9a\u4f4d\u7684\uff0c\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7<code>switch_to_alert()<\/code>\u65b9\u6cd5\u63a5\u6536\u5f39\u7a97\u3002<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nfrom selenium.webdriver.common.action_chains import ActionChains\nimport time\n\ndriver = webdriver.Chrome()\ndriver.implicitly_wait(10)\ndriver.get(&#039;http:\/\/www.baidu.com&#039;)\n\n# \u9f20\u6807\u60ac\u505c\u81f3\u201c\u8bbe\u7f6e\u201d\u94fe\u63a5\nlink = driver.find_element_by_link_text(&#039;\u8bbe\u7f6e&#039;)\nActionChains(driver).move_to_element(link).perform()\n\n# \u6253\u5f00\u641c\u7d22\u8bbe\u7f6e\ndriver.find_element_by_link_text(&quot;\u641c\u7d22\u8bbe\u7f6e&quot;).click()\n\n# \u4fdd\u5b58\u8bbe\u7f6e\ntime.sleep(2)\n# driver.find_element_by_class_name(&quot;prefpanelgo&quot;).click()\ndriver.find_element_by_xpath(&quot;\/\/a[@class=&#039;prefpanelgo&#039;]&quot;).click()\n\n# \u63a5\u53d7\u8b66\u544a\u6846\ndriver.switch_to.alert.accept()\n\ndriver.quit()<\/code><\/pre>\n<p>\u901a\u8fc7<code>switch_to_alert()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u9875\u9762\u4e0a\u7684\u8b66\u544a\u6846\uff0c\u5e76\u4f7f\u7528<code>accept()<\/code>\u65b9\u6cd5\u63a5\u53d7\u8b66\u544a\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728WebDriver\u4e2d\u5904\u7406JavaScript\u6240\u751f\u6210\u7684alert\u3001confirm\u4ee5\u53caprompt\u5341\u5206\u7b80\u5355\uff0c\u5177\u4f53 [&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-761","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/761","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=761"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}