{"id":1139253,"date":"2025-01-08T22:08:26","date_gmt":"2025-01-08T14:08:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139253.html"},"modified":"2025-01-08T22:08:29","modified_gmt":"2025-01-08T14:08:29","slug":"python%e5%a6%82%e4%bd%95%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e4%b8%aa%e6%b5%8f%e8%a7%88%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139253.html","title":{"rendered":"Python\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u6d4f\u89c8\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102623\/f16dd7cc-0976-463a-8476-b2af1005a09a.webp\" alt=\"Python\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u6d4f\u89c8\u5668\" \/><\/p>\n<p><p> <strong>Python \u521d\u59cb\u5316\u6d4f\u89c8\u5668\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u5e93\u3001\u4f7f\u7528WebDriver\u3001\u4f7f\u7528Headless\u6d4f\u89c8\u5668\u6280\u672f<\/strong>\u3002\u5176\u4e2d\uff0cSelenium\u4e0eWebDriver\u662f\u6700\u5e38\u89c1\u548c\u5e7f\u6cdb\u4f7f\u7528\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Selenium\u548cWebDriver\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u6d4f\u89c8\u5668\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u53ef\u80fd\u7684\u65b9\u6cd5\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001Selenium\u7b80\u4ecb<\/strong><\/h2>\n<p><p>Selenium \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002\u5b83\u652f\u6301\u591a\u79cd\u6d4f\u89c8\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u6d4b\u8bd5\u811a\u672c\uff0c\u5305\u62ecPython\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Selenium<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5b89\u88c5Selenium\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4e0b\u8f7d\u548c\u914d\u7f6eWebDriver<\/h3>\n<\/p>\n<p><p>Selenium\u9700\u8981\u4e00\u4e2aWebDriver\u6765\u4e0e\u6d4f\u89c8\u5668\u8fdb\u884c\u4ea4\u4e92\u3002\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u6709\u4e0d\u540c\u7684WebDriver\uff0c\u4f8b\u5982\uff0cChrome\u4f7f\u7528\u7684\u662fChromeDriver\uff0cFirefox\u4f7f\u7528\u7684\u662fGeckoDriver\u3002\u4f60\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\u4e0b\u8f7d\u76f8\u5e94\u7684WebDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u8f7d\u94fe\u63a5\uff1a<\/p>\n<\/p>\n<ul>\n<li><a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\">ChromeDriver<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/mozilla\/geckodriver\/releases\">GeckoDriver<\/a><\/li>\n<\/ul>\n<p><h3>3\u3001\u4f7f\u7528Selenium\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u548cChromeDriver\u521d\u59cb\u5316Chrome\u6d4f\u89c8\u5668\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver\u7684\u8def\u5f84<\/strong><\/h2>\n<p>driver_path = &#39;path\/to\/chromedriver&#39;<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=driver_path)<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u7f51\u5740<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001WebDriver\u7b80\u4ecb<\/strong><\/h2>\n<p><p>WebDriver \u662fSelenium\u9879\u76ee\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u7f16\u7a0b\u63a5\u53e3\u6765\u4e0e\u6d4f\u89c8\u5668\u8fdb\u884c\u4ea4\u4e92\u3002WebDriver\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5b83\u53ef\u4ee5\u4e0e\u591a\u4e2a\u6d4f\u89c8\u5668\u517c\u5bb9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>1\u3001WebDriver\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>WebDriver\u7684\u4f7f\u7528\u975e\u5e38\u76f4\u89c2\uff0c\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528Python\u4ee3\u7801\u6765\u63a7\u5236\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u7f51\u5740<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u67e5\u627e\u5143\u7d20\u5e76\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>search_box = driver.find_element_by_name(&quot;q&quot;)<\/p>\n<p>search_box.send_keys(&quot;Selenium&quot;)<\/p>\n<p>search_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762\u6807\u9898<\/strong><\/h2>\n<p>print(driver.title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5f88\u591a\u73b0\u4ee3\u7f51\u7ad9\u4f7f\u7528JavaScript\u6765\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u3002\u4f7f\u7528WebDriver\uff0c\u4f60\u53ef\u4ee5\u7b49\u5f85\u8fd9\u4e9b\u5185\u5bb9\u52a0\u8f7d\u5b8c\u6210\uff0c\u7136\u540e\u8fdb\u884c\u64cd\u4f5c\u3002<\/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.support.ui import WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u7f51\u5740<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<p>try:<\/p>\n<p>    # \u7b49\u5f85\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>    element = WebDriverWait(driver, 10).until(<\/p>\n<p>        EC.presence_of_element_located((By.ID, &quot;myElement&quot;))<\/p>\n<p>    )<\/p>\n<p>finally:<\/p>\n<p>    driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001Headless\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p><p>Headless\u6d4f\u89c8\u5668\u662f\u6307\u6ca1\u6709\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u6d4f\u89c8\u5668\uff0c\u9002\u7528\u4e8e\u670d\u52a1\u5668\u73af\u5883\u4e0b\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002Selenium\u652f\u6301\u4f7f\u7528Headless\u6a21\u5f0f\u6765\u8fd0\u884c\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528Chrome Headless\u6a21\u5f0f<\/h3>\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>\u8bbe\u7f6eChrome\u4e3aHeadless\u6a21\u5f0f<\/strong><\/h2>\n<p>chrome_options = Options()<\/p>\n<p>chrome_options.add_argument(&quot;--headless&quot;)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;, options=chrome_options)<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u7f51\u5740<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762\u6807\u9898<\/strong><\/h2>\n<p>print(driver.title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Firefox Headless\u6a21\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.firefox.options import Options<\/p>\n<h2><strong>\u8bbe\u7f6eFirefox\u4e3aHeadless\u6a21\u5f0f<\/strong><\/h2>\n<p>firefox_options = Options()<\/p>\n<p>firefox_options.add_argument(&quot;--headless&quot;)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Firefox(executable_path=&#39;path\/to\/geckodriver&#39;, options=firefox_options)<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u7f51\u5740<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762\u6807\u9898<\/strong><\/h2>\n<p>print(driver.title)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n<p><p>\u5728\u4f7f\u7528Selenium\u548cWebDriver\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u663e\u5f0f\u7b49\u5f85<\/h3>\n<\/p>\n<p><p>\u663e\u5f0f\u7b49\u5f85\u53ef\u4ee5\u786e\u4fdd\u5728\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u64cd\u4f5c\uff0c\u4ece\u800c\u907f\u514d\u5f02\u5e38\u548c\u9519\u8bef\u3002<\/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>\u663e\u5f0f\u7b49\u5f85<\/strong><\/h2>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &quot;myElement&quot;))<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u9047\u5230\u95ee\u9898\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.common.exceptions import NoSuchElementException, TimeoutException<\/p>\n<p>try:<\/p>\n<p>    element = driver.find_element_by_id(&quot;myElement&quot;)<\/p>\n<p>except NoSuchElementException:<\/p>\n<p>    print(&quot;\u5143\u7d20\u672a\u627e\u5230&quot;)<\/p>\n<p>except TimeoutException:<\/p>\n<p>    print(&quot;\u52a0\u8f7d\u8d85\u65f6&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u7ba1\u7406\u6d4f\u89c8\u5668\u4f1a\u8bdd<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Selenium\u65f6\uff0c\u7ba1\u7406\u6d4f\u89c8\u5668\u4f1a\u8bdd\u53ef\u4ee5\u63d0\u9ad8\u8d44\u6e90\u5229\u7528\u7387\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6d4f\u89c8\u5668<\/p>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6267\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u9664\u4e86Selenium\u548cWebDriver\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u521d\u59cb\u5316\u6d4f\u89c8\u5668\uff0c\u4f8b\u5982\u4f7f\u7528Playwright\u548cPyppeteer\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528Playwright<\/h3>\n<\/p>\n<p><p>Playwright\u662f\u4e00\u4e2aNode.js\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6d4f\u89c8\u5668\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7Python\u63a5\u53e3\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from playwright.sync_api import sync_playwright<\/p>\n<p>with sync_playwright() as p:<\/p>\n<p>    browser = p.chromium.launch(headless=False)<\/p>\n<p>    page = browser.new_page()<\/p>\n<p>    page.goto(&#39;https:\/\/www.example.com&#39;)<\/p>\n<p>    print(page.title())<\/p>\n<p>    browser.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Pyppeteer<\/h3>\n<\/p>\n<p><p>Pyppeteer\u662fPuppeteer\u7684Python\u7248\u672c\uff0c\u652f\u6301Chrome\u548cChromium\u7684\u81ea\u52a8\u5316\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>from pyppeteer import launch<\/p>\n<p>async def main():<\/p>\n<p>    browser = await launch(headless=True)<\/p>\n<p>    page = await browser.newPage()<\/p>\n<p>    await page.goto(&#39;https:\/\/www.example.com&#39;)<\/p>\n<p>    title = await page.title()<\/p>\n<p>    print(title)<\/p>\n<p>    await browser.close()<\/p>\n<p>asyncio.get_event_loop().run_until_complete(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p><strong>Python \u521d\u59cb\u5316\u6d4f\u89c8\u5668\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u5e93\u3001\u4f7f\u7528WebDriver\u3001\u4f7f\u7528Headless\u6d4f\u89c8\u5668\u6280\u672f<\/strong>\u3002Selenium\u548cWebDriver\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6d4f\u89c8\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u3002\u901a\u8fc7\u663e\u5f0f\u7b49\u5f85\u548c\u5f02\u5e38\u5904\u7406\u7b49\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6b64\u5916\uff0cPlaywright\u548cPyppeteer\u7b49\u5de5\u5177\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u6d4f\u89c8\u5668\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521d\u59cb\u5316\u4e0d\u540c\u7c7b\u578b\u7684\u6d4f\u89c8\u5668\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u521d\u59cb\u5316\u591a\u79cd\u7c7b\u578b\u7684\u6d4f\u89c8\u5668\uff0c\u5305\u62ecChrome\u3001Firefox\u3001Safari\u7b49\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5e76\u786e\u4fddSelenium\u5df2\u5b89\u88c5\u3002\u4ee3\u7801\u793a\u4f8b\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\n# \u521d\u59cb\u5316Chrome\u6d4f\u89c8\u5668\ndriver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)\n\n# \u521d\u59cb\u5316Firefox\u6d4f\u89c8\u5668\ndriver = webdriver.Firefox(executable_path=&#39;path\/to\/geckodriver&#39;)\n\n# \u521d\u59cb\u5316Safari\u6d4f\u89c8\u5668\ndriver = webdriver.Safari()\n<\/code><\/pre>\n<p>\u6839\u636e\u60a8\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6d4f\u89c8\u5668\u548c\u9a71\u52a8\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ba1\u7406\u6d4f\u89c8\u5668\u7a97\u53e3\u548c\u6807\u7b7e\u9875\uff1f<\/strong><br \/>\u4f7f\u7528Selenium\u65f6\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u6d4f\u89c8\u5668\u7a97\u53e3\u548c\u6807\u7b7e\u9875\u3002\u53ef\u4ee5\u901a\u8fc7<code>driver.switch_to.window()<\/code>\u65b9\u6cd5\u5207\u6362\u6807\u7b7e\u9875\uff0c\u901a\u8fc7<code>driver.close()<\/code>\u5173\u95ed\u5f53\u524d\u6807\u7b7e\u9875\uff0c\u4f7f\u7528<code>driver.quit()<\/code>\u5173\u95ed\u6574\u4e2a\u6d4f\u89c8\u5668\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u6253\u5f00\u65b0\u7684\u6807\u7b7e\u9875\ndriver.execute_script(&quot;window.open(&#39;https:\/\/www.example.com&#39;);&quot;)\n\n# \u5207\u6362\u5230\u65b0\u6253\u5f00\u7684\u6807\u7b7e\u9875\ndriver.switch_to.window(driver.window_handles[1])\n\n# \u5173\u95ed\u5f53\u524d\u6807\u7b7e\u9875\ndriver.close()\n\n# \u5207\u6362\u56de\u4e4b\u524d\u7684\u6807\u7b7e\u9875\ndriver.switch_to.window(driver.window_handles[0])\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u521d\u59cb\u5316\u6d4f\u89c8\u5668\u65f6\u8bbe\u7f6e\u7279\u5b9a\u7684\u9009\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Selenium\u521d\u59cb\u5316\u6d4f\u89c8\u5668\u65f6\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u591a\u79cd\u9009\u9879\u6765\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u65e0\u5934\u6a21\u5f0f\u3001\u7981\u7528\u6269\u5c55\u3001\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9009\u9879\u8bbe\u7f6e\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nfrom selenium.webdriver.chrome.service import Service\nfrom selenium.webdriver.chrome.options import Options\n\n# \u8bbe\u7f6eChrome\u9009\u9879\nchrome_options = Options()\nchrome_options.add_argument(&quot;--headless&quot;)  # \u65e0\u5934\u6a21\u5f0f\nchrome_options.add_argument(&quot;--disable-extensions&quot;)  # \u7981\u7528\u6269\u5c55\nchrome_options.add_argument(&quot;window-size=1920,1080&quot;)  # \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\n\n# \u521d\u59cb\u5316Chrome\u6d4f\u89c8\u5668\ndriver = webdriver.Chrome(service=Service(&#39;path\/to\/chromedriver&#39;), options=chrome_options)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u9009\u9879\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316\u6d4f\u89c8\u5668\u7684\u6027\u80fd\u548c\u884c\u4e3a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u521d\u59cb\u5316\u6d4f\u89c8\u5668\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Selenium\u5e93\u3001\u4f7f\u7528WebDriver\u3001\u4f7f\u7528Headless [&hellip;]","protected":false},"author":3,"featured_media":1139263,"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\/1139253"}],"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=1139253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139253\/revisions"}],"predecessor-version":[{"id":1139267,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139253\/revisions\/1139267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139263"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}