{"id":1104425,"date":"2025-01-08T16:20:32","date_gmt":"2025-01-08T08:20:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104425.html"},"modified":"2025-01-08T16:20:35","modified_gmt":"2025-01-08T08:20:35","slug":"python%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%adweb%e9%a1%b5%e9%9d%a2%e4%b8%ad%e7%9a%84%e5%bc%b9%e6%a1%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104425.html","title":{"rendered":"python\u5982\u4f55\u5173\u95edweb\u9875\u9762\u4e2d\u7684\u5f39\u6846"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065816\/90cf2403-fe30-4fab-8023-a7e287a5a94b.webp\" alt=\"python\u5982\u4f55\u5173\u95edweb\u9875\u9762\u4e2d\u7684\u5f39\u6846\" \/><\/p>\n<p><p> <strong>Python\u5173\u95edweb\u9875\u9762\u4e2d\u7684\u5f39\u6846\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528Selenium\u5e93\u3001\u5904\u7406Alert\u5bf9\u8c61\u3001\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u7528\u6237\u70b9\u51fb\u3001\u5904\u7406JavaScript\u5f39\u6846\u3001\u4f7f\u7528WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u7b49\u5f85\u5f39\u6846\u51fa\u73b0\u5e76\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u7528\u6237\u70b9\u51fb\u3001\u5904\u7406JavaScript\u5f39\u6846\u3001\u4f7f\u7528WebDriverWait\u7b49\u5f85\u5f39\u6846\u51fa\u73b0\u5e76\u5173\u95ed\u3002\u4f7f\u7528Selenium\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u5904\u7406\u5404\u79cdWeb\u9875\u9762\u64cd\u4f5c\uff0c\u5305\u62ec\u5904\u7406\u5f39\u6846\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7Selenium\u5173\u95ed\u5f39\u6846\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eSelenium<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Selenium\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u4ee5\u53ca\u76f8\u5e94\u7684WebDriver\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f7f\u7528\u7684\u662fChrome\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Selenium\u548cChromeDriver\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u4ece<a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">ChromeDriver\u4e0b\u8f7d\u9875\u9762<\/a>\u4e0b\u8f7d\u4e0eChrome\u6d4f\u89c8\u5668\u7248\u672c\u5339\u914d\u7684ChromeDriver\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u521b\u5efaWebDriver\u5b9e\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u548c\u914d\u7f6e\u597dSelenium\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2aWebDriver\u5b9e\u4f8b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u542f\u52a8Chrome\u6d4f\u89c8\u5668\u5e76\u6253\u5f00\u4e00\u4e2aURL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efaChrome WebDriver\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u6307\u5b9aURL<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5904\u7406JavaScript Alert\u5f39\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u5728Web\u9875\u9762\u4e2d\uff0cJavaScript Alert\u5f39\u6846\u662f\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u5f39\u6846\u7c7b\u578b\u3002Selenium\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684API\u6765\u5904\u7406\u8fd9\u4e9b\u5f39\u6846\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>switch_to.alert<\/code>\u65b9\u6cd5\u83b7\u53d6Alert\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\u6765\u63a5\u53d7\u6216\u53d6\u6d88\u5f39\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.alert import Alert<\/p>\n<h2><strong>\u5047\u8bbe\u5728\u9875\u9762\u52a0\u8f7d\u4e4b\u540e\u4f1a\u5f39\u51fa\u4e00\u4e2aAlert\u5f39\u6846<\/strong><\/h2>\n<p>alert = Alert(driver)<\/p>\n<h2><strong>\u63a5\u53d7Alert\u5f39\u6846<\/strong><\/h2>\n<p>alert.accept()<\/p>\n<h2><strong>\u6216\u8005\u53d6\u6d88Alert\u5f39\u6846<\/strong><\/h2>\n<h2><strong>alert.dismiss()<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528WebDriverWait\u7b49\u5f85\u5f39\u6846\u51fa\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u5f39\u6846\u5e76\u4e0d\u4f1a\u7acb\u5373\u51fa\u73b0\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528WebDriverWait\u6765\u7b49\u5f85\u5f39\u6846\u51fa\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7b49\u5f85Alert\u5f39\u6846\u51fa\u73b0\u5e76\u5173\u95ed\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.alert import Alert<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u7b49\u5f85Alert\u5f39\u6846\u51fa\u73b0<\/strong><\/h2>\n<p>WebDriverWait(driver, 10).until(EC.alert_is_present())<\/p>\n<h2><strong>\u83b7\u53d6Alert\u5bf9\u8c61\u5e76\u63a5\u53d7\u5f39\u6846<\/strong><\/h2>\n<p>alert = Alert(driver)<\/p>\n<p>alert.accept()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u5f39\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86JavaScript Alert\u5f39\u6846\uff0c\u8fd8\u6709\u5176\u4ed6\u7c7b\u578b\u7684\u5f39\u6846\uff0c\u4f8b\u5982\u786e\u8ba4\u6846\uff08Confirm\uff09\u548c\u63d0\u793a\u6846\uff08Prompt\uff09\u3002\u8fd9\u4e9b\u5f39\u6846\u7684\u5904\u7406\u65b9\u5f0f\u4e0eAlert\u7c7b\u4f3c\uff0c\u53ea\u9700\u8981\u8c03\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>send_keys<\/code>\u65b9\u6cd5\u5411\u63d0\u793a\u6846\u8f93\u5165\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u5728\u9875\u9762\u52a0\u8f7d\u4e4b\u540e\u4f1a\u5f39\u51fa\u4e00\u4e2aPrompt\u5f39\u6846<\/p>\n<p>prompt = Alert(driver)<\/p>\n<h2><strong>\u5411Prompt\u5f39\u6846\u8f93\u5165\u6587\u672c<\/strong><\/h2>\n<p>prompt.send_keys(&#39;Hello, world!&#39;)<\/p>\n<h2><strong>\u63a5\u53d7Prompt\u5f39\u6846<\/strong><\/h2>\n<p>prompt.accept()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u4f7f\u7528Selenium\u5904\u7406\u81ea\u5b9a\u4e49\u5f39\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e9bWeb\u9875\u9762\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684HTML\u548cCSS\u5b9e\u73b0\u5f39\u6846\uff0c\u800c\u4e0d\u662f\u4f7f\u7528JavaScript\u7684Alert\u3001Confirm\u6216Prompt\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7684\u5176\u4ed6\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u70b9\u51fb\u5173\u95ed\u6309\u94ae\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5f39\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<h2><strong>\u5047\u8bbe\u81ea\u5b9a\u4e49\u5f39\u6846\u7684\u5173\u95ed\u6309\u94ae\u6709\u4e00\u4e2a\u7279\u5b9a\u7684CSS\u7c7b\u540d<\/strong><\/h2>\n<p>close_button = driver.find_element(By.CLASS_NAME, &#39;custom-close-button&#39;)<\/p>\n<h2><strong>\u70b9\u51fb\u5173\u95ed\u6309\u94ae<\/strong><\/h2>\n<p>close_button.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e03\u3001\u5904\u7406\u591a\u7a97\u53e3\u548ciframe\u4e2d\u7684\u5f39\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u5f39\u6846\u4f1a\u51fa\u73b0\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u6216iframe\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5148\u5207\u6362\u5230\u6b63\u786e\u7684\u7a97\u53e3\u6216iframe\uff0c\u624d\u80fd\u5904\u7406\u5f39\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5207\u6362\u5230iframe\u5e76\u5173\u95ed\u5f39\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5207\u6362\u5230iframe<\/p>\n<p>driver.switch_to.frame(&#39;iframe_name_or_id&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u5e76\u70b9\u51fb\u5173\u95ed\u6309\u94ae<\/strong><\/h2>\n<p>close_button = driver.find_element(By.CLASS_NAME, &#39;custom-close-button&#39;)<\/p>\n<p>close_button.click()<\/p>\n<h2><strong>\u5207\u6362\u56de\u4e3b\u5185\u5bb9<\/strong><\/h2>\n<p>driver.switch_to.default_content()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528Python\u548cSelenium\u5173\u95edWeb\u9875\u9762\u4e2d\u7684\u5404\u79cd\u5f39\u6846\u3002\u6839\u636e\u5177\u4f53\u7684\u5f39\u6846\u7c7b\u578b\u548c\u5b9e\u73b0\u65b9\u5f0f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5f39\u6846\u3002Selenium\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\uff0c\u53ef\u4ee5\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684Web\u9875\u9762\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u81ea\u52a8\u5173\u95ed\u7f51\u9875\u4e2d\u7684\u5f39\u6846\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002Selenium\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u81ea\u52a8\u8bc6\u522b\u5e76\u5173\u95ed\u5f39\u6846\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5b9a\u4f4d\u5230\u5f39\u6846\u7684\u5173\u95ed\u6309\u94ae\u5e76\u6267\u884c\u70b9\u51fb\u64cd\u4f5c\u3002\u5728\u4f7f\u7528\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b89\u88c5Selenium\uff0c\u5e76\u4e14\u5df2\u4e0b\u8f7d\u4e0e\u6d4f\u89c8\u5668\u7248\u672c\u5339\u914d\u7684WebDriver\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7f51\u9875\u52a0\u8f7d\u65f6\u51fa\u73b0\u7684\u5f39\u6846\uff1f<\/strong><br \/>\u5728\u5904\u7406\u7f51\u9875\u52a0\u8f7d\u65f6\u7684\u5f39\u6846\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528WebDriverWait\u6765\u7b49\u5f85\u5f39\u6846\u51fa\u73b0\u5e76\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u8bbe\u7f6e\u5408\u9002\u7684\u8d85\u65f6\u65f6\u95f4\u548c\u6761\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5728\u5f39\u6846\u52a0\u8f7d\u540e\u81ea\u52a8\u6267\u884c\u5173\u95ed\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u811a\u672c\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u7528Python\u8bc6\u522b\u591a\u79cd\u7c7b\u578b\u7684\u5f39\u6846\u5e76\u5173\u95ed\u5b83\u4eec\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u53ef\u4ee5\u901a\u8fc7Selenium\u8bc6\u522b\u591a\u79cd\u7c7b\u578b\u7684\u5f39\u6846\uff0c\u5305\u62ec\u6a21\u6001\u7a97\u53e3\u3001JavaScript\u5f39\u6846\u7b49\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5206\u6790DOM\u7ed3\u6784\uff0c\u5b9a\u4f4d\u4e0d\u540c\u7c7b\u578b\u7684\u5f39\u6846\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u8fdb\u884c\u5173\u95ed\u3002\u8fd9\u4f7f\u5f97\u60a8\u7684\u811a\u672c\u80fd\u591f\u7075\u6d3b\u5e94\u5bf9\u4e0d\u540c\u573a\u666f\u4e2d\u7684\u5f39\u6846\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5173\u95edweb\u9875\u9762\u4e2d\u7684\u5f39\u6846\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528Selenium\u5e93\u3001\u5904\u7406Alert\u5bf9\u8c61\u3001\u6a21\u62df\u7528\u6237\u64cd [&hellip;]","protected":false},"author":3,"featured_media":1104435,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104425"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1104425"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104425\/revisions"}],"predecessor-version":[{"id":1104437,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104425\/revisions\/1104437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104435"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}