{"id":31,"date":"2026-07-02T10:00:00","date_gmt":"2026-07-02T02:00:00","guid":{"rendered":"http:\/\/localhost:8888\/?p=31"},"modified":"2026-07-09T16:17:44","modified_gmt":"2026-07-09T08:17:44","slug":"selenium4%e6%96%b0%e7%89%b9%e6%80%a7%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/blog.runnergo.cn\/?p=31","title":{"rendered":"Selenium 4\u65b0\u7279\u6027\u6df1\u5ea6\u89e3\u6790 &#8211; Relative Locator\u4e0eW3C\u534f\u8bae\u5b9e\u6218"},"content":{"rendered":"<h1>Selenium 4\u65b0\u7279\u6027\u6df1\u5ea6\u89e3\u6790 - Relative Locator\u4e0eW3C\u534f\u8bae\u5b9e\u6218<\/h1>\n\n&gt; \u4f5c\u8005\uff1a\u6d4b\u5f00\u8001\u738b\n---\n\n<h2>\u524d\u8a00<\/h2>\n\n\u5927\u5bb6\u597d\uff0c\u6211\u662f\u6d4b\u5f00\u8001\u738b\u3002\u505a\u81ea\u52a8\u5316\u6d4b\u8bd57\u5e74\uff0cSelenium\u7528\u4e865\u5e74\u591a\uff0c\u89c1\u8bc1\u4e86\u5b83\u4ece2.x\u52304.x\u7684\u6f14\u53d8\u3002\u4eca\u5929\u8fd9\u7bc7\uff0c\u6211\u60f3\u7cfb\u7edf\u804a\u804aSelenium 4\u7684\u65b0\u7279\u6027\uff0c\u4ee5\u53ca\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u3002\n\nSelenium 4\u57282021\u5e74\u5e95\u6b63\u5f0f\u53d1\u5e03\uff0c\u76f8\u6bd43.x\u7248\u672c\uff0c\u53d8\u5316\u5f88\u5927\uff1a\n<ul><li>\u5b8c\u5168\u652f\u6301W3C\u6807\u51c6\u534f\u8bae<\/li>\n<li>\u5168\u65b0\u7684Relative Locator\uff08\u76f8\u5bf9\u5b9a\u4f4d\u5668\uff09<\/li>\n<li>\u91cd\u6784\u7684Grid\u67b6\u6784<\/li>\n<li>\u66f4\u597d\u7684Chrome DevTools\u652f\u6301<\/li>\n<li>\u7a97\u53e3\u7ba1\u7406API\u589e\u5f3a<\/li>\n<\/ul>\n\u8fd9\u4e9b\u53d8\u5316\u5bf9\u6211\u4eec\u5199\u81ea\u52a8\u5316\u811a\u672c\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f\u4e0b\u9762\u4e00\u4e00\u5206\u6790\u3002\n\n---\n\n<h2>\u4e00\u3001W3C\u534f\u8bae\uff1a\u544a\u522bJSON Wire Protocol<\/h2>\n\n<h3>\u4ec0\u4e48\u662fW3C\u534f\u8bae\uff1f<\/h3>\n\n\u5728Selenium 3\u65f6\u4ee3\uff0cWebDriver\u4f7f\u7528\u7684\u662fJSON Wire Protocol\uff0c\u8fd9\u662fSelenium\u81ea\u5b9a\u4e49\u7684\u534f\u8bae\u3002\u4e0d\u540c\u6d4f\u89c8\u5668\u5382\u5546\u9700\u8981\u5b9e\u73b0\u81ea\u5df1\u7684Driver\uff08\u5982chromedriver\u3001geckodriver\uff09\uff0c\u534f\u8bae\u5b9e\u73b0\u6709\u5dee\u5f02\uff0c\u5bfc\u81f4\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\u3002\n\nSelenium 4\u91c7\u7528\u4e86W3C WebDriver\u6807\u51c6\u534f\u8bae\uff0c\u8fd9\u610f\u5473\u7740\uff1a\n<ul><li>\u6d4f\u89c8\u5668Driver\u5b9e\u73b0\u66f4\u7edf\u4e00<\/li>\n<li>API\u884c\u4e3a\u66f4\u4e00\u81f4<\/li>\n<li>\u8de8\u6d4f\u89c8\u5668\u6d4b\u8bd5\u66f4\u7a33\u5b9a<\/li>\n<\/ul>\n<h3>\u5bf9\u6211\u4eec\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/h3>\n\n\u597d\u6d88\u606f\uff1a<strong>\u73b0\u6709\u811a\u672c\u57fa\u672c\u4e0d\u7528\u6539<\/strong>\u3002Selenium 4\u5411\u540e\u517c\u5bb9\uff0c\u5927\u90e8\u5206API\u4fdd\u6301\u4e0d\u53d8\u3002\n\n\u4f46\u6709\u4e00\u4e9b\u7ec6\u8282\u9700\u8981\u6ce8\u610f\uff1a\n\n<code>`<\/code>python\n<h1>Selenium 3 \u7684\u5199\u6cd5\uff08\u4ecd\u7136\u652f\u6301\uff09<\/h1>\ndriver.find_element_by_id(\"username\")\ndriver.find_element_by_css_selector(\".btn-primary\")\n\n<h1>Selenium 4 \u63a8\u8350\u7684\u5199\u6cd5<\/h1>\nfrom selenium.webdriver.common.by import By\n\ndriver.find_element(By.ID, \"username\")\ndriver.find_element(By.CSS_SELECTOR, \".btn-primary\")\n<code>`<\/code>\n\n\u65e7\u65b9\u6cd5\u4f1a\u663e\u793aDeprecationWarning\uff0c\u5efa\u8bae\u7edf\u4e00\u6539\u6210\u65b0\u5199\u6cd5\u3002\n\n<h3>\u5b9e\u9645\u8e29\u5751<\/h3>\n\n\u6211\u5728\u5347\u7ea7\u4e00\u4e2a\u8001\u9879\u76ee\u65f6\uff0c\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\n\n<code>`<\/code>python\n<h1>\u8fd9\u884c\u4ee3\u7801\u5728Selenium 3\u53ef\u4ee5\u8fd0\u884c<\/h1>\nelement = driver.find_element_by_xpath(\"\/\/div[@class='content']\")\n\n<h1>Selenium 4 \u4f1a\u62a5\u8b66\u544a<\/h1>\n<h1>DeprecationWarning: find_element_by_* is deprecated. Please use find_element() instead<\/h1>\n<code>`<\/code>\n\n<strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u5199\u4e00\u4e2a\u517c\u5bb9\u5c42\u811a\u672c\uff0c\u6279\u91cf\u66ff\u6362\u65e7\u65b9\u6cd5\u3002\n\n<code>`<\/code>python\n<h1>\u517c\u5bb9\u5c42\u793a\u4f8b<\/h1>\ndef find_by_id(driver, element_id):\n    return driver.find_element(By.ID, element_id)\n\ndef find_by_xpath(driver, xpath):\n    return driver.find_element(By.XPATH, xpath)\n<code>`<\/code>\n\n---\n\n<h2>\u4e8c\u3001Relative Locator\uff1a\u9769\u547d\u6027\u7684\u5b9a\u4f4d\u65b9\u5f0f<\/h2>\n\n\u8fd9\u662fSelenium 4\u6700\u8ba9\u4eba\u5174\u594b\u7684\u65b0\u7279\u6027\uff01\n\n<h3>\u4f20\u7edf\u5b9a\u4f4d\u7684\u75db\u70b9<\/h3>\n\n\u4ee5\u524d\u5b9a\u4f4d\u5143\u7d20\uff0c\u53ea\u80fd\u7528\uff1a\n<ul><li>ID\u3001Name\u3001ClassName<\/li>\n<li>XPath\u3001CSS Selector<\/li>\n<li>LinkText\u3001PartialLinkText<\/li>\n<\/ul>\n\u4f46\u8fd9\u4e9b\u5b9a\u4f4d\u5668\u90fd\u662f\u57fa\u4e8e\u5143\u7d20\u81ea\u8eab\u7684\u5c5e\u6027\u3002\u5982\u679c\u5143\u7d20\u6ca1\u6709ID\u3001\u6ca1\u6709\u7a33\u5b9a\u7684class\uff0cXPath\u53c8\u5f88\u957f\u5f88\u8106\u5f31\uff0c\u600e\u4e48\u529e\uff1f\n\n<h3>Relative Locator\u767b\u573a<\/h3>\n\nRelative Locator\u5141\u8bb8\u6211\u4eec\u6839\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u7a7a\u95f4\u5173\u7cfb\u6765\u5b9a\u4f4d\uff1a\n\n<ul><li><code>above()<\/code> - \u5728\u67d0\u5143\u7d20\u4e0a\u65b9<\/li>\n<li><code>below()<\/code> - \u5728\u67d0\u5143\u7d20\u4e0b\u65b9<\/li>\n<li><code>toLeftOf()<\/code> - \u5728\u67d0\u5143\u7d20\u5de6\u4fa7<\/li>\n<li><code>toRightOf()<\/code> - \u5728\u67d0\u5143\u7d20\u53f3\u4fa7<\/li>\n<li><code>near()<\/code> - \u5728\u67d0\u5143\u7d20\u9644\u8fd1<\/li>\n<\/ul>\n<h3>\u5b9e\u6218\u793a\u4f8b<\/h3>\n\n\u5047\u8bbe\u6709\u4e00\u4e2a\u767b\u5f55\u8868\u5355\uff1a\n\n<code>`<\/code>html\n\n    <img decoding=\"async\" src=\"logo.png\" alt=\"Logo\">\n    \n    \n    <button type=\"submit\">\u767b\u5f55<\/button>\n\n<code>`<\/code>\n\n\u8f93\u5165\u6846\u6ca1\u6709ID\uff0cclass\u4e5f\u662f\u52a8\u6001\u751f\u6210\u7684\u3002\u7528\u4f20\u7edf\u65b9\u5f0f\u5b9a\u4f4d\u5f88\u9ebb\u70e6\u3002\n\n<strong>\u7528Relative Locator\uff1a<\/strong>\n\n<code>`<\/code>python\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.relative_locator import locate_with\n\ndriver = webdriver.Chrome()\ndriver.get(\"https:\/\/example.com\/login\")\n\n<h1>\u5148\u5b9a\u4f4dLogo<\/h1>\nlogo = driver.find_element(By.TAG_NAME, \"img\")\n\n<h1>\u5b9a\u4f4d\u7528\u6237\u540d\u8f93\u5165\u6846\uff08\u5728Logo\u4e0b\u65b9\uff09<\/h1>\nusername = driver.find_element(locate_with(By.TAG_NAME, \"input\").below(logo))\n\n<h1>\u5b9a\u4f4d\u5bc6\u7801\u8f93\u5165\u6846\uff08\u5728\u7528\u6237\u540d\u4e0b\u65b9\uff09<\/h1>\npassword = driver.find_element(locate_with(By.TAG_NAME, \"input\").below(username))\n\n<h1>\u5b9a\u4f4d\u767b\u5f55\u6309\u94ae\uff08\u5728\u5bc6\u7801\u4e0b\u65b9\uff09<\/h1>\nlogin_btn = driver.find_element(locate_with(By.TAG_NAME, \"button\").below(password))\n<code>`<\/code>\n\n<h3>\u7ec4\u5408\u4f7f\u7528<\/h3>\n\nRelative Locator\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff1a\n\n<code>`<\/code>python\nfrom selenium.webdriver.support.relative_locator import locate_with\n\n<h1>\u5b9a\u4f4d\u5728Logo\u4e0b\u65b9\u3001\u4e14\u5728\u53f3\u4fa7\u680f\u5185\u7684\u5143\u7d20<\/h1>\nelement = driver.find_element(\n    locate_with(By.TAG_NAME, \"input\")\n    .below(logo)\n    .to_right_of(sidebar)\n)\n<code>`<\/code>\n\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n\nRelative Locator\u7279\u522b\u9002\u5408\u8fd9\u4e9b\u573a\u666f\uff1a\n\n1. <strong>\u8868\u5355\u5e03\u5c40\u56fa\u5b9a\u4f46\u5143\u7d20\u5c5e\u6027\u4e0d\u7a33\u5b9a<\/strong>\n   - \u8f93\u5165\u6846\u6ca1\u6709\u56fa\u5b9aID\n   - \u6839\u636eLabel\u6587\u672c\u5b9a\u4f4d\u65c1\u8fb9\u8f93\u5165\u6846\n\n2. <strong>\u54cd\u5e94\u5f0f\u5e03\u5c40\u6d4b\u8bd5<\/strong>\n   - \u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u4e0b\u5143\u7d20\u4f4d\u7f6e\u53d8\u5316\n   - \u76f8\u5bf9\u4f4d\u7f6e\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8\n\n3. <strong>\u52a8\u6001\u5185\u5bb9\u9875\u9762<\/strong>\n   - \u5217\u8868\u9879\u6ca1\u6709\u56fa\u5b9a\u5c5e\u6027\n   - \u4f46\u76f8\u5bf9\u4f4d\u7f6e\u5173\u7cfb\u7a33\u5b9a\n\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n<ul><li>Relative Locator\u4f9d\u8d56\u5143\u7d20\u5728\u9875\u9762\u4e0a\u7684\u5b9e\u9645\u4f4d\u7f6e<\/li>\n<li>\u5143\u7d20\u4e0d\u53ef\u89c1\u65f6\u5b9a\u4f4d\u4f1a\u5931\u8d25<\/li>\n<li>\u590d\u6742\u5e03\u5c40\u53ef\u80fd\u6709\u6b67\u4e49\uff08\u6bd4\u5982\u591a\u4e2a\u5143\u7d20\u90fd\u7b26\u5408\u6761\u4ef6\uff09<\/li>\n<\/ul>\n---\n\n<h2>\u4e09\u3001WebDriverManager\u81ea\u52a8\u7ba1\u7406Driver<\/h2>\n\nSelenium 4\u4e0d\u518d\u5185\u7f6eDriver\u7ba1\u7406\uff0c\u63a8\u8350\u4f7f\u7528WebDriverManager\u3002\n\n<h3>\u4ee5\u524d\u7684\u75db\u70b9<\/h3>\n\n<code>`<\/code>python\n<h1>\u4ee5\u524d\u9700\u8981\u624b\u52a8\u4e0b\u8f7dchromedriver\uff0c\u653e\u5230\u6307\u5b9a\u8def\u5f84<\/h1>\ndriver = webdriver.Chrome(\"\/path\/to\/chromedriver\")\n\n<h1>\u95ee\u9898\uff1a<\/h1>\n<h1>1. Chrome\u66f4\u65b0\u540e\uff0cDriver\u7248\u672c\u4e0d\u5339\u914d<\/h1>\n<h1>2. \u4e0d\u540c\u73af\u5883\u8def\u5f84\u4e0d\u540c<\/h1>\n<h1>3. CI\/CD\u73af\u5883\u914d\u7f6e\u9ebb\u70e6<\/h1>\n<code>`<\/code>\n\n<h3>\u73b0\u5728\u7684\u89e3\u51b3\u65b9\u6848<\/h3>\n\n<code>`<\/code>python\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.service import Service\nfrom webdriver_manager.chrome import ChromeDriverManager\n\n<h1>\u81ea\u52a8\u4e0b\u8f7d\u548c\u7ba1\u7406Driver<\/h1>\ndriver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))\n<code>`<\/code>\n\nWebDriverManager\u4f1a\uff1a\n<ul><li>\u81ea\u52a8\u68c0\u6d4b\u6d4f\u89c8\u5668\u7248\u672c<\/li>\n<li>\u4e0b\u8f7d\u5339\u914d\u7684Driver<\/li>\n<li>\u7f13\u5b58\u5230\u672c\u5730\uff0c\u4e0d\u7528\u91cd\u590d\u4e0b\u8f7d<\/li>\n<\/ul>\n---\n\n<h2>\u56db\u3001Chrome DevTools\u652f\u6301<\/h2>\n\nSelenium 4\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Chrome DevTools\u534f\u8bae\uff0c\u5b9e\u73b0\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002\n\n<h3>\u6a21\u62df\u7f51\u7edc\u6761\u4ef6<\/h3>\n\n<code>`<\/code>python\nfrom selenium import webdriver\nfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n\ndriver = webdriver.Chrome()\n\n<h1>\u542f\u7528\u7f51\u7edc\u6a21\u62df<\/h1>\ndriver.execute_cdp_cmd(\"Network.enable\", {})\n\n<h1>\u6a21\u62df3G\u7f51\u7edc<\/h1>\ndriver.execute_cdp_cmd(\"Network.emulateNetworkConditions\", {\n    \"offline\": False,\n    \"latency\": 100,  # \u5ef6\u8fdf100ms\n    \"downloadThroughput\": 500 * 1024,  # \u4e0b\u8f7d\u901f\u5ea6 500KB\/s\n    \"uploadThroughput\": 500 * 1024,    # \u4e0a\u4f20\u901f\u5ea6 500KB\/s\n})\n\ndriver.get(\"https:\/\/example.com\")\n<code>`<\/code>\n\n<h3>\u6a21\u62df\u5730\u7406\u4f4d\u7f6e<\/h3>\n\n<code>`<\/code>python\n<h1>\u6a21\u62dfGPS\u4f4d\u7f6e<\/h1>\ndriver.execute_cdp_cmd(\"Emulation.setGeolocationOverride\", {\n    \"latitude\": 39.9042,  # \u5317\u4eac\n    \"longitude\": 116.4074,\n    \"accuracy\": 100\n})\n\ndriver.get(\"https:\/\/example.com\/map\")\n<code>`<\/code>\n\n<h3>\u622a\u53d6\u7279\u5b9a\u5143\u7d20<\/h3>\n\n<code>`<\/code>python\n<h1>\u622a\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u622a\u56fe<\/h1>\nelement = driver.find_element(By.ID, \"chart\")\nscreenshot = element.screenshot_as_png\n\nwith open(\"chart.png\", \"wb\") as f:\n    f.write(screenshot)\n<code>`<\/code>\n\n---\n\n<h2>\u4e94\u3001\u7a97\u53e3\u7ba1\u7406\u589e\u5f3a<\/h2>\n\nSelenium 4\u65b0\u589e\u4e86\u7a97\u53e3\u7ba1\u7406API\uff1a\n\n<code>`<\/code>python\n<h1>\u521b\u5efa\u65b0\u6807\u7b7e\u9875<\/h1>\ndriver.switch_to.new_window(\"tab\")\n\n<h1>\u521b\u5efa\u65b0\u7a97\u53e3<\/h1>\ndriver.switch_to.new_window(\"window\")\n\n<h1>\u83b7\u53d6\u7a97\u53e3\u4fe1\u606f<\/h1>\nwindow_rect = driver.get_window_rect()\nprint(window_rect)  # {'x': 10, 'y': 10, 'width': 1200, 'height': 800}\n\n<h1>\u8bbe\u7f6e\u7a97\u53e3\u4f4d\u7f6e\u548c\u5927\u5c0f<\/h1>\ndriver.set_window_rect(x=100, y=100, width=1024, height=768)\n\n<h1>\u6700\u5c0f\u5316\u7a97\u53e3<\/h1>\ndriver.minimize_window()\n\n<h1>\u5168\u5c4f<\/h1>\ndriver.fullscreen_window()\n<code>`<\/code>\n\n---\n\n<h2>\u516d\u3001Grid 4\u67b6\u6784\u91cd\u6784<\/h2>\n\nSelenium Grid 4\u5b8c\u5168\u91cd\u6784\u4e86\u67b6\u6784\uff1a\n\n<h3>\u65b0\u67b6\u6784\u7ec4\u4ef6<\/h3>\n\n<ul><li><strong>Router<\/strong>\uff1a\u63a5\u6536\u8bf7\u6c42\uff0c\u5206\u53d1\u5230\u5bf9\u5e94\u7ec4\u4ef6<\/li>\n<li><strong>Distributor<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684Node\u6267\u884c\u6d4b\u8bd5<\/li>\n<li><strong>Session Map<\/strong>\uff1a\u5b58\u50a8Session\u4e0eNode\u7684\u6620\u5c04\u5173\u7cfb<\/li>\n<li><strong>Session Queue<\/strong>\uff1a\u65b0Session\u8bf7\u6c42\u961f\u5217<\/li>\n<li><strong>Nodes<\/strong>\uff1a\u5b9e\u9645\u6267\u884c\u6d4b\u8bd5\u7684\u8282\u70b9<\/li>\n<\/ul>\n<h3>\u542f\u52a8Standalone\u6a21\u5f0f<\/h3>\n\n<code>`<\/code>bash\njava -jar selenium-server-4.x.jar standalone\n<code>`<\/code>\n\n<h3>\u542f\u52a8Hub-Node\u6a21\u5f0f<\/h3>\n\n<code>`<\/code>bash\n<h1>\u542f\u52a8Hub<\/h1>\njava -jar selenium-server-4.x.jar hub\n\n<h1>\u542f\u52a8Node\uff08\u8fde\u63a5\u5230Hub\uff09<\/h1>\njava -jar selenium-server-4.x.jar node --hub http:\/\/hub-ip:4444\n<code>`<\/code>\n\n<h3>Docker\u90e8\u7f72<\/h3>\n\n<code>`<\/code>bash\n<h1>\u542f\u52a8Standalone<\/h1>\ndocker run -d -p 4444:4444 selenium\/standalone-chrome\n\n<h1>\u542f\u52a8Hub<\/h1>\ndocker run -d -p 4444:4444 selenium\/hub\n\n<h1>\u542f\u52a8Node<\/h1>\ndocker run -d --net host selenium\/node-chrome\n<code>`<\/code>\n\n---\n\n<h2>\u4e03\u3001\u5b9e\u6218\uff1aSelenium 4\u81ea\u52a8\u5316\u6846\u67b6\u6a21\u677f<\/h2>\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8eSelenium 4 + Pytest\u7684\u81ea\u52a8\u5316\u6846\u67b6\u793a\u4f8b\uff1a\n\n<code>`<\/code>python\n<h1>conftest.py<\/h1>\nimport pytest\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.service import Service\nfrom webdriver_manager.chrome import ChromeDriverManager\n\n@pytest.fixture(scope=\"function\")\ndef driver():\n    \"\"\"\u521d\u59cb\u5316WebDriver\"\"\"\n    options = webdriver.ChromeOptions()\n    options.add_argument(\"--headless\")  # \u65e0\u5934\u6a21\u5f0f\n    options.add_argument(\"--no-sandbox\")\n    options.add_argument(\"--disable-dev-shm-usage\")\n\n    driver = webdriver.Chrome(\n        service=Service(ChromeDriverManager().install()),\n        options=options\n    )\n    driver.maximize_window()\n\n    yield driver\n\n    driver.quit()\n\n\n<h1>test_login.py<\/h1>\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.relative_locator import locate_with\n\ndef test_login(driver):\n    \"\"\"\u6d4b\u8bd5\u767b\u5f55\u529f\u80fd\"\"\"\n    driver.get(\"https:\/\/example.com\/login\")\n\n    # \u4f7f\u7528Relative Locator\u5b9a\u4f4d\n    logo = driver.find_element(By.TAG_NAME, \"img\")\n    username = driver.find_element(locate_with(By.TAG_NAME, \"input\").below(logo))\n    password = driver.find_element(locate_with(By.TAG_NAME, \"input\").below(username))\n\n    username.send_keys(\"testuser\")\n    password.send_keys(\"password123\")\n\n    login_btn = driver.find_element(By.CSS_SELECTOR, \"button[type='submit']\")\n    login_btn.click()\n\n    # \u9a8c\u8bc1\u767b\u5f55\u6210\u529f\n    assert \"dashboard\" in driver.current_url\n<code>`<\/code>\n\n---\n\n<h2>\u516b\u3001\u5347\u7ea7\u5efa\u8bae<\/h2>\n\n\u5982\u679c\u4f60\u7684\u9879\u76ee\u8fd8\u5728\u7528Selenium 3\uff0c\u5347\u7ea7\u52304\u7684\u5efa\u8bae\uff1a\n\n1. <strong>\u5148\u5347\u7ea7\u4f9d\u8d56<\/strong>\n   <code>`<\/code>bash\n   pip install selenium==4.x.x\n   pip install webdriver-manager\n   <code>`<\/code>\n\n2. <strong>\u66f4\u65b0\u5b9a\u4f4d\u5668\u5199\u6cd5<\/strong>\n   - \u5168\u5c40\u641c\u7d22 <code>find_element_by_<\/code>\n   - \u66ff\u6362\u4e3a <code>find_element(By.*, ...)<\/code>\n\n3. <strong>\u6d4b\u8bd5Relative Locator<\/strong>\n   - \u627e\u51e0\u4e2a\u590d\u6742\u5b9a\u4f4d\u573a\u666f\n   - \u5c1d\u8bd5\u7528Relative Locator\u7b80\u5316\n\n4. <strong>\u8bc4\u4f30Grid\u5347\u7ea7<\/strong>\n   - \u5982\u679c\u7528\u4e86Grid\uff0c\u8bc4\u4f30\u662f\u5426\u5347\u7ea7\u52304.x\n   - Docker\u90e8\u7f72\u66f4\u7b80\u5355\n\n---\n\n<h2>\u603b\u7ed3<\/h2>\n\nSelenium 4\u662f\u4e00\u6b21\u91cd\u8981\u7684\u7248\u672c\u5347\u7ea7\uff0c\u4e3b\u8981\u6536\u76ca\uff1a\n\n| \u7279\u6027 | \u6536\u76ca |\n|------|------|\n| W3C\u534f\u8bae | \u8de8\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u66f4\u597d |\n| Relative Locator | \u5b9a\u4f4d\u5143\u7d20\u66f4\u7075\u6d3b |\n| DevTools\u652f\u6301 | \u7f51\u7edc\u6a21\u62df\u3001\u5730\u7406\u4f4d\u7f6e\u7b49\u9ad8\u7ea7\u529f\u80fd |\n| Grid\u91cd\u6784 | \u5206\u5e03\u5f0f\u6d4b\u8bd5\u66f4\u6613\u90e8\u7f72 |\n| API\u73b0\u4ee3\u5316 | \u4ee3\u7801\u66f4\u89c4\u8303\u3001\u53ef\u7ef4\u62a4 |\n\n\u5bf9\u4e8e\u65b0\u9879\u76ee\uff0c\u5efa\u8bae\u76f4\u63a5\u7528Selenium 4\u3002\u8001\u9879\u76ee\u5347\u7ea7\u98ce\u9669\u4e0d\u5927\uff0cAPI\u517c\u5bb9\u6027\u5f88\u597d\u3002\n\n---\n\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n\n<ul><li>Selenium\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/www.selenium.dev\/documentation\/<\/li>\n<li>Selenium 4 Release Notes\uff1ahttps:\/\/github.com\/SeleniumHQ\/selenium\/blob\/trunk\/java\/CHANGELOG<\/li>\n<li>WebDriver W3C\u6807\u51c6\uff1ahttps:\/\/w3c.github.io\/webdriver\/<\/li>\n<li>RunnerGo\u535a\u5ba2\uff1ahttps:\/\/www.runnergo.com\/blog\/<\/li>\n<\/ul>\n---\n\n<strong>\u4e0b\u671f\u9884\u544a\uff1aPlaywright vs Cypress\u5bf9\u51b3 - 2026\u5e74\u6700\u503c\u5f97\u5b66\u4e60\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6<\/strong>\n\n&gt; \u4f5c\u8005\uff1a\u6d4b\u5f00\u8001\u738b\uff0c7\u5e74\u6d4b\u8bd5\u5f00\u53d1\u7ecf\u9a8c\uff0c\u4e13\u6ce8Web\u81ea\u52a8\u5316\u548c\u6d4b\u8bd5\u6846\u67b6\u8bbe\u8ba1\u3002\u516c\u4f17\u53f7\uff1a\u6d4b\u5f00\u90a3\u4e9b\u4e8b\u513f","protected":false},"excerpt":{"rendered":"<p>Selenium 4\u65b0\u7279\u6027\u6df1\u5ea6\u89e3\u6790 &#8211; Relative Locator\u4e0eW3C\u534f\u8bae\u5b9e\u6218 &gt; \u4f5c\u8005\uff1a\u6d4b\u5f00\u8001\u738b &#8212; \u524d\u8a00 \u5927\u5bb6\u597d\uff0c\u6211\u662f\u6d4b\u5f00\u8001\u738b\u3002\u505a\u81ea\u52a8\u5316\u6d4b\u8bd57\u5e74\uff0cSelenium\u7528\u4e865\u5e74\u591a\uff0c\u89c1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":0,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.runnergo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}