{"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 &#8211; Relative Locator\u4e0eW3C\u534f\u8bae\u5b9e\u6218<\/h1>\n<p>&gt; \u4f5c\u8005\uff1a\u6d4b\u5f00\u8001\u738b<br \/>\n&#8212;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\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<\/p>\n<p>Selenium 4\u57282021\u5e74\u5e95\u6b63\u5f0f\u53d1\u5e03\uff0c\u76f8\u6bd43.x\u7248\u672c\uff0c\u53d8\u5316\u5f88\u5927\uff1a<\/p>\n<ul>\n<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<p>\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<\/p>\n<p>&#8212;<\/p>\n<h2>\u4e00\u3001W3C\u534f\u8bae\uff1a\u544a\u522bJSON Wire Protocol<\/h2>\n<h3>\u4ec0\u4e48\u662fW3C\u534f\u8bae\uff1f<\/h3>\n<p>\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<\/p>\n<p>Selenium 4\u91c7\u7528\u4e86W3C WebDriver\u6807\u51c6\u534f\u8bae\uff0c\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n<ul>\n<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<p>\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<\/p>\n<p>\u4f46\u6709\u4e00\u4e9b\u7ec6\u8282\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<p><code>`<\/code>python<\/p>\n<h1>Selenium 3 \u7684\u5199\u6cd5\uff08\u4ecd\u7136\u652f\u6301\uff09<\/h1>\n<p>driver.find_element_by_id(&#8220;username&#8221;)<br \/>\ndriver.find_element_by_css_selector(&#8220;.btn-primary&#8221;)<\/p>\n<h1>Selenium 4 \u63a8\u8350\u7684\u5199\u6cd5<\/h1>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>driver.find_element(By.ID, &#8220;username&#8221;)<br \/>\ndriver.find_element(By.CSS_SELECTOR, &#8220;.btn-primary&#8221;)<br \/>\n<code>`<\/code><\/p>\n<p>\u65e7\u65b9\u6cd5\u4f1a\u663e\u793aDeprecationWarning\uff0c\u5efa\u8bae\u7edf\u4e00\u6539\u6210\u65b0\u5199\u6cd5\u3002<\/p>\n<h3>\u5b9e\u9645\u8e29\u5751<\/h3>\n<p>\u6211\u5728\u5347\u7ea7\u4e00\u4e2a\u8001\u9879\u76ee\u65f6\uff0c\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p><code>`<\/code>python<\/p>\n<h1>\u8fd9\u884c\u4ee3\u7801\u5728Selenium 3\u53ef\u4ee5\u8fd0\u884c<\/h1>\n<p>element = driver.find_element_by_xpath(&#8220;\/\/div[@class=&#8217;content&#8217;]&#8221;)<\/p>\n<h1>Selenium 4 \u4f1a\u62a5\u8b66\u544a<\/h1>\n<h1>DeprecationWarning: find_element_by_* is deprecated. Please use find_element() instead<\/h1>\n<p><code>`<\/code><\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u5199\u4e00\u4e2a\u517c\u5bb9\u5c42\u811a\u672c\uff0c\u6279\u91cf\u66ff\u6362\u65e7\u65b9\u6cd5\u3002<\/p>\n<p><code>`<\/code>python<\/p>\n<h1>\u517c\u5bb9\u5c42\u793a\u4f8b<\/h1>\n<p>def find_by_id(driver, element_id):<br \/>\n    return driver.find_element(By.ID, element_id)<\/p>\n<p>def find_by_xpath(driver, xpath):<br \/>\n    return driver.find_element(By.XPATH, xpath)<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e8c\u3001Relative Locator\uff1a\u9769\u547d\u6027\u7684\u5b9a\u4f4d\u65b9\u5f0f<\/h2>\n<p>\u8fd9\u662fSelenium 4\u6700\u8ba9\u4eba\u5174\u594b\u7684\u65b0\u7279\u6027\uff01<\/p>\n<h3>\u4f20\u7edf\u5b9a\u4f4d\u7684\u75db\u70b9<\/h3>\n<p>\u4ee5\u524d\u5b9a\u4f4d\u5143\u7d20\uff0c\u53ea\u80fd\u7528\uff1a<\/p>\n<ul>\n<li>ID\u3001Name\u3001ClassName<\/li>\n<li>XPath\u3001CSS Selector<\/li>\n<li>LinkText\u3001PartialLinkText<\/li>\n<\/ul>\n<p>\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<\/p>\n<h3>Relative Locator\u767b\u573a<\/h3>\n<p>Relative Locator\u5141\u8bb8\u6211\u4eec\u6839\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u7a7a\u95f4\u5173\u7cfb\u6765\u5b9a\u4f4d\uff1a<\/p>\n<ul>\n<li><code>above()<\/code> &#8211; \u5728\u67d0\u5143\u7d20\u4e0a\u65b9<\/li>\n<li><code>below()<\/code> &#8211; \u5728\u67d0\u5143\u7d20\u4e0b\u65b9<\/li>\n<li><code>toLeftOf()<\/code> &#8211; \u5728\u67d0\u5143\u7d20\u5de6\u4fa7<\/li>\n<li><code>toRightOf()<\/code> &#8211; \u5728\u67d0\u5143\u7d20\u53f3\u4fa7<\/li>\n<li><code>near()<\/code> &#8211; \u5728\u67d0\u5143\u7d20\u9644\u8fd1<\/li>\n<\/ul>\n<h3>\u5b9e\u6218\u793a\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u767b\u5f55\u8868\u5355\uff1a<\/p>\n<p><code>`<\/code>html<\/p>\n<p>    <img decoding=\"async\" src=\"logo.png\" alt=\"Logo\"><\/p>\n<p>    <button type=\"submit\">\u767b\u5f55<\/button><\/p>\n<p><code>`<\/code><\/p>\n<p>\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<\/p>\n<p><strong>\u7528Relative Locator\uff1a<\/strong><\/p>\n<p><code>`<\/code>python<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.relative_locator import locate_with<\/p>\n<p>driver = webdriver.Chrome()<br \/>\ndriver.get(&#8220;https:\/\/example.com\/login&#8221;)<\/p>\n<h1>\u5148\u5b9a\u4f4dLogo<\/h1>\n<p>logo = driver.find_element(By.TAG_NAME, &#8220;img&#8221;)<\/p>\n<h1>\u5b9a\u4f4d\u7528\u6237\u540d\u8f93\u5165\u6846\uff08\u5728Logo\u4e0b\u65b9\uff09<\/h1>\n<p>username = driver.find_element(locate_with(By.TAG_NAME, &#8220;input&#8221;).below(logo))<\/p>\n<h1>\u5b9a\u4f4d\u5bc6\u7801\u8f93\u5165\u6846\uff08\u5728\u7528\u6237\u540d\u4e0b\u65b9\uff09<\/h1>\n<p>password = driver.find_element(locate_with(By.TAG_NAME, &#8220;input&#8221;).below(username))<\/p>\n<h1>\u5b9a\u4f4d\u767b\u5f55\u6309\u94ae\uff08\u5728\u5bc6\u7801\u4e0b\u65b9\uff09<\/h1>\n<p>login_btn = driver.find_element(locate_with(By.TAG_NAME, &#8220;button&#8221;).below(password))<br \/>\n<code>`<\/code><\/p>\n<h3>\u7ec4\u5408\u4f7f\u7528<\/h3>\n<p>Relative Locator\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\uff1a<\/p>\n<p><code>`<\/code>python<br \/>\nfrom selenium.webdriver.support.relative_locator import locate_with<\/p>\n<h1>\u5b9a\u4f4d\u5728Logo\u4e0b\u65b9\u3001\u4e14\u5728\u53f3\u4fa7\u680f\u5185\u7684\u5143\u7d20<\/h1>\n<p>element = driver.find_element(<br \/>\n    locate_with(By.TAG_NAME, &#8220;input&#8221;)<br \/>\n    .below(logo)<br \/>\n    .to_right_of(sidebar)<br \/>\n)<br \/>\n<code>`<\/code><\/p>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<p>Relative Locator\u7279\u522b\u9002\u5408\u8fd9\u4e9b\u573a\u666f\uff1a<\/p>\n<p>1. <strong>\u8868\u5355\u5e03\u5c40\u56fa\u5b9a\u4f46\u5143\u7d20\u5c5e\u6027\u4e0d\u7a33\u5b9a<\/strong><br \/>\n   &#8211; \u8f93\u5165\u6846\u6ca1\u6709\u56fa\u5b9aID<br \/>\n   &#8211; \u6839\u636eLabel\u6587\u672c\u5b9a\u4f4d\u65c1\u8fb9\u8f93\u5165\u6846<\/p>\n<p>2. <strong>\u54cd\u5e94\u5f0f\u5e03\u5c40\u6d4b\u8bd5<\/strong><br \/>\n   &#8211; \u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u4e0b\u5143\u7d20\u4f4d\u7f6e\u53d8\u5316<br \/>\n   &#8211; \u76f8\u5bf9\u4f4d\u7f6e\u5173\u7cfb\u4fdd\u6301\u4e0d\u53d8<\/p>\n<p>3. <strong>\u52a8\u6001\u5185\u5bb9\u9875\u9762<\/strong><br \/>\n   &#8211; \u5217\u8868\u9879\u6ca1\u6709\u56fa\u5b9a\u5c5e\u6027<br \/>\n   &#8211; \u4f46\u76f8\u5bf9\u4f4d\u7f6e\u5173\u7cfb\u7a33\u5b9a<\/p>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<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<p>&#8212;<\/p>\n<h2>\u4e09\u3001WebDriverManager\u81ea\u52a8\u7ba1\u7406Driver<\/h2>\n<p>Selenium 4\u4e0d\u518d\u5185\u7f6eDriver\u7ba1\u7406\uff0c\u63a8\u8350\u4f7f\u7528WebDriverManager\u3002<\/p>\n<h3>\u4ee5\u524d\u7684\u75db\u70b9<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>\u4ee5\u524d\u9700\u8981\u624b\u52a8\u4e0b\u8f7dchromedriver\uff0c\u653e\u5230\u6307\u5b9a\u8def\u5f84<\/h1>\n<p>driver = webdriver.Chrome(&#8220;\/path\/to\/chromedriver&#8221;)<\/p>\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<p><code>`<\/code><\/p>\n<h3>\u73b0\u5728\u7684\u89e3\u51b3\u65b9\u6848<\/h3>\n<p><code>`<\/code>python<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.chrome.service import Service<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<h1>\u81ea\u52a8\u4e0b\u8f7d\u548c\u7ba1\u7406Driver<\/h1>\n<p>driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))<br \/>\n<code>`<\/code><\/p>\n<p>WebDriverManager\u4f1a\uff1a<\/p>\n<ul>\n<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<p>&#8212;<\/p>\n<h2>\u56db\u3001Chrome DevTools\u652f\u6301<\/h2>\n<p>Selenium 4\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Chrome DevTools\u534f\u8bae\uff0c\u5b9e\u73b0\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<h3>\u6a21\u62df\u7f51\u7edc\u6761\u4ef6<\/h3>\n<p><code>`<\/code>python<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilities<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<h1>\u542f\u7528\u7f51\u7edc\u6a21\u62df<\/h1>\n<p>driver.execute_cdp_cmd(&#8220;Network.enable&#8221;, {})<\/p>\n<h1>\u6a21\u62df3G\u7f51\u7edc<\/h1>\n<p>driver.execute_cdp_cmd(&#8220;Network.emulateNetworkConditions&#8221;, {<br \/>\n    &#8220;offline&#8221;: False,<br \/>\n    &#8220;latency&#8221;: 100,  # \u5ef6\u8fdf100ms<br \/>\n    &#8220;downloadThroughput&#8221;: 500 * 1024,  # \u4e0b\u8f7d\u901f\u5ea6 500KB\/s<br \/>\n    &#8220;uploadThroughput&#8221;: 500 * 1024,    # \u4e0a\u4f20\u901f\u5ea6 500KB\/s<br \/>\n})<\/p>\n<p>driver.get(&#8220;https:\/\/example.com&#8221;)<br \/>\n<code>`<\/code><\/p>\n<h3>\u6a21\u62df\u5730\u7406\u4f4d\u7f6e<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>\u6a21\u62dfGPS\u4f4d\u7f6e<\/h1>\n<p>driver.execute_cdp_cmd(&#8220;Emulation.setGeolocationOverride&#8221;, {<br \/>\n    &#8220;latitude&#8221;: 39.9042,  # \u5317\u4eac<br \/>\n    &#8220;longitude&#8221;: 116.4074,<br \/>\n    &#8220;accuracy&#8221;: 100<br \/>\n})<\/p>\n<p>driver.get(&#8220;https:\/\/example.com\/map&#8221;)<br \/>\n<code>`<\/code><\/p>\n<h3>\u622a\u53d6\u7279\u5b9a\u5143\u7d20<\/h3>\n<p><code>`<\/code>python<\/p>\n<h1>\u622a\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u622a\u56fe<\/h1>\n<p>element = driver.find_element(By.ID, &#8220;chart&#8221;)<br \/>\nscreenshot = element.screenshot_as_png<\/p>\n<p>with open(&#8220;chart.png&#8221;, &#8220;wb&#8221;) as f:<br \/>\n    f.write(screenshot)<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e94\u3001\u7a97\u53e3\u7ba1\u7406\u589e\u5f3a<\/h2>\n<p>Selenium 4\u65b0\u589e\u4e86\u7a97\u53e3\u7ba1\u7406API\uff1a<\/p>\n<p><code>`<\/code>python<\/p>\n<h1>\u521b\u5efa\u65b0\u6807\u7b7e\u9875<\/h1>\n<p>driver.switch_to.new_window(&#8220;tab&#8221;)<\/p>\n<h1>\u521b\u5efa\u65b0\u7a97\u53e3<\/h1>\n<p>driver.switch_to.new_window(&#8220;window&#8221;)<\/p>\n<h1>\u83b7\u53d6\u7a97\u53e3\u4fe1\u606f<\/h1>\n<p>window_rect = driver.get_window_rect()<br \/>\nprint(window_rect)  # {&#8216;x&#8217;: 10, &#8216;y&#8217;: 10, &#8216;width&#8217;: 1200, &#8216;height&#8217;: 800}<\/p>\n<h1>\u8bbe\u7f6e\u7a97\u53e3\u4f4d\u7f6e\u548c\u5927\u5c0f<\/h1>\n<p>driver.set_window_rect(x=100, y=100, width=1024, height=768)<\/p>\n<h1>\u6700\u5c0f\u5316\u7a97\u53e3<\/h1>\n<p>driver.minimize_window()<\/p>\n<h1>\u5168\u5c4f<\/h1>\n<p>driver.fullscreen_window()<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u516d\u3001Grid 4\u67b6\u6784\u91cd\u6784<\/h2>\n<p>Selenium Grid 4\u5b8c\u5168\u91cd\u6784\u4e86\u67b6\u6784\uff1a<\/p>\n<h3>\u65b0\u67b6\u6784\u7ec4\u4ef6<\/h3>\n<ul>\n<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<p><code>`<\/code>bash<br \/>\njava -jar selenium-server-4.x.jar standalone<br \/>\n<code>`<\/code><\/p>\n<h3>\u542f\u52a8Hub-Node\u6a21\u5f0f<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u542f\u52a8Hub<\/h1>\n<p>java -jar selenium-server-4.x.jar hub<\/p>\n<h1>\u542f\u52a8Node\uff08\u8fde\u63a5\u5230Hub\uff09<\/h1>\n<p>java -jar selenium-server-4.x.jar node &#8211;hub http:\/\/hub-ip:4444<br \/>\n<code>`<\/code><\/p>\n<h3>Docker\u90e8\u7f72<\/h3>\n<p><code>`<\/code>bash<\/p>\n<h1>\u542f\u52a8Standalone<\/h1>\n<p>docker run -d -p 4444:4444 selenium\/standalone-chrome<\/p>\n<h1>\u542f\u52a8Hub<\/h1>\n<p>docker run -d -p 4444:4444 selenium\/hub<\/p>\n<h1>\u542f\u52a8Node<\/h1>\n<p>docker run -d &#8211;net host selenium\/node-chrome<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u4e03\u3001\u5b9e\u6218\uff1aSelenium 4\u81ea\u52a8\u5316\u6846\u67b6\u6a21\u677f<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8eSelenium 4 + Pytest\u7684\u81ea\u52a8\u5316\u6846\u67b6\u793a\u4f8b\uff1a<\/p>\n<p><code>`<\/code>python<\/p>\n<h1>conftest.py<\/h1>\n<p>import pytest<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.chrome.service import Service<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>@pytest.fixture(scope=&#8221;function&#8221;)<br \/>\ndef driver():<br \/>\n    &#8220;&#8221;&#8221;\u521d\u59cb\u5316WebDriver&#8221;&#8221;&#8221;<br \/>\n    options = webdriver.ChromeOptions()<br \/>\n    options.add_argument(&#8220;&#8211;headless&#8221;)  # \u65e0\u5934\u6a21\u5f0f<br \/>\n    options.add_argument(&#8220;&#8211;no-sandbox&#8221;)<br \/>\n    options.add_argument(&#8220;&#8211;disable-dev-shm-usage&#8221;)<\/p>\n<p>    driver = webdriver.Chrome(<br \/>\n        service=Service(ChromeDriverManager().install()),<br \/>\n        options=options<br \/>\n    )<br \/>\n    driver.maximize_window()<\/p>\n<p>    yield driver<\/p>\n<p>    driver.quit()<\/p>\n<h1>test_login.py<\/h1>\n<p>from selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.relative_locator import locate_with<\/p>\n<p>def test_login(driver):<br \/>\n    &#8220;&#8221;&#8221;\u6d4b\u8bd5\u767b\u5f55\u529f\u80fd&#8221;&#8221;&#8221;<br \/>\n    driver.get(&#8220;https:\/\/example.com\/login&#8221;)<\/p>\n<p>    # \u4f7f\u7528Relative Locator\u5b9a\u4f4d<br \/>\n    logo = driver.find_element(By.TAG_NAME, &#8220;img&#8221;)<br \/>\n    username = driver.find_element(locate_with(By.TAG_NAME, &#8220;input&#8221;).below(logo))<br \/>\n    password = driver.find_element(locate_with(By.TAG_NAME, &#8220;input&#8221;).below(username))<\/p>\n<p>    username.send_keys(&#8220;testuser&#8221;)<br \/>\n    password.send_keys(&#8220;password123&#8221;)<\/p>\n<p>    login_btn = driver.find_element(By.CSS_SELECTOR, &#8220;button[type=&#8217;submit&#8217;]&#8221;)<br \/>\n    login_btn.click()<\/p>\n<p>    # \u9a8c\u8bc1\u767b\u5f55\u6210\u529f<br \/>\n    assert &#8220;dashboard&#8221; in driver.current_url<br \/>\n<code>`<\/code><\/p>\n<p>&#8212;<\/p>\n<h2>\u516b\u3001\u5347\u7ea7\u5efa\u8bae<\/h2>\n<p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u8fd8\u5728\u7528Selenium 3\uff0c\u5347\u7ea7\u52304\u7684\u5efa\u8bae\uff1a<\/p>\n<p>1. <strong>\u5148\u5347\u7ea7\u4f9d\u8d56<\/strong><br \/>\n   <code>`<\/code>bash<br \/>\n   pip install selenium==4.x.x<br \/>\n   pip install webdriver-manager<br \/>\n   <code>`<\/code><\/p>\n<p>2. <strong>\u66f4\u65b0\u5b9a\u4f4d\u5668\u5199\u6cd5<\/strong><br \/>\n   &#8211; \u5168\u5c40\u641c\u7d22 <code>find_element_by_<\/code><br \/>\n   &#8211; \u66ff\u6362\u4e3a <code>find_element(By.*, ...)<\/code><\/p>\n<p>3. <strong>\u6d4b\u8bd5Relative Locator<\/strong><br \/>\n   &#8211; \u627e\u51e0\u4e2a\u590d\u6742\u5b9a\u4f4d\u573a\u666f<br \/>\n   &#8211; \u5c1d\u8bd5\u7528Relative Locator\u7b80\u5316<\/p>\n<p>4. <strong>\u8bc4\u4f30Grid\u5347\u7ea7<\/strong><br \/>\n   &#8211; \u5982\u679c\u7528\u4e86Grid\uff0c\u8bc4\u4f30\u662f\u5426\u5347\u7ea7\u52304.x<br \/>\n   &#8211; Docker\u90e8\u7f72\u66f4\u7b80\u5355<\/p>\n<p>&#8212;<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Selenium 4\u662f\u4e00\u6b21\u91cd\u8981\u7684\u7248\u672c\u5347\u7ea7\uff0c\u4e3b\u8981\u6536\u76ca\uff1a<\/p>\n<p>| \u7279\u6027 | \u6536\u76ca |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|<br \/>\n| W3C\u534f\u8bae | \u8de8\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u66f4\u597d |<br \/>\n| Relative Locator | \u5b9a\u4f4d\u5143\u7d20\u66f4\u7075\u6d3b |<br \/>\n| DevTools\u652f\u6301 | \u7f51\u7edc\u6a21\u62df\u3001\u5730\u7406\u4f4d\u7f6e\u7b49\u9ad8\u7ea7\u529f\u80fd |<br \/>\n| Grid\u91cd\u6784 | \u5206\u5e03\u5f0f\u6d4b\u8bd5\u66f4\u6613\u90e8\u7f72 |<br \/>\n| API\u73b0\u4ee3\u5316 | \u4ee3\u7801\u66f4\u89c4\u8303\u3001\u53ef\u7ef4\u62a4 |<\/p>\n<p>\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<\/p>\n<p>&#8212;<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<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<p>&#8212;<\/p>\n<p><strong>\u4e0b\u671f\u9884\u544a\uff1aPlaywright vs Cypress\u5bf9\u51b3 &#8211; 2026\u5e74\u6700\u503c\u5f97\u5b66\u4e60\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6<\/strong><\/p>\n<p>&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<\/p>\n","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\uff0cSelen&#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}]}}