{"id":1105627,"date":"2025-01-08T16:32:43","date_gmt":"2025-01-08T08:32:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105627.html"},"modified":"2025-01-08T16:32:45","modified_gmt":"2025-01-08T08:32:45","slug":"python%e5%a6%82%e4%bd%95%e6%a8%a1%e6%8b%9f%e6%b5%8f%e8%a7%88%e5%99%a8%e7%82%b9%e5%87%bb%e8%be%93%e5%85%a5%e5%af%86%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105627.html","title":{"rendered":"python\u5982\u4f55\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070432\/bd1f396c-dcb0-4ccb-9c95-cc58d3e4ef34.webp\" alt=\"python\u5982\u4f55\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801\" \/><\/p>\n<p><p> \u4e00\u3001Python\u5982\u4f55\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801<\/p>\n<\/p>\n<p><p><strong>Python\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001Selenium\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u3001\u5bf9\u7f51\u9875\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u4ea4\u4e92\u3001\u5b83\u652f\u6301\u5404\u79cd\u6d4f\u89c8\u5668\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1aSelenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\u3002\u901a\u8fc7Selenium\uff0c\u4f60\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u8fdb\u884c\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u70b9\u51fb\u6309\u94ae\u3001\u8f93\u5165\u6587\u672c\u3001\u63d0\u4ea4\u8868\u5355\u7b49\u3002\u8981\u4f7f\u7528Selenium\uff0c\u4f60\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff08\u5982ChromeDriver\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u548c\u8bbe\u7f6eSelenium<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Selenium\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u5b89\u88c5\u548c\u8bbe\u7f6e\u5de5\u4f5c\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Selenium\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5Selenium\u5e93\u540e\uff0c\u4f60\u8fd8\u9700\u8981\u4e0b\u8f7d\u76f8\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fChrome\u6d4f\u89c8\u5668\uff0c\u4f60\u9700\u8981\u4e0b\u8f7dChromeDriver\u3002\u4f60\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u7f51\u5740\u4e0b\u8f7dChromeDriver\uff1a<a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads<\/a><\/p>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5e76\u89e3\u538bChromeDriver\u540e\uff0c\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u5176\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7f16\u5199Python\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199Python\u4ee3\u7801\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com\/login&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u7528\u6237\u540d\u8f93\u5165\u6846\u5e76\u8f93\u5165\u7528\u6237\u540d<\/strong><\/h2>\n<p>username_input = driver.find_element(By.NAME, &#39;username&#39;)<\/p>\n<p>username_input.send_keys(&#39;your_username&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u5bc6\u7801\u8f93\u5165\u6846\u5e76\u8f93\u5165\u5bc6\u7801<\/strong><\/h2>\n<p>password_input = driver.find_element(By.NAME, &#39;password&#39;)<\/p>\n<p>password_input.send_keys(&#39;your_password&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u767b\u5f55\u6309\u94ae\u5e76\u70b9\u51fb<\/strong><\/h2>\n<p>login_button = driver.find_element(By.XPATH, &#39;\/\/*[@id=&quot;login-button&quot;]&#39;)<\/p>\n<p>login_button.click()<\/p>\n<h2><strong>\u7b49\u5f85\u51e0\u79d2\u949f\uff0c\u4ee5\u4fbf\u89c2\u5bdf\u7ed3\u679c<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6df1\u5165\u7406\u89e3Selenium\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>1\u3001\u67e5\u627e\u5143\u7d20<\/p>\n<\/p>\n<p><p>Selenium\u63d0\u4f9b\u4e86\u591a\u79cd\u67e5\u627e\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u901a\u8fc7ID\u3001\u540d\u79f0\u3001\u6807\u7b7e\u540d\u3001\u7c7b\u540d\u3001XPath\u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u7f51\u9875\u5143\u7d20\u7684\u5c5e\u6027\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = driver.find_element(By.ID, &#39;element_id&#39;)<\/p>\n<p>element = driver.find_element(By.NAME, &#39;element_name&#39;)<\/p>\n<p>element = driver.find_element(By.TAG_NAME, &#39;element_tag&#39;)<\/p>\n<p>element = driver.find_element(By.CLASS_NAME, &#39;element_class&#39;)<\/p>\n<p>element = driver.find_element(By.XPATH, &#39;element_xpath&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u8fd4\u56de\u4e00\u4e2aWebElement\u5bf9\u8c61\uff0c\u8868\u793a\u9875\u9762\u4e0a\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u64cd\u4f5c\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5bf9WebElement\u5bf9\u8c61\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8f93\u5165\u6587\u672c\u3001\u70b9\u51fb\u6309\u94ae\u3001\u83b7\u53d6\u6587\u672c\u5185\u5bb9\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element.send_keys(&#39;some text&#39;)  # \u8f93\u5165\u6587\u672c<\/p>\n<p>element.click()  # \u70b9\u51fb\u5143\u7d20<\/p>\n<p>text = element.text  # \u83b7\u53d6\u5143\u7d20\u6587\u672c\u5185\u5bb9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0cSelenium\u8fd8\u652f\u6301\u6a21\u62df\u952e\u76d8\u64cd\u4f5c\uff0c\u4f8b\u5982\u6309\u4e0bEnter\u952e\u3001Tab\u952e\u7b49\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Keys\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.keys import Keys<\/p>\n<p>element.send_keys(Keys.RETURN)  # \u6309\u4e0bEnter\u952e<\/p>\n<p>element.send_keys(Keys.TAB)  # \u6309\u4e0bTab\u952e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u7b49\u5f85<\/p>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u7b49\u5f85\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u73af\u8282\u3002Selenium\u63d0\u4f9b\u4e86\u663e\u5f0f\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85\u4e24\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u7b49\u5f85\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u9690\u5f0f\u7b49\u5f85<\/p>\n<\/p>\n<p><p>\u9690\u5f0f\u7b49\u5f85\u662f\u6307\u5728\u67e5\u627e\u5143\u7d20\u65f6\uff0c\u5982\u679c\u5143\u7d20\u672a\u7acb\u5373\u51fa\u73b0\uff0cSelenium\u4f1a\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u518d\u67e5\u627e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528implicitly_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u65b9\u6cd5\u6765\u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.implicitly_wait(10)  # \u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u65f6\u95f4\u4e3a10\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u663e\u5f0f\u7b49\u5f85<\/p>\n<\/p>\n<p><p>\u663e\u5f0f\u7b49\u5f85\u662f\u6307\u5728\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\u4e4b\u524d\uff0c\u7b49\u5f85\u7279\u5b9a\u6761\u4ef6\u6210\u7acb\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528WebDriverWait\u7c7b\u548cexpected_conditions\u6a21\u5757\u6765\u5b9e\u73b0\u663e\u5f0f\u7b49\u5f85\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/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\u5f85\u5143\u7d20\u51fa\u73b0<\/strong><\/h2>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &#39;element_id&#39;))<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5f39\u51fa\u7a97\u53e3\u548c\u6846\u67b6<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u5f39\u51fa\u7a97\u53e3\u548c\u6846\u67b6\uff08iframe\uff09\u3002Selenium\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u5207\u6362\u5230\u5f39\u51fa\u7a97\u53e3<\/p>\n<\/p>\n<p><p>\u5f53\u51fa\u73b0\u5f39\u51fa\u7a97\u53e3\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528switch_to.window\u65b9\u6cd5\u5207\u6362\u5230\u5f39\u51fa\u7a97\u53e3\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5f53\u524d\u7a97\u53e3\u53e5\u67c4<\/p>\n<p>main_window = driver.current_window_handle<\/p>\n<h2><strong>\u5207\u6362\u5230\u5f39\u51fa\u7a97\u53e3<\/strong><\/h2>\n<p>for handle in driver.window_handles:<\/p>\n<p>    if handle != main_window:<\/p>\n<p>        driver.switch_to.window(handle)<\/p>\n<p>        break<\/p>\n<h2><strong>\u5728\u5f39\u51fa\u7a97\u53e3\u4e2d\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<h2><strong>\u5207\u6362\u56de\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>driver.switch_to.window(main_window)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u5207\u6362\u5230\u6846\u67b6<\/p>\n<\/p>\n<p><p>\u5f53\u9875\u9762\u4e2d\u5305\u542b\u6846\u67b6\uff08iframe\uff09\u65f6\uff0c\u4f60\u9700\u8981\u4f7f\u7528switch_to.frame\u65b9\u6cd5\u5207\u6362\u5230\u6846\u67b6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5207\u6362\u5230\u6846\u67b6<\/p>\n<p>driver.switch_to.frame(&#39;frame_name&#39;)<\/p>\n<h2><strong>\u5728\u6846\u67b6\u4e2d\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<h2><strong>\u5207\u6362\u56de\u4e3b\u9875\u9762<\/strong><\/h2>\n<p>driver.switch_to.default_content()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406JavaScript\u5f39\u51fa\u6846<\/p>\n<\/p>\n<p><p>\u5728\u6d4f\u89c8\u5668\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230JavaScript\u5f39\u51fa\u6846\uff08\u5982alert\u3001confirm\u3001prompt\uff09\u3002Selenium\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u5f39\u51fa\u6846\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a5\u53d7alert\u5f39\u51fa\u6846<\/p>\n<p>alert = driver.switch_to.alert<\/p>\n<p>alert.accept()<\/p>\n<h2><strong>\u62d2\u7eddconfirm\u5f39\u51fa\u6846<\/strong><\/h2>\n<p>confirm = driver.switch_to.alert<\/p>\n<p>confirm.dismiss()<\/p>\n<h2><strong>\u8f93\u5165\u6587\u672c\u5230prompt\u5f39\u51fa\u6846<\/strong><\/h2>\n<p>prompt = driver.switch_to.alert<\/p>\n<p>prompt.send_keys(&#39;some text&#39;)<\/p>\n<p>prompt.accept()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d<\/p>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u64cd\u4f5c\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u3002Selenium\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u6587\u4ef6\u4e0a\u4f20<\/p>\n<\/p>\n<p><p>\u6587\u4ef6\u4e0a\u4f20\u901a\u5e38\u6d89\u53ca\u5230\u8f93\u5165\u6587\u4ef6\u8def\u5f84\u5230\u6587\u4ef6\u9009\u62e9\u5668\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528send_keys\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_input = driver.find_element(By.NAME, &#39;file&#39;)<\/p>\n<p>file_input.send_keys(&#39;\/path\/to\/your\/file&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6587\u4ef6\u4e0b\u8f7d<\/p>\n<\/p>\n<p><p>\u6587\u4ef6\u4e0b\u8f7d\u901a\u5e38\u6d89\u53ca\u5230\u5904\u7406\u6d4f\u89c8\u5668\u7684\u4e0b\u8f7d\u5bf9\u8bdd\u6846\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u6d4f\u89c8\u5668\u914d\u7f6e\u6765\u81ea\u52a8\u4e0b\u8f7d\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eChrome\u6d4f\u89c8\u5668\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528ChromeOptions\u6765\u914d\u7f6e\u81ea\u52a8\u4e0b\u8f7d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.chrome.options import Options<\/p>\n<h2><strong>\u914d\u7f6e\u81ea\u52a8\u4e0b\u8f7d<\/strong><\/h2>\n<p>chrome_options = Options()<\/p>\n<p>chrome_options.add_experimental_option(&#39;prefs&#39;, {<\/p>\n<p>    &#39;download.default_directory&#39;: &#39;\/path\/to\/download\/directory&#39;,<\/p>\n<p>    &#39;download.prompt_for_download&#39;: False,<\/p>\n<p>    &#39;download.directory_upgrade&#39;: True,<\/p>\n<p>    &#39;<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>browsing.enabled&#39;: True<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>driver = webdriver.Chrome(options=chrome_options)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406Cookies\u548c\u4f1a\u8bdd<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406Cookies\u548c\u4f1a\u8bdd\u4fe1\u606f\u3002Selenium\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u548c\u8bbe\u7f6eCookies\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u83b7\u53d6Cookies<\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528get_cookies\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u4f1a\u8bdd\u7684\u6240\u6709Cookies\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cookies = driver.get_cookies()<\/p>\n<p>for cookie in cookies:<\/p>\n<p>    print(cookie)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u8bbe\u7f6eCookies<\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528add_cookie\u65b9\u6cd5\u6765\u8bbe\u7f6e\u65b0\u7684Cookies\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cookie = {&#39;name&#39;: &#39;my_cookie&#39;, &#39;value&#39;: &#39;cookie_value&#39;}<\/p>\n<p>driver.add_cookie(cookie)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u7ed3\u675f\u81ea\u52a8\u5316\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u81ea\u52a8\u5316\u64cd\u4f5c\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528quit\u65b9\u6cd5\u6765\u5173\u95ed\u6d4f\u89c8\u5668\u5e76\u7ed3\u675f\u4f1a\u8bdd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u548cSelenium\u5e93\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\uff0c\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c\u3002Selenium\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7f51\u9875\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u64cd\u4f5c\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Selenium\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u5e76\u8f93\u5165\u5bc6\u7801\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u5305\u62ec\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\u3002Selenium\u80fd\u591f\u63a7\u5236\u6d4f\u89c8\u5668\uff0c\u6a21\u62df\u7528\u6237\u7684\u884c\u4e3a\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Selenium\u548c\u9002\u5408\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff08\u5982ChromeDriver\uff09\u3002\u901a\u8fc7\u7f16\u5199\u811a\u672c\uff0c\u60a8\u53ef\u4ee5\u81ea\u52a8\u5316\u8f93\u5165\u5bc6\u7801\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u4f7f\u7528Selenium\u8fdb\u884c\u5bc6\u7801\u8f93\u5165\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Selenium\u8fdb\u884c\u5bc6\u7801\u8f93\u5165\u65f6\uff0c\u786e\u4fdd\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\uff0c\u4ee5\u907f\u514d\u5143\u7d20\u672a\u52a0\u8f7d\u5bfc\u81f4\u7684\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528\u663e\u5f0f\u6216\u9690\u5f0f\u7b49\u5f85\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u9009\u62e9\u5668\uff08\u5982ID\u3001\u7c7b\u540d\u6216XPath\uff09\u6765\u627e\u5230\u5bc6\u7801\u8f93\u5165\u6846\u548c\u63d0\u4ea4\u6309\u94ae\uff0c\u4ee5\u786e\u4fdd\u8f93\u5165\u64cd\u4f5c\u987a\u5229\u8fdb\u884c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u901a\u8fc7\u54ea\u4e9b\u65b9\u6cd5\u63d0\u9ad8\u6a21\u62df\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\u7684\u6210\u529f\u7387\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6a21\u62df\u70b9\u51fb\u548c\u8f93\u5165\u5bc6\u7801\u7684\u6210\u529f\u7387\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a\u4f7f\u7528\u9002\u5f53\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u786e\u4fdd\u9875\u9762\u5b8c\u5168\u52a0\u8f7d\uff1b\u4f7f\u7528JavaScript\u6267\u884c\u70b9\u51fb\u64cd\u4f5c\u4f5c\u4e3a\u5907\u9009\u65b9\u6848\uff1b\u5728\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u786e\u4fdd\u6e05\u9664\u8f93\u5165\u6846\u4e2d\u7684\u4efb\u4f55\u9ed8\u8ba4\u6587\u672c\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u968f\u673a\u7684\u8f93\u5165\u5ef6\u8fdf\u53ef\u4ee5\u6a21\u62df\u4eba\u7c7b\u8f93\u5165\u7684\u81ea\u7136\u6027\uff0c\u4ece\u800c\u51cf\u5c11\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001Python\u5982\u4f55\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801 Python\u6a21\u62df\u6d4f\u89c8\u5668\u70b9\u51fb\u8f93\u5165\u5bc6\u7801\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u3001Se [&hellip;]","protected":false},"author":3,"featured_media":1105636,"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\/1105627"}],"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=1105627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105627\/revisions"}],"predecessor-version":[{"id":1105639,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105627\/revisions\/1105639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105636"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}