{"id":52142,"date":"2025-08-11T10:37:16","date_gmt":"2025-08-11T02:37:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/52142.html"},"modified":"2025-08-11T10:37:16","modified_gmt":"2025-08-11T02:37:16","slug":"%e5%85%a8%e9%9d%a2%e4%ba%86%e8%a7%a3selenium","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/52142.html","title":{"rendered":"\u5168\u9762\u4e86\u89e3selenium"},"content":{"rendered":"<h3>\u5f15\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u9009\u62e9 Selenium&#xff1f;<\/h3>\n<p>\u5728\u5f53\u4eca\u7684\u4e92\u8054\u7f51\u65f6\u4ee3&#xff0c;Web \u5e94\u7528\u5df2\u7ecf\u6210\u4e3a\u4f01\u4e1a\u670d\u52a1\u7528\u6237\u7684\u4e3b\u8981\u8f7d\u4f53\u3002\u65e0\u8bba\u662f\u7535\u5546\u5e73\u53f0\u3001\u793e\u4ea4\u5a92\u4f53\u8fd8\u662f\u4f01\u4e1a\u5185\u90e8\u7cfb\u7edf&#xff0c;\u90fd\u9700\u8981\u4fdd\u8bc1\u5176\u529f\u80fd\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u8fd9\u5c31\u50ac\u751f\u4e86\u5bf9 Web \u81ea\u52a8\u5316\u5de5\u5177\u7684\u5f3a\u70c8\u9700\u6c42 \u2014\u2014 \u5f00\u53d1\u8005\u9700\u8981\u9ad8\u6548\u5730\u6d4b\u8bd5 Web \u5e94\u7528&#xff0c;\u6570\u636e\u5206\u6790\u5e08\u9700\u8981\u6279\u91cf\u83b7\u53d6\u7f51\u9875\u4fe1\u606f&#xff0c;\u800c\u666e\u901a\u7528\u6237\u4e5f\u5e0c\u671b\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\u7b80\u5316\u91cd\u590d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p>Selenium\u4f5c\u4e3a\u76ee\u524d\u6700\u6d41\u884c\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177\u4e4b\u4e00&#xff0c;\u81ea 2004 \u5e74\u8bde\u751f\u4ee5\u6765&#xff0c;\u5df2\u7ecf\u6210\u4e3a\u884c\u4e1a\u6807\u51c6\u3002\u5b83\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00&#xff08;Python\u3001Java\u3001C# \u7b49&#xff09;\u3001\u591a\u79cd\u6d4f\u89c8\u5668&#xff08;Chrome\u3001Firefox\u3001Edge \u7b49&#xff09;\u548c\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf&#xff0c;\u80fd\u591f\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u64cd\u4f5c\u884c\u4e3a&#xff0c;\u5b9e\u73b0\u4ece\u7b80\u5355\u7684\u8868\u5355\u63d0\u4ea4\u5230\u590d\u6742\u7684\u5355\u9875\u5e94\u7528\u4ea4\u4e92\u7684\u5168\u6d41\u7a0b\u81ea\u52a8\u5316\u3002<\/p>\n<\/p>\n<p>\u672c\u6587\u5c06\u4ece Selenium \u7684\u6838\u5fc3\u6982\u5ff5\u51fa\u53d1&#xff0c;\u901a\u8fc7\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5168\u9762\u8986\u76d6\u4ee5\u4e0b\u5185\u5bb9&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>Selenium \u7684\u67b6\u6784\u4e0e\u6838\u5fc3\u7ec4\u4ef6<\/li>\n<li>\u73af\u5883\u642d\u5efa\u4e0e\u57fa\u7840\u914d\u7f6e<\/li>\n<li>\u5143\u7d20\u5b9a\u4f4d\u4e0e\u4ea4\u4e92\u64cd\u4f5c<\/li>\n<li>\u9ad8\u7ea7\u529f\u80fd&#xff08;\u7b49\u5f85\u673a\u5236\u3001\u7a97\u53e3\u5207\u6362\u3001\u6587\u4ef6\u5904\u7406\u7b49&#xff09;<\/li>\n<li>\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u96c6\u6210<\/li>\n<li>\u7f51\u9875\u6570\u636e\u722c\u53d6\u5b9e\u6218<\/li>\n<li>\u6027\u80fd\u4f18\u5316\u4e0e\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ul>\n<p>\u65e0\u8bba\u4f60\u662f\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u3001\u6570\u636e\u5206\u6790\u5e08\u8fd8\u662f\u5f00\u53d1\u4eba\u5458&#xff0c;\u638c\u63e1 Selenium \u90fd\u5c06\u663e\u8457\u63d0\u5347\u4f60\u7684\u5de5\u4f5c\u6548\u7387&#xff0c;\u8ba9\u4f60\u4ece\u7e41\u7410\u7684\u91cd\u590d\u52b3\u52a8\u4e2d\u89e3\u653e\u51fa\u6765\u3002<\/p>\n<h3>\u4e00\u3001Selenium \u6838\u5fc3\u6982\u5ff5\u4e0e\u67b6\u6784<\/h3>\n<h4>1.1 Selenium \u662f\u4ec0\u4e48&#xff1f;<\/h4>\n<p>Selenium \u662f\u4e00\u4e2a\u7528\u4e8e Web \u5e94\u7528\u6d4b\u8bd5\u7684\u5f00\u6e90\u5de5\u5177\u96c6&#xff0c;\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u63a7\u5236\u6d4f\u89c8\u5668\u6267\u884c\u5404\u79cd\u64cd\u4f5c&#xff0c;\u5982\u70b9\u51fb\u6309\u94ae\u3001\u586b\u5199\u8868\u5355\u3001\u9875\u9762\u5bfc\u822a\u7b49\u3002\u4e0e\u5176\u4ed6\u81ea\u52a8\u5316\u5de5\u5177\u76f8\u6bd4&#xff0c;Selenium \u7684\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u771f\u5b9e\u6d4f\u89c8\u5668\u73af\u5883&#xff1a;\u76f4\u63a5\u9a71\u52a8\u771f\u5b9e\u6d4f\u89c8\u5668&#xff0c;\u6a21\u62df\u7528\u6237\u771f\u5b9e\u64cd\u4f5c&#xff0c;\u907f\u514d\u4e86\u4e0e JavaScript \u6e32\u67d3\u76f8\u5173\u7684\u517c\u5bb9\u6027\u95ee\u9898<\/li>\n<li>\u8de8\u5e73\u53f0\u4e0e\u8de8\u6d4f\u89c8\u5668&#xff1a;\u652f\u6301 Windows\u3001macOS\u3001Linux&#xff0c;\u4ee5\u53ca Chrome\u3001Firefox\u3001Safari \u7b49\u4e3b\u6d41\u6d4f\u89c8\u5668<\/li>\n<li>\u591a\u8bed\u8a00\u652f\u6301&#xff1a;\u63d0\u4f9b Python\u3001Java\u3001C#\u3001Ruby \u7b49\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684 API<\/li>\n<li>\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301&#xff1a;\u4f5c\u4e3a\u6700\u6d41\u884c\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177&#xff0c;\u62e5\u6709\u4e30\u5bcc\u7684\u6587\u6863\u548c\u89e3\u51b3\u65b9\u6848<\/li>\n<\/ul>\n<h4>1.2 Selenium \u7684\u6838\u5fc3\u7ec4\u4ef6<\/h4>\n<p>Selenium \u751f\u6001\u7cfb\u7edf\u5305\u542b\u591a\u4e2a\u7ec4\u4ef6&#xff0c;\u5404\u81ea\u627f\u62c5\u4e0d\u540c\u7684\u529f\u80fd&#xff1a;<\/p>\n<\/p>\n<li>Selenium WebDriver&#xff1a;\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u63d0\u4f9b\u7f16\u7a0b\u63a5\u53e3\u7528\u4e8e\u63a7\u5236\u6d4f\u89c8\u5668&#xff0c;\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00&#xff0c;\u662f\u672c\u6587\u7684\u91cd\u70b9\u5185\u5bb9<\/li>\n<li>Selenium IDE&#xff1a;\u6d4f\u89c8\u5668\u63d2\u4ef6&#xff0c;\u7528\u4e8e\u5f55\u5236\u548c\u56de\u653e\u64cd\u4f5c&#xff0c;\u9002\u5408\u5feb\u901f\u521b\u5efa\u7b80\u5355\u7684\u81ea\u52a8\u5316\u811a\u672c<\/li>\n<li>Selenium Grid&#xff1a;\u7528\u4e8e\u5206\u5e03\u5f0f\u6d4b\u8bd5&#xff0c;\u5141\u8bb8\u5728\u4e0d\u540c\u7684\u673a\u5668\u548c\u6d4f\u89c8\u5668\u4e0a\u5e76\u884c\u6267\u884c\u6d4b\u8bd5\u7528\u4f8b&#xff0c;\u63d0\u9ad8\u6d4b\u8bd5\u6548\u7387<\/li>\n<h4>1.3 Selenium \u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<p>Selenium WebDriver \u91c7\u7528\u5ba2\u6237\u7aef &#8211; \u670d\u52a1\u5668\u67b6\u6784&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u5ba2\u6237\u7aef&#xff1a;\u5f00\u53d1\u8005\u7f16\u5199\u7684\u81ea\u52a8\u5316\u811a\u672c&#xff08;\u5982 Python \u4ee3\u7801&#xff09;<\/li>\n<li>\u6d4f\u89c8\u5668\u9a71\u52a8&#xff1a;\u7279\u5b9a\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\u7a0b\u5e8f&#xff08;\u5982 ChromeDriver&#xff09;&#xff0c;\u4f5c\u4e3a\u5ba2\u6237\u7aef\u4e0e\u6d4f\u89c8\u5668\u4e4b\u95f4\u7684\u6865\u6881<\/li>\n<li>\u6d4f\u89c8\u5668&#xff1a;\u5b9e\u9645\u6267\u884c\u64cd\u4f5c\u7684\u6d4f\u89c8\u5668&#xff08;\u5982 Chrome&#xff09;<\/li>\n<\/ul>\n<p>\u5176\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<\/p>\n<li>\u5ba2\u6237\u7aef\u811a\u672c\u901a\u8fc7 WebDriver API \u53d1\u9001\u6307\u4ee4&#xff08;\u5982 &#034;\u6253\u5f00\u7f51\u9875&#034;\u3001&#034;\u70b9\u51fb\u6309\u94ae&#034;&#xff09;<\/li>\n<li>\u6d4f\u89c8\u5668\u9a71\u52a8\u63a5\u6536\u6307\u4ee4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6d4f\u89c8\u5668\u80fd\u7406\u89e3\u7684\u683c\u5f0f<\/li>\n<li>\u6d4f\u89c8\u5668\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u9a71\u52a8<\/li>\n<li>\u9a71\u52a8\u5c06\u7ed3\u679c\u53cd\u9988\u7ed9\u5ba2\u6237\u7aef\u811a\u672c<\/li>\n<p>\u8fd9\u79cd\u67b6\u6784\u7684\u4f18\u52bf\u5728\u4e8e&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u8de8\u8bed\u8a00\u5f00\u53d1&#xff08;\u53ea\u9700\u5b9e\u73b0\u5bf9\u5e94\u7684 WebDriver API&#xff09;<\/li>\n<li>\u53ef\u4ee5\u8fdc\u7a0b\u63a7\u5236\u4e0d\u540c\u673a\u5668\u4e0a\u7684\u6d4f\u89c8\u5668<\/li>\n<li>\u4e0e\u6d4f\u89c8\u5668\u7684\u4ea4\u4e92\u66f4\u52a0\u7a33\u5b9a\u53ef\u9760<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u73af\u5883\u642d\u5efa\u4e0e\u57fa\u7840\u914d\u7f6e<\/h3>\n<h4>2.1 \u5b89\u88c5 Python<\/h4>\n<p>Selenium \u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00&#xff0c;\u5176\u4e2d Python \u56e0\u5176\u7b80\u6d01\u6613\u5b66\u7684\u7279\u70b9\u6210\u4e3a\u6700\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u4e4b\u4e00\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5 Python \u73af\u5883&#xff1a;<\/p>\n<\/p>\n<li>\u8bbf\u95ee Python \u5b98\u7f51&#xff08;Welcome to Python.org&#xff09;\u4e0b\u8f7d\u5bf9\u5e94\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305<\/li>\n<li>\u5b89\u88c5\u65f6\u52fe\u9009 &#034;Add Python to PATH&#034;&#xff0c;\u65b9\u4fbf\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528 Python<\/li>\n<li>\u9a8c\u8bc1\u5b89\u88c5&#xff1a;\u6253\u5f00\u547d\u4ee4\u884c&#xff0c;\u8f93\u5165python &#8211;version\u6216python3 &#8211;version&#xff0c;\u663e\u793a\u7248\u672c\u53f7\u5373\u5b89\u88c5\u6210\u529f<\/li>\n<h4>2.2 \u5b89\u88c5 Selenium \u5e93<\/h4>\n<p>\u4f7f\u7528 pip&#xff08;Python \u5305\u7ba1\u7406\u5de5\u5177&#xff09;\u5b89\u88c5 Selenium&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>pip install selenium<br \/>\n# \u6216\u4f7f\u7528pip3&#xff08;\u533a\u5206Python2\u548cPython3\u65f6&#xff09;<br \/>\npip3 install selenium<\/p>\n<p>\u9a8c\u8bc1\u5b89\u88c5&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>python -c &#034;import selenium; print(selenium.__version__)&#034;<\/p>\n<p>\u82e5\u8f93\u51fa Selenium \u7248\u672c\u53f7&#xff08;\u5982 4.15.0&#xff09;&#xff0c;\u5219\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<h4>2.3 \u5b89\u88c5\u6d4f\u89c8\u5668\u9a71\u52a8<\/h4>\n<p>Selenium \u9700\u8981\u7279\u5b9a\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u624d\u80fd\u63a7\u5236\u6d4f\u89c8\u5668&#xff0c;\u4e0d\u540c\u6d4f\u89c8\u5668\u9700\u8981\u5bf9\u5e94\u7248\u672c\u7684\u9a71\u52a8&#xff1a;<\/p>\n<\/p>\n<table>\n<tr>\u6d4f\u89c8\u5668\u9a71\u52a8\u4e0b\u8f7d\u5730\u5740<\/tr>\n<tbody>\n<tr>\n<td>Chrome<\/td>\n<td>https:\/\/sites.google.com\/chromium.org\/driver\/<\/td>\n<\/tr>\n<tr>\n<td>Firefox<\/td>\n<td>https:\/\/github.com\/mozilla\/geckodriver\/releases<\/td>\n<\/tr>\n<tr>\n<td>Edge<\/td>\n<td>Microsoft Edge WebDriver | Microsoft Edge Developer<\/td>\n<\/tr>\n<tr>\n<td>Safari<\/td>\n<td>\u5185\u7f6e&#xff08;\u9700\u5728 Safari \u8bbe\u7f6e\u4e2d\u542f\u7528 &#034;\u5f00\u53d1&#034; \u83dc\u5355\u4e0b\u7684 &#034;\u5141\u8bb8\u8fdc\u7a0b\u81ea\u52a8\u5316&#034;&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b89\u88c5\u6b65\u9aa4&#xff08;\u4ee5 Chrome \u4e3a\u4f8b&#xff09;&#xff1a;<\/p>\n<\/p>\n<li>\u67e5\u770b Chrome \u7248\u672c&#xff1a;\u6253\u5f00 Chrome&#xff0c;\u5728\u5730\u5740\u680f\u8f93\u5165chrome:\/\/version\/&#xff0c;\u8bb0\u5f55\u7248\u672c\u53f7&#xff08;\u5982 118.0.5993.88&#xff09;<\/li>\n<li>\u4e0b\u8f7d\u5bf9\u5e94\u7248\u672c\u7684 ChromeDriver&#xff1a;\u5728\u4e0b\u8f7d\u9875\u9762\u627e\u5230\u4e0e Chrome \u7248\u672c\u5339\u914d\u7684\u9a71\u52a8&#xff08;\u4e3b\u7248\u672c\u53f7\u9700\u4e00\u81f4&#xff0c;\u5982 118.x.x.x&#xff09;<\/li>\n<li>\u914d\u7f6e\u9a71\u52a8&#xff1a;\n<ul>\n<li>Windows&#xff1a;\u5c06\u4e0b\u8f7d\u7684 chromedriver.exe \u653e\u5165 Python \u5b89\u88c5\u76ee\u5f55&#xff08;\u6216\u6dfb\u52a0\u5230\u7cfb\u7edf PATH \u73af\u5883\u53d8\u91cf&#xff09;<\/li>\n<li>macOS\/Linux&#xff1a;\u5c06\u9a71\u52a8\u6587\u4ef6\u653e\u5165 \/usr\/local\/bin\/ \u76ee\u5f55&#xff0c;\u6216\u6dfb\u52a0\u5230 PATH<\/li>\n<\/ul>\n<\/li>\n<p>\u9a8c\u8bc1\u9a71\u52a8\u914d\u7f6e&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p># Windows<br \/>\nchromedriver &#8211;version<\/p>\n<p># macOS\/Linux<br \/>\nchromedriver &#8211;version<\/p>\n<p>\u82e5\u8f93\u51fa\u9a71\u52a8\u7248\u672c\u53f7&#xff0c;\u5219\u914d\u7f6e\u6210\u529f\u3002<\/p>\n<h4>2.4 \u7b2c\u4e00\u4e2a Selenium \u7a0b\u5e8f<\/h4>\n<p>\u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u9a8c\u8bc1\u73af\u5883\u662f\u5426\u914d\u7f6e\u6b63\u786e&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.chrome.service import Service<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p># \u521d\u59cb\u5316Chrome\u6d4f\u89c8\u5668<br \/>\n# \u4f7f\u7528webdriver_manager\u81ea\u52a8\u7ba1\u7406\u9a71\u52a8&#xff08;\u65e0\u9700\u624b\u52a8\u4e0b\u8f7d&#xff09;<br \/>\ndriver &#061; webdriver.Chrome(service&#061;Service(ChromeDriverManager().install()))<\/p>\n<p># \u6253\u5f00\u767e\u5ea6\u9996\u9875<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># \u6253\u5370\u9875\u9762\u6807\u9898<br \/>\nprint(&#034;\u9875\u9762\u6807\u9898&#xff1a;&#034;, driver.title)<\/p>\n<p># \u7b49\u5f853\u79d2&#xff08;\u8ba9\u6211\u4eec\u770b\u6e05\u6548\u679c&#xff09;<br \/>\nimport time<br \/>\ntime.sleep(3)<\/p>\n<p># \u5173\u95ed\u6d4f\u89c8\u5668<br \/>\ndriver.quit()<\/p>\n<p>\u4ee3\u7801\u8bf4\u660e&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>webdriver.Chrome()&#xff1a;\u521b\u5efa Chrome \u6d4f\u89c8\u5668\u5b9e\u4f8b<\/li>\n<li>driver.get(url)&#xff1a;\u6253\u5f00\u6307\u5b9a URL<\/li>\n<li>driver.title&#xff1a;\u83b7\u53d6\u5f53\u524d\u9875\u9762\u6807\u9898<\/li>\n<li>driver.quit()&#xff1a;\u5173\u95ed\u6d4f\u89c8\u5668\u5e76\u91ca\u653e\u8d44\u6e90<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u7a0b\u5e8f\u540e&#xff0c;\u4f1a\u81ea\u52a8\u6253\u5f00 Chrome \u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u767e\u5ea6\u9996\u9875&#xff0c;\u63a7\u5236\u53f0\u8f93\u51fa\u9875\u9762\u6807\u9898&#xff0c;3 \u79d2\u540e\u5173\u95ed\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p>\u63d0\u793a&#xff1a;webdriver_manager\u5e93\u53ef\u4ee5\u81ea\u52a8\u4e0b\u8f7d\u548c\u7ba1\u7406\u6d4f\u89c8\u5668\u9a71\u52a8&#xff0c;\u907f\u514d\u7248\u672c\u5339\u914d\u95ee\u9898&#xff0c;\u5b89\u88c5\u65b9\u6cd5&#xff1a;pip install webdriver-manager<\/p>\n<h3>\u4e09\u3001\u5143\u7d20\u5b9a\u4f4d&#xff1a;Selenium \u7684\u6838\u5fc3\u6280\u80fd<\/h3>\n<p>\u5728 Web \u81ea\u52a8\u5316\u4e2d&#xff0c;\u5143\u7d20\u5b9a\u4f4d\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u91cd\u8981\u7684\u64cd\u4f5c \u2014\u2014 \u6211\u4eec\u9700\u8981\u5148\u627e\u5230\u9875\u9762\u4e0a\u7684\u5143\u7d20&#xff08;\u5982\u6309\u94ae\u3001\u8f93\u5165\u6846&#xff09;&#xff0c;\u624d\u80fd\u8fdb\u884c\u540e\u7eed\u7684\u4ea4\u4e92\u3002Selenium \u63d0\u4f9b\u4e86\u591a\u79cd\u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5&#xff0c;\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002<\/p>\n<h4>3.1 \u5e38\u7528\u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5<\/h4>\n<p>Selenium \u63d0\u4f9b\u4e86 8 \u79cd\u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5&#xff0c;\u5b9a\u4e49\u5728By\u7c7b\u4e2d&#xff1a;<\/p>\n<\/p>\n<table>\n<tr>\u65b9\u6cd5\u8bf4\u660e\u9002\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>By.ID<\/td>\n<td>\u901a\u8fc7\u5143\u7d20\u7684 id \u5c5e\u6027\u5b9a\u4f4d<\/td>\n<td>\u5143\u7d20\u6709\u552f\u4e00 id \u65f6&#xff08;\u63a8\u8350&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>By.NAME<\/td>\n<td>\u901a\u8fc7\u5143\u7d20\u7684 name \u5c5e\u6027\u5b9a\u4f4d<\/td>\n<td>\u5143\u7d20\u6709 name \u5c5e\u6027\u4e14\u503c\u552f\u4e00\u65f6<\/td>\n<\/tr>\n<tr>\n<td>By.CLASS_NAME<\/td>\n<td>\u901a\u8fc7\u5143\u7d20\u7684 class \u5c5e\u6027\u5b9a\u4f4d<\/td>\n<td>\u5143\u7d20\u6709 class \u5c5e\u6027\u65f6&#xff08;\u6ce8\u610f&#xff1a;class \u53ef\u80fd\u5305\u542b\u591a\u4e2a\u503c&#xff0c;\u9700\u5b8c\u6574\u5339\u914d&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>By.TAG_NAME<\/td>\n<td>\u901a\u8fc7 HTML \u6807\u7b7e\u540d\u5b9a\u4f4d<\/td>\n<td>\u9875\u9762\u4e2d\u8be5\u6807\u7b7e\u552f\u4e00\u65f6<\/td>\n<\/tr>\n<tr>\n<td>By.LINK_TEXT<\/td>\n<td>\u901a\u8fc7\u94fe\u63a5\u7684\u5b8c\u6574\u6587\u672c\u5b9a\u4f4d<\/td>\n<td>\u5b9a\u4f4d&lt;a&gt;\u6807\u7b7e&#xff0c;\u6587\u672c\u552f\u4e00\u65f6<\/td>\n<\/tr>\n<tr>\n<td>By.PARTIAL_LINK_TEXT<\/td>\n<td>\u901a\u8fc7\u94fe\u63a5\u7684\u90e8\u5206\u6587\u672c\u5b9a\u4f4d<\/td>\n<td>\u5b9a\u4f4d&lt;a&gt;\u6807\u7b7e&#xff0c;\u53ea\u9700\u5339\u914d\u90e8\u5206\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td>By.XPATH<\/td>\n<td>\u901a\u8fc7 XPath \u8868\u8fbe\u5f0f\u5b9a\u4f4d<\/td>\n<td>\u590d\u6742\u573a\u666f&#xff0c;\u51e0\u4e4e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5<\/td>\n<\/tr>\n<tr>\n<td>By.CSS_SELECTOR<\/td>\n<td>\u901a\u8fc7 CSS \u9009\u62e9\u5668\u5b9a\u4f4d<\/td>\n<td>\u590d\u6742\u573a\u666f&#xff0c;\u8bed\u6cd5\u7b80\u6d01&#xff0c;\u6027\u80fd\u4f18\u4e8e XPath<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3.2 \u5355\u5143\u7d20\u5b9a\u4f4d\u4e0e\u591a\u5143\u7d20\u5b9a\u4f4d<\/h4>\n<p>Selenium \u63d0\u4f9b\u4e86\u4e24\u7c7b\u5b9a\u4f4d\u65b9\u6cd5&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u5355\u5143\u7d20\u5b9a\u4f4d&#xff1a;\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20&#xff0c;\u5982find_element()<\/li>\n<li>\u591a\u5143\u7d20\u5b9a\u4f4d&#xff1a;\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u5217\u8868&#xff0c;\u5982find_elements()<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p># \u521d\u59cb\u5316\u6d4f\u89c8\u5668<br \/>\ndriver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># 1. \u901a\u8fc7ID\u5b9a\u4f4d&#xff08;\u767e\u5ea6\u641c\u7d22\u6846&#xff09;<br \/>\nsearch_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<br \/>\nprint(&#034;ID\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, search_box.tag_name)  # \u8f93\u51fa&#xff1a;input<\/p>\n<p># 2. \u901a\u8fc7NAME\u5b9a\u4f4d<br \/>\nsearch_box &#061; driver.find_element(By.NAME, &#034;wd&#034;)<br \/>\nprint(&#034;NAME\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, search_box.get_attribute(&#034;id&#034;))  # \u8f93\u51fa&#xff1a;kw<\/p>\n<p># 3. \u901a\u8fc7CLASS_NAME\u5b9a\u4f4d&#xff08;\u767e\u5ea6\u4e00\u4e0b\u6309\u94ae&#xff09;<br \/>\nsearch_button &#061; driver.find_element(By.CLASS_NAME, &#034;s_btn&#034;)<br \/>\nprint(&#034;CLASS_NAME\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, search_button.get_attribute(&#034;value&#034;))  # \u8f93\u51fa&#xff1a;\u767e\u5ea6\u4e00\u4e0b<\/p>\n<p># 4. \u901a\u8fc7TAG_NAME\u5b9a\u4f4d&#xff08;\u5b9a\u4f4d\u6240\u6709a\u6807\u7b7e&#xff09;<br \/>\nlinks &#061; driver.find_elements(By.TAG_NAME, &#034;a&#034;)<br \/>\nprint(&#034;\u9875\u9762\u4e2da\u6807\u7b7e\u6570\u91cf&#xff1a;&#034;, len(links))  # \u8f93\u51fa&#xff1a;\u9875\u9762\u4e2d\u94fe\u63a5\u7684\u6570\u91cf<\/p>\n<p># 5. \u901a\u8fc7LINK_TEXT\u5b9a\u4f4d&#xff08;\u767e\u5ea6\u9996\u9875\u7684&#034;\u65b0\u95fb&#034;\u94fe\u63a5&#xff09;<br \/>\nnews_link &#061; driver.find_element(By.LINK_TEXT, &#034;\u65b0\u95fb&#034;)<br \/>\nprint(&#034;LINK_TEXT\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, news_link.get_attribute(&#034;href&#034;))  # \u8f93\u51fa&#xff1a;\u65b0\u95fb\u9875\u9762URL<\/p>\n<p># 6. \u901a\u8fc7PARTIAL_LINK_TEXT\u5b9a\u4f4d&#xff08;\u5305\u542b&#034;\u5730\u56fe&#034;\u7684\u94fe\u63a5&#xff09;<br \/>\nmap_link &#061; driver.find_element(By.PARTIAL_LINK_TEXT, &#034;\u5730\u56fe&#034;)<br \/>\nprint(&#034;PARTIAL_LINK_TEXT\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, map_link.text)  # \u8f93\u51fa&#xff1a;\u5730\u56fe<\/p>\n<p># 7. \u901a\u8fc7XPATH\u5b9a\u4f4d&#xff08;\u641c\u7d22\u6846&#xff09;<br \/>\nsearch_box &#061; driver.find_element(By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;kw&#034;]&#039;)<br \/>\nprint(&#034;XPATH\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, search_box.tag_name)  # \u8f93\u51fa&#xff1a;input<\/p>\n<p># 8. \u901a\u8fc7CSS_SELECTOR\u5b9a\u4f4d&#xff08;\u641c\u7d22\u6309\u94ae&#xff09;<br \/>\nsearch_button &#061; driver.find_element(By.CSS_SELECTOR, &#034;#su&#034;)<br \/>\nprint(&#034;CSS_SELECTOR\u5b9a\u4f4d\u7ed3\u679c&#xff1a;&#034;, search_button.get_attribute(&#034;value&#034;))  # \u8f93\u51fa&#xff1a;\u767e\u5ea6\u4e00\u4e0b<\/p>\n<p># \u5173\u95ed\u6d4f\u89c8\u5668<br \/>\ndriver.quit()<\/p>\n<h4>3.3 XPath \u8be6\u89e3<\/h4>\n<p>XPath&#xff08;XML Path Language&#xff09;\u662f\u4e00\u79cd\u5728 XML \u6587\u6863\u4e2d\u5b9a\u4f4d\u8282\u70b9\u7684\u8bed\u8a00&#xff0c;\u4e5f\u9002\u7528\u4e8e HTML \u6587\u6863\u3002XPath \u529f\u80fd\u5f3a\u5927&#xff0c;\u51e0\u4e4e\u53ef\u4ee5\u5b9a\u4f4d\u5230\u9875\u9762\u4e0a\u7684\u4efb\u4f55\u5143\u7d20\u3002<\/p>\n<h5>3.3.1 \u7edd\u5bf9\u8def\u5f84\u4e0e\u76f8\u5bf9\u8def\u5f84<\/h5>\n<ul>\n<li>\n<p>\u7edd\u5bf9\u8def\u5f84&#xff1a;\u4ece\u6839\u8282\u70b9\/html\u5f00\u59cb&#xff0c;\u9010\u7ea7\u5411\u4e0b&#xff0c;\u5982\/html\/body\/div[1]\/div[1]\/div[5]\/div\/div\/form\/span[1]\/input<\/p>\n<ul>\n<li>\u7f3a\u70b9&#xff1a;\u9875\u9762\u7ed3\u6784\u7a0d\u6709\u53d8\u5316\u5c31\u4f1a\u5931\u6548&#xff0c;\u4e0d\u63a8\u8350\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u76f8\u5bf9\u8def\u5f84&#xff1a;\u4ece\u4efb\u610f\u8282\u70b9\u5f00\u59cb&#xff0c;\u4ee5\/\/\u5f00\u5934&#xff0c;\u5982\/\/input[&#064;id&#061;&#034;kw&#034;]<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u66f4\u7075\u6d3b&#xff0c;\u5bf9\u9875\u9762\u7ed3\u6784\u53d8\u5316\u7684\u5bb9\u5fcd\u5ea6\u66f4\u9ad8&#xff0c;\u63a8\u8350\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>3.3.2 \u5e38\u7528 XPath \u8868\u8fbe\u5f0f<\/h5>\n<table>\n<tr>\u8868\u8fbe\u5f0f\u8bf4\u660e\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d<\/td>\n<td>\u9009\u62e9\u6240\u6709\u8be5\u6807\u7b7e\u7684\u5143\u7d20<\/td>\n<td>\/\/input\u00a0\u9009\u62e9\u6240\u6709 input \u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\/\/&#064;\u5c5e\u6027\u540d<\/td>\n<td>\u9009\u62e9\u6240\u6709\u5177\u6709\u8be5\u5c5e\u6027\u7684\u5143\u7d20<\/td>\n<td>\/\/&#064;id\u00a0\u9009\u62e9\u6240\u6709\u5177\u6709 id \u5c5e\u6027\u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d[&#064;\u5c5e\u6027\u540d&#061;&#034;\u503c&#034;]<\/td>\n<td>\u9009\u62e9\u5c5e\u6027\u7b49\u4e8e\u6307\u5b9a\u503c\u7684\u5143\u7d20<\/td>\n<td>\/\/input[&#064;name&#061;&#034;wd&#034;]<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d[contains(&#064;\u5c5e\u6027\u540d,&#034;\u90e8\u5206\u503c&#034;)]<\/td>\n<td>\u9009\u62e9\u5c5e\u6027\u5305\u542b\u6307\u5b9a\u90e8\u5206\u503c\u7684\u5143\u7d20<\/td>\n<td>\/\/a[contains(&#064;href,&#034;baidu.com&#034;)]<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d[text()&#061;&#034;\u6587\u672c&#034;]<\/td>\n<td>\u9009\u62e9\u6587\u672c\u5185\u5bb9\u7b49\u4e8e\u6307\u5b9a\u503c\u7684\u5143\u7d20<\/td>\n<td>\/\/a[text()&#061;&#034;\u65b0\u95fb&#034;]<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d[contains(text(),&#034;\u90e8\u5206\u6587\u672c&#034;)]<\/td>\n<td>\u9009\u62e9\u6587\u672c\u5305\u542b\u6307\u5b9a\u90e8\u5206\u7684\u5143\u7d20<\/td>\n<td>\/\/a[contains(text(),&#034;\u5730\u56fe&#034;)]<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u6807\u7b7e\u540d[position()&#061;n]<\/td>\n<td>\u9009\u62e9\u7b2c n \u4e2a\u8be5\u6807\u7b7e\u7684\u5143\u7d20<\/td>\n<td>\/\/div[position()&#061;1]\u00a0\u9009\u62e9\u7b2c\u4e00\u4e2a div<\/td>\n<\/tr>\n<tr>\n<td>\/\/\u7236\u6807\u7b7e\u540d\/\u5b50\u6807\u7b7e\u540d<\/td>\n<td>\u9009\u62e9\u7236\u6807\u7b7e\u4e0b\u7684\u5b50\u6807\u7b7e<\/td>\n<td>\/\/form\/input\u00a0\u9009\u62e9 form \u4e0b\u7684 input<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>XPath \u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># 1. \u5b9a\u4f4did\u4e3akw\u7684input\u5143\u7d20<br \/>\nelement &#061; driver.find_element(By.XPATH, &#039;\/\/input[&#064;id&#061;&#034;kw&#034;]&#039;)<\/p>\n<p># 2. \u5b9a\u4f4dname\u5305\u542b&#034;wd&#034;\u7684\u5143\u7d20<br \/>\nelement &#061; driver.find_element(By.XPATH, &#039;\/\/*[contains(&#064;name,&#034;wd&#034;)]&#039;)<\/p>\n<p># 3. \u5b9a\u4f4d\u6587\u672c\u4e3a&#034;\u767e\u5ea6\u4e00\u4e0b&#034;\u7684\u5143\u7d20<br \/>\nelement &#061; driver.find_element(By.XPATH, &#039;\/\/*[text()&#061;&#034;\u767e\u5ea6\u4e00\u4e0b&#034;]&#039;)<\/p>\n<p># 4. \u5b9a\u4f4dclass\u5305\u542b&#034;bg&#034;\u7684div\u4e0b\u7684input\u5143\u7d20<br \/>\nelement &#061; driver.find_element(By.XPATH, &#039;\/\/div[contains(&#064;class,&#034;bg&#034;)]\/input&#039;)<\/p>\n<p># 5. \u5b9a\u4f4d\u7b2c2\u4e2aa\u6807\u7b7e<br \/>\nelement &#061; driver.find_element(By.XPATH, &#039;\/\/a[position()&#061;2]&#039;)<\/p>\n<p>driver.quit()<\/p>\n<h4>3.4 CSS \u9009\u62e9\u5668\u8be6\u89e3<\/h4>\n<p>CSS \u9009\u62e9\u5668\u662f\u53e6\u4e00\u79cd\u5f3a\u5927\u7684\u5143\u7d20\u5b9a\u4f4d\u65b9\u5f0f&#xff0c;\u8bed\u6cd5\u7b80\u6d01&#xff0c;\u6027\u80fd\u901a\u5e38\u4f18\u4e8e XPath&#xff0c;\u662f\u5f88\u591a\u5f00\u53d1\u8005\u7684\u9996\u9009\u3002<\/p>\n<h5>3.4.1 \u5e38\u7528 CSS \u9009\u62e9\u5668\u8bed\u6cd5<\/h5>\n<table>\n<tr>\u9009\u62e9\u5668\u8bf4\u660e\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>#id<\/td>\n<td>\u901a\u8fc7 id \u5b9a\u4f4d<\/td>\n<td>#kw\u00a0\u5b9a\u4f4d id \u4e3a kw \u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>.class<\/td>\n<td>\u901a\u8fc7 class \u5b9a\u4f4d<\/td>\n<td>.s_btn\u00a0\u5b9a\u4f4d class \u4e3a s_btn \u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u7b7e\u540d<\/td>\n<td>\u901a\u8fc7\u6807\u7b7e\u540d\u5b9a\u4f4d<\/td>\n<td>input\u00a0\u5b9a\u4f4d\u6240\u6709 input \u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u7b7e\u540d#id<\/td>\n<td>\u6807\u7b7e &#043; id \u5b9a\u4f4d<\/td>\n<td>input#kw\u00a0\u5b9a\u4f4d id \u4e3a kw \u7684 input \u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u7b7e\u540d.class<\/td>\n<td>\u6807\u7b7e &#043; class \u5b9a\u4f4d<\/td>\n<td>input.s_ipt\u00a0\u5b9a\u4f4d class \u4e3a s_ipt \u7684 input \u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>[\u5c5e\u6027\u540d]<\/td>\n<td>\u5177\u6709\u6307\u5b9a\u5c5e\u6027\u7684\u5143\u7d20<\/td>\n<td>[name]\u00a0\u5b9a\u4f4d\u6240\u6709\u6709 name \u5c5e\u6027\u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>[\u5c5e\u6027\u540d&#061;&#034;\u503c&#034;]<\/td>\n<td>\u5c5e\u6027\u7b49\u4e8e\u6307\u5b9a\u503c\u7684\u5143\u7d20<\/td>\n<td>[name&#061;&#034;wd&#034;]\u00a0\u5b9a\u4f4d name \u4e3a wd \u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>[\u5c5e\u6027\u540d^&#061;&#034;\u524d\u7f00&#034;]<\/td>\n<td>\u5c5e\u6027\u4ee5\u6307\u5b9a\u524d\u7f00\u5f00\u5934\u7684\u5143\u7d20<\/td>\n<td>[href^&#061;&#034;http:\/\/&#034;]\u00a0\u5b9a\u4f4d href \u4ee5 http:\/\/ \u5f00\u5934\u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>[\u5c5e\u6027\u540d$&#061;&#034;\u540e\u7f00&#034;]<\/td>\n<td>\u5c5e\u6027\u4ee5\u6307\u5b9a\u540e\u7f00\u7ed3\u5c3e\u7684\u5143\u7d20<\/td>\n<td>[src$&#061;&#034;.png&#034;]\u00a0\u5b9a\u4f4d src \u4ee5.png \u7ed3\u5c3e\u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>[\u5c5e\u6027\u540d*&#061;&#034;\u5305\u542b&#034;]<\/td>\n<td>\u5c5e\u6027\u5305\u542b\u6307\u5b9a\u503c\u7684\u5143\u7d20<\/td>\n<td>[class*&#061;&#034;btn&#034;]\u00a0\u5b9a\u4f4d class \u5305\u542b btn \u7684\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td>\u7236\u9009\u62e9\u5668 \u5b50\u9009\u62e9\u5668<\/td>\n<td>\u540e\u4ee3\u5143\u7d20<\/td>\n<td>form input\u00a0\u5b9a\u4f4d form \u4e0b\u7684\u6240\u6709 input \u540e\u4ee3<\/td>\n<\/tr>\n<tr>\n<td>\u7236\u9009\u62e9\u5668&gt;\u5b50\u9009\u62e9\u5668<\/td>\n<td>\u76f4\u63a5\u5b50\u5143\u7d20<\/td>\n<td>div&gt;input\u00a0\u5b9a\u4f4d div \u7684\u76f4\u63a5\u5b50\u5143\u7d20 input<\/td>\n<\/tr>\n<tr>\n<td>\u9009\u62e9\u56681&#043;\u9009\u62e9\u56682<\/td>\n<td>\u76f8\u90bb\u5144\u5f1f\u5143\u7d20<\/td>\n<td>input&#043;button\u00a0\u5b9a\u4f4d input \u540e\u9762\u7684\u7b2c\u4e00\u4e2a button<\/td>\n<\/tr>\n<tr>\n<td>\u9009\u62e9\u5668:nth-child(n)<\/td>\n<td>\u7b2c n \u4e2a\u5b50\u5143\u7d20<\/td>\n<td>li:nth-child(2)\u00a0\u5b9a\u4f4d\u7b2c 2 \u4e2a li \u5143\u7d20<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>CSS \u9009\u62e9\u5668\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># 1. \u901a\u8fc7id\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;#kw&#039;)<\/p>\n<p># 2. \u901a\u8fc7class\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;.s_btn&#039;)<\/p>\n<p># 3. \u6807\u7b7e&#043;\u5c5e\u6027\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;input[name&#061;&#034;wd&#034;]&#039;)<\/p>\n<p># 4. \u5c5e\u6027\u5305\u542b\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;[class*&#061;&#034;bg&#034;]&#039;)<\/p>\n<p># 5. \u540e\u4ee3\u5143\u7d20\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;form #kw&#039;)<\/p>\n<p># 6. \u7b2cn\u4e2a\u5b50\u5143\u7d20\u5b9a\u4f4d<br \/>\nelement &#061; driver.find_element(By.CSS_SELECTOR, &#039;div:nth-child(1)&#039;)<\/p>\n<p>driver.quit()<\/p>\n<h4>3.5 \u5b9a\u4f4d\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\u4f18\u5148\u4f7f\u7528 ID&#xff1a;ID \u5728\u9875\u9762\u4e2d\u901a\u5e38\u662f\u552f\u4e00\u7684&#xff0c;\u5b9a\u4f4d\u901f\u5ea6\u5feb\u4e14\u7a33\u5b9a<\/li>\n<li>\u5176\u6b21\u8003\u8651 NAME \u6216 CLASS&#xff1a;\u5982\u679c\u6ca1\u6709 ID&#xff0c;\u53ef\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5<\/li>\n<li>\u590d\u6742\u573a\u666f\u7528 XPath \u6216 CSS&#xff1a;\u5bf9\u4e8e\u52a8\u6001\u751f\u6210\u7684\u5143\u7d20\u6216\u590d\u6742\u7ed3\u6784&#xff0c;\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5<\/li>\n<li>\u907f\u514d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84&#xff1a;\u7ef4\u62a4\u6027\u5dee&#xff0c;\u5bb9\u6613\u53d7\u9875\u9762\u7ed3\u6784\u53d8\u5316\u5f71\u54cd<\/li>\n<li>\u4f7f\u7528\u76f8\u5bf9\u5b9a\u4f4d&#xff1a;\u63d0\u9ad8\u811a\u672c\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u7ed3\u5408\u591a\u79cd\u5c5e\u6027&#xff1a;\u5f53\u5355\u4e00\u5c5e\u6027\u4e0d\u8db3\u4ee5\u5b9a\u4f4d\u65f6&#xff0c;\u53ef\u7ec4\u5408\u591a\u4e2a\u5c5e\u6027<\/li>\n<li>\u5229\u7528\u5f00\u53d1\u8005\u5de5\u5177&#xff1a;\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177&#xff08;F12&#xff09;\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u5143\u7d20\u5e76\u751f\u6210\u9009\u62e9\u5668<\/li>\n<p>\u5f00\u53d1\u8005\u5de5\u5177\u4f7f\u7528\u6280\u5de7&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u5728\u9875\u9762\u53f3\u952e\u70b9\u51fb\u5143\u7d20&#xff0c;\u9009\u62e9 &#034;\u68c0\u67e5&#034; \u6253\u5f00\u5f00\u53d1\u8005\u5de5\u5177<\/li>\n<li>\u5728 Elements \u9762\u677f\u4e2d\u53f3\u952e\u70b9\u51fb\u5143\u7d20&#xff0c;\u9009\u62e9 &#034;Copy&#034; -&gt; &#034;Copy XPath&#034; \u6216 &#034;Copy selector&#034; \u83b7\u53d6\u5b9a\u4f4d\u8868\u8fbe\u5f0f<\/li>\n<li>\u6ce8\u610f&#xff1a;\u81ea\u52a8\u751f\u6210\u7684\u8868\u8fbe\u5f0f\u53ef\u80fd\u4e0d\u591f\u7b80\u6d01&#xff0c;\u9700\u8981\u624b\u52a8\u4f18\u5316<\/li>\n<\/ul>\n<h3>\u56db\u3001\u5143\u7d20\u4ea4\u4e92&#xff1a;\u6a21\u62df\u7528\u6237\u64cd\u4f5c<\/h3>\n<p>\u5b9a\u4f4d\u5230\u5143\u7d20\u540e&#xff0c;\u4e0b\u4e00\u6b65\u5c31\u662f\u4e0e\u5143\u7d20\u8fdb\u884c\u4ea4\u4e92&#xff0c;\u6a21\u62df\u7528\u6237\u7684\u5404\u79cd\u64cd\u4f5c\u3002Selenium \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u70b9\u51fb\u3001\u8f93\u5165\u3001\u9009\u62e9\u7b49\u64cd\u4f5c\u3002<\/p>\n<h4>4.1 \u5e38\u7528\u5143\u7d20\u4ea4\u4e92\u65b9\u6cd5<\/h4>\n<table>\n<tr>\u65b9\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>click()<\/td>\n<td>\u70b9\u51fb\u5143\u7d20&#xff08;\u9002\u7528\u4e8e\u6309\u94ae\u3001\u94fe\u63a5\u7b49&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>send_keys(*value)<\/td>\n<td>\u5411\u8f93\u5165\u6846\u53d1\u9001\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td>clear()<\/td>\n<td>\u6e05\u7a7a\u8f93\u5165\u6846\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>submit()<\/td>\n<td>\u63d0\u4ea4\u8868\u5355&#xff08;\u9002\u7528\u4e8e form \u5143\u7d20&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>get_attribute(name)<\/td>\n<td>\u83b7\u53d6\u5143\u7d20\u7684\u5c5e\u6027\u503c<\/td>\n<\/tr>\n<tr>\n<td>text<\/td>\n<td>\u83b7\u53d6\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>is_displayed()<\/td>\n<td>\u5224\u65ad\u5143\u7d20\u662f\u5426\u53ef\u89c1<\/td>\n<\/tr>\n<tr>\n<td>is_enabled()<\/td>\n<td>\u5224\u65ad\u5143\u7d20\u662f\u5426\u53ef\u7528<\/td>\n<\/tr>\n<tr>\n<td>is_selected()<\/td>\n<td>\u5224\u65ad\u590d\u9009\u6846\u6216\u5355\u9009\u6309\u94ae\u662f\u5426\u88ab\u9009\u4e2d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># \u5b9a\u4f4d\u641c\u7d22\u6846<br \/>\nsearch_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<\/p>\n<p># 1. \u8f93\u5165\u6587\u672c<br \/>\nsearch_box.send_keys(&#034;Selenium\u81ea\u52a8\u5316&#034;)<br \/>\ntime.sleep(2)  # \u7b49\u5f852\u79d2&#xff0c;\u89c2\u5bdf\u6548\u679c<\/p>\n<p># 2. \u83b7\u53d6\u5c5e\u6027\u503c<br \/>\nprint(&#034;\u641c\u7d22\u6846\u7c7b\u578b&#xff1a;&#034;, search_box.get_attribute(&#034;type&#034;))  # \u8f93\u51fa&#xff1a;text<br \/>\nprint(&#034;\u641c\u7d22\u6846\u540d\u79f0&#xff1a;&#034;, search_box.get_attribute(&#034;name&#034;))  # \u8f93\u51fa&#xff1a;wd<\/p>\n<p># 3. \u6e05\u7a7a\u5185\u5bb9<br \/>\nsearch_box.clear()<br \/>\ntime.sleep(2)<\/p>\n<p># 4. \u91cd\u65b0\u8f93\u5165\u5e76\u63d0\u4ea4<br \/>\nsearch_box.send_keys(&#034;Python&#034;)<br \/>\nsearch_box.submit()  # \u63d0\u4ea4\u8868\u5355&#xff0c;\u76f8\u5f53\u4e8e\u70b9\u51fb\u641c\u7d22\u6309\u94ae<br \/>\ntime.sleep(3)<\/p>\n<p># 5. \u5b9a\u4f4d\u641c\u7d22\u7ed3\u679c\u6807\u9898\u5e76\u83b7\u53d6\u6587\u672c<br \/>\nresult_title &#061; driver.find_element(By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;)<br \/>\nprint(&#034;\u7b2c\u4e00\u4e2a\u641c\u7d22\u7ed3\u679c\u6807\u9898&#xff1a;&#034;, result_title.text)<\/p>\n<p># 6. \u5224\u65ad\u5143\u7d20\u72b6\u6001<br \/>\nprint(&#034;\u7ed3\u679c\u6807\u9898\u662f\u5426\u53ef\u89c1&#xff1a;&#034;, result_title.is_displayed())  # \u8f93\u51fa&#xff1a;True<\/p>\n<p># 7. \u70b9\u51fb\u641c\u7d22\u7ed3\u679c<br \/>\nresult_title.click()<br \/>\ntime.sleep(5)<\/p>\n<p># \u5173\u95ed\u6d4f\u89c8\u5668<br \/>\ndriver.quit()<\/p>\n<h4>4.2 \u5904\u7406\u8868\u5355\u5143\u7d20<\/h4>\n<p>\u8868\u5355\u662f Web \u5e94\u7528\u4e2d\u6700\u5e38\u89c1\u7684\u4ea4\u4e92\u5143\u7d20&#xff0c;\u5305\u62ec\u6587\u672c\u6846\u3001\u5bc6\u7801\u6846\u3001\u590d\u9009\u6846\u3001\u5355\u9009\u6309\u94ae\u3001\u4e0b\u62c9\u83dc\u5355\u7b49\u3002<\/p>\n<h5>4.2.1 \u5904\u7406\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae<\/h5>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p># \u8bbf\u95ee\u4e00\u4e2a\u5305\u542b\u8868\u5355\u7684\u6d4b\u8bd5\u9875\u9762<br \/>\ndriver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/html\/html_forms.asp&#034;)<\/p>\n<p># \u5207\u6362\u5230\u5305\u542b\u8868\u5355\u7684iframe&#xff08;\u793a\u4f8b\u9875\u9762\u7684\u8868\u5355\u5728iframe\u4e2d&#xff09;<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<\/p>\n<p># 1. \u5904\u7406\u590d\u9009\u6846<br \/>\ncheckbox1 &#061; driver.find_element(By.XPATH, &#039;\/\/input[&#064;value&#061;&#034;vehicle1&#034;]&#039;)<br \/>\ncheckbox2 &#061; driver.find_element(By.XPATH, &#039;\/\/input[&#064;value&#061;&#034;vehicle2&#034;]&#039;)<\/p>\n<p># \u52fe\u9009\u590d\u9009\u6846&#xff08;\u5982\u679c\u672a\u52fe\u9009&#xff09;<br \/>\nif not checkbox1.is_selected():<br \/>\n    checkbox1.click()<br \/>\ntime.sleep(2)<\/p>\n<p>if not checkbox2.is_selected():<br \/>\n    checkbox2.click()<br \/>\ntime.sleep(2)<\/p>\n<p># \u53d6\u6d88\u52fe\u9009\u7b2c\u4e00\u4e2a\u590d\u9009\u6846<br \/>\nif checkbox1.is_selected():<br \/>\n    checkbox1.click()<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u5904\u7406\u5355\u9009\u6309\u94ae<br \/>\nradio1 &#061; driver.find_element(By.XPATH, &#039;\/\/input[&#064;value&#061;&#034;male&#034;]&#039;)<br \/>\nradio2 &#061; driver.find_element(By.XPATH, &#039;\/\/input[&#064;value&#061;&#034;female&#034;]&#039;)<\/p>\n<p># \u9009\u62e9\u7b2c\u4e00\u4e2a\u5355\u9009\u6309\u94ae<br \/>\nif not radio1.is_selected():<br \/>\n    radio1.click()<br \/>\ntime.sleep(2)<\/p>\n<p># \u9009\u62e9\u7b2c\u4e8c\u4e2a\u5355\u9009\u6309\u94ae<br \/>\nif not radio2.is_selected():<br \/>\n    radio2.click()<br \/>\ntime.sleep(2)<\/p>\n<p># \u9000\u51faiframe<br \/>\ndriver.switch_to.default_content()<\/p>\n<p>driver.quit()<\/p>\n<h5>4.2.2 \u5904\u7406\u4e0b\u62c9\u83dc\u5355<\/h5>\n<p>\u4e0b\u62c9\u83dc\u5355&#xff08;&lt;select&gt;\u5143\u7d20&#xff09;\u662f\u8868\u5355\u4e2d\u7684\u7279\u6b8a\u5143\u7d20&#xff0c;Selenium \u63d0\u4f9b\u4e86Select\u7c7b\u4e13\u95e8\u5904\u7406&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import Select<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/tags\/tryit.asp?filename&#061;tryhtml_select&#034;)<\/p>\n<p># \u5207\u6362\u5230iframe<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<\/p>\n<p># \u5b9a\u4f4dselect\u5143\u7d20\u5e76\u521b\u5efaSelect\u5bf9\u8c61<br \/>\nselect_element &#061; driver.find_element(By.TAG_NAME, &#034;select&#034;)<br \/>\nselect &#061; Select(select_element)<\/p>\n<p># 1. \u901a\u8fc7\u7d22\u5f15\u9009\u62e9&#xff08;\u7d22\u5f15\u4ece0\u5f00\u59cb&#xff09;<br \/>\nselect.select_by_index(1)  # \u9009\u62e9\u7b2c\u4e8c\u4e2a\u9009\u9879<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u901a\u8fc7\u53ef\u89c1\u6587\u672c\u9009\u62e9<br \/>\nselect.select_by_visible_text(&#034;Saab&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># 3. \u901a\u8fc7value\u5c5e\u6027\u9009\u62e9<br \/>\nselect.select_by_value(&#034;opel&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># \u83b7\u53d6\u6240\u6709\u9009\u9879<br \/>\noptions &#061; select.options<br \/>\nprint(&#034;\u6240\u6709\u9009\u9879&#xff1a;&#034;)<br \/>\nfor option in options:<br \/>\n    print(option.text)<\/p>\n<p># \u83b7\u53d6\u5f53\u524d\u9009\u4e2d\u7684\u9009\u9879<br \/>\nselected_option &#061; select.first_selected_option<br \/>\nprint(&#034;\u5f53\u524d\u9009\u4e2d\u7684\u9009\u9879&#xff1a;&#034;, selected_option.text)<\/p>\n<p># \u9000\u51faiframe<br \/>\ndriver.switch_to.default_content()<\/p>\n<p>driver.quit()<\/p>\n<h4>4.3 \u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d<\/h4>\n<p>\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d\u662f Web \u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u529f\u80fd&#xff0c;Selenium \u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<h5>4.3.1 \u6587\u4ef6\u4e0a\u4f20<\/h5>\n<p>\u6587\u4ef6\u4e0a\u4f20\u901a\u5e38\u901a\u8fc7&lt;input type&#061;&#034;file&#034;&gt;\u5143\u7d20\u5b9e\u73b0&#xff0c;Selenium \u53ef\u4ee5\u76f4\u63a5\u5411\u8be5\u5143\u7d20\u53d1\u9001\u6587\u4ef6\u8def\u5f84&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<br \/>\nimport os<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/tags\/tryit.asp?filename&#061;tryhtml_input_type_file&#034;)<\/p>\n<p># \u5207\u6362\u5230iframe<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<\/p>\n<p># \u5b9a\u4f4d\u6587\u4ef6\u4e0a\u4f20\u5143\u7d20<br \/>\nfile_input &#061; driver.find_element(By.ID, &#034;myFile&#034;)<\/p>\n<p># \u83b7\u53d6\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55<br \/>\ncurrent_dir &#061; os.path.dirname(os.path.abspath(__file__))<br \/>\n# \u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u8def\u5f84&#xff08;\u66ff\u6362\u4e3a\u5b9e\u9645\u6587\u4ef6\u8def\u5f84&#xff09;<br \/>\nfile_path &#061; os.path.join(current_dir, &#034;test.txt&#034;)<\/p>\n<p># \u53d1\u9001\u6587\u4ef6\u8def\u5f84\u5230\u6587\u4ef6\u4e0a\u4f20\u5143\u7d20<br \/>\nfile_input.send_keys(file_path)<br \/>\ntime.sleep(3)<\/p>\n<p># \u9000\u51faiframe<br \/>\ndriver.switch_to.default_content()<\/p>\n<p>driver.quit()<\/p>\n<p>\u6ce8\u610f&#xff1a;\u786e\u4fddtest.txt\u6587\u4ef6\u5b58\u5728\u4e8e\u811a\u672c\u6240\u5728\u76ee\u5f55&#xff0c;\u6216\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<h5>4.3.2 \u6587\u4ef6\u4e0b\u8f7d<\/h5>\n<p>\u6587\u4ef6\u4e0b\u8f7d\u9700\u8981\u914d\u7f6e\u6d4f\u89c8\u5668\u7684\u4e0b\u8f7d\u8def\u5f84&#xff0c;\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u914d\u7f6e\u65b9\u5f0f\u7565\u6709\u4e0d\u540c&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.chrome.options import Options<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<br \/>\nimport os<\/p>\n<p># \u914d\u7f6eChrome\u6d4f\u89c8\u5668\u7684\u4e0b\u8f7d\u8bbe\u7f6e<br \/>\nchrome_options &#061; Options()<br \/>\n# \u8bbe\u7f6e\u4e0b\u8f7d\u8def\u5f84&#xff08;\u66ff\u6362\u4e3a\u5b9e\u9645\u8def\u5f84&#xff09;<br \/>\ndownload_dir &#061; os.path.join(os.path.dirname(os.path.abspath(__file__)), &#034;downloads&#034;)<br \/>\n# \u521b\u5efa\u4e0b\u8f7d\u76ee\u5f55&#xff08;\u5982\u679c\u4e0d\u5b58\u5728&#xff09;<br \/>\nos.makedirs(download_dir, exist_ok&#061;True)<\/p>\n<p># \u914d\u7f6e\u4e0b\u8f7d\u9009\u9879<br \/>\nprefs &#061; {<br \/>\n    &#034;download.default_directory&#034;: download_dir,  # \u4e0b\u8f7d\u8def\u5f84<br \/>\n    &#034;download.prompt_for_download&#034;: False,       # \u4e0d\u63d0\u793a\u4e0b\u8f7d<br \/>\n    &#034;download.directory_upgrade&#034;: True,<br \/>\n    &#034;safebrowsing.enabled&#034;: True                 # \u542f\u7528\u5b89\u5168\u6d4f\u89c8<br \/>\n}<br \/>\nchrome_options.add_experimental_option(&#034;prefs&#034;, prefs)<\/p>\n<p># \u521d\u59cb\u5316\u6d4f\u89c8\u5668<br \/>\ndriver &#061; webdriver.Chrome(<br \/>\n    ChromeDriverManager().install(),<br \/>\n    options&#061;chrome_options<br \/>\n)<\/p>\n<p># \u8bbf\u95ee\u4e00\u4e2a\u63d0\u4f9b\u4e0b\u8f7d\u7684\u9875\u9762&#xff08;\u793a\u4f8b&#xff09;<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/python\/python_intro.asp&#034;)<\/p>\n<p># \u5b9a\u4f4d\u5e76\u70b9\u51fb\u4e0b\u8f7d\u94fe\u63a5&#xff08;\u793a\u4f8b&#xff09;<br \/>\n# \u6ce8\u610f&#xff1a;\u5b9e\u9645\u9875\u9762\u7684\u4e0b\u8f7d\u94fe\u63a5\u53ef\u80fd\u4e0d\u540c&#xff0c;\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539<br \/>\ntry:<br \/>\n    download_link &#061; driver.find_element(By.LINK_TEXT, &#034;Download Python&#034;)<br \/>\n    download_link.click()<br \/>\n    print(&#034;\u5f00\u59cb\u4e0b\u8f7d&#8230;&#034;)<br \/>\n    time.sleep(10)  # \u7b49\u5f85\u4e0b\u8f7d\u5b8c\u6210&#xff08;\u6839\u636e\u6587\u4ef6\u5927\u5c0f\u8c03\u6574&#xff09;<br \/>\n    print(f&#034;\u6587\u4ef6\u5df2\u4e0b\u8f7d\u5230&#xff1a;{download_dir}&#034;)<br \/>\nexcept Exception as e:<br \/>\n    print(&#034;\u672a\u627e\u5230\u4e0b\u8f7d\u94fe\u63a5&#xff1a;&#034;, e)<\/p>\n<p>driver.quit()<\/p>\n<h3>\u4e94\u3001\u6d4f\u89c8\u5668\u63a7\u5236\u4e0e\u5bfc\u822a<\/h3>\n<p>\u9664\u4e86\u5143\u7d20\u4ea4\u4e92&#xff0c;Selenium \u8fd8\u53ef\u4ee5\u63a7\u5236\u6d4f\u89c8\u5668\u672c\u8eab&#xff0c;\u5982\u8c03\u6574\u7a97\u53e3\u5927\u5c0f\u3001\u524d\u8fdb\u540e\u9000\u3001\u5237\u65b0\u9875\u9762\u7b49\u3002<\/p>\n<h4>5.1 \u6d4f\u89c8\u5668\u7a97\u53e3\u63a7\u5236<\/h4>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># 1. \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<br \/>\ndriver.set_window_size(1024, 768)  # \u5bbd1024&#xff0c;\u9ad8768<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u6700\u5927\u5316\u7a97\u53e3<br \/>\ndriver.maximize_window()<br \/>\ntime.sleep(2)<\/p>\n<p># 3. \u6700\u5c0f\u5316\u7a97\u53e3<br \/>\ndriver.minimize_window()<br \/>\ntime.sleep(2)<\/p>\n<p># 4. \u5168\u5c4f\u663e\u793a<br \/>\ndriver.fullscreen_window()<br \/>\ntime.sleep(2)<\/p>\n<p># 5. \u83b7\u53d6\u7a97\u53e3\u5927\u5c0f<br \/>\nwindow_size &#061; driver.get_window_size()<br \/>\nprint(f&#034;\u7a97\u53e3\u5927\u5c0f&#xff1a;\u5bbd{window_size[&#039;width&#039;]}&#xff0c;\u9ad8{window_size[&#039;height&#039;]}&#034;)<\/p>\n<p># 6. \u8bbe\u7f6e\u7a97\u53e3\u4f4d\u7f6e&#xff08;\u5de6\u4e0a\u89d2\u5750\u6807&#xff09;<br \/>\ndriver.set_window_position(100, 100)<br \/>\ntime.sleep(2)<\/p>\n<p># 7. \u83b7\u53d6\u7a97\u53e3\u4f4d\u7f6e<br \/>\nwindow_position &#061; driver.get_window_position()<br \/>\nprint(f&#034;\u7a97\u53e3\u4f4d\u7f6e&#xff1a;x&#061;{window_position[&#039;x&#039;]}&#xff0c;y&#061;{window_position[&#039;y&#039;]}&#034;)<\/p>\n<p>driver.quit()<\/p>\n<h4>5.2 \u9875\u9762\u5bfc\u822a<\/h4>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<\/p>\n<p># 1. \u6253\u5f00\u7f51\u9875<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<br \/>\nprint(&#034;\u5f53\u524dURL&#xff1a;&#034;, driver.current_url)<br \/>\nprint(&#034;\u9875\u9762\u6807\u9898&#xff1a;&#034;, driver.title)<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u6253\u5f00\u53e6\u4e00\u4e2a\u7f51\u9875<br \/>\ndriver.get(&#034;https:\/\/www.bing.com&#034;)<br \/>\nprint(&#034;\u5f53\u524dURL&#xff1a;&#034;, driver.current_url)<br \/>\nprint(&#034;\u9875\u9762\u6807\u9898&#xff1a;&#034;, driver.title)<br \/>\ntime.sleep(2)<\/p>\n<p># 3. \u540e\u9000<br \/>\ndriver.back()<br \/>\nprint(&#034;\u540e\u9000\u540eURL&#xff1a;&#034;, driver.current_url)<br \/>\ntime.sleep(2)<\/p>\n<p># 4. \u524d\u8fdb<br \/>\ndriver.forward()<br \/>\nprint(&#034;\u524d\u8fdb\u540eURL&#xff1a;&#034;, driver.current_url)<br \/>\ntime.sleep(2)<\/p>\n<p># 5. \u5237\u65b0\u9875\u9762<br \/>\ndriver.refresh()<br \/>\ntime.sleep(2)<\/p>\n<p># 6. \u83b7\u53d6\u9875\u9762\u6e90\u7801<br \/>\npage_source &#061; driver.page_source<br \/>\nprint(&#034;\u9875\u9762\u6e90\u7801\u957f\u5ea6&#xff1a;&#034;, len(page_source))<\/p>\n<p>driver.quit()<\/p>\n<h4>5.3 \u591a\u7a97\u53e3\u4e0e iframe \u5207\u6362<\/h4>\n<p>\u73b0\u4ee3 Web \u5e94\u7528\u7ecf\u5e38\u4f7f\u7528\u591a\u7a97\u53e3\u6216 iframe&#xff08;\u5185\u5d4c\u6846\u67b6&#xff09;&#xff0c;Selenium \u9700\u8981\u5207\u6362\u5230\u76f8\u5e94\u7684\u4e0a\u4e0b\u6587\u624d\u80fd\u64cd\u4f5c\u5176\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<h5>5.3.1 \u591a\u7a97\u53e3\u5207\u6362<\/h5>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># \u83b7\u53d6\u5f53\u524d\u7a97\u53e3\u53e5\u67c4&#xff08;\u552f\u4e00\u6807\u8bc6&#xff09;<br \/>\noriginal_window &#061; driver.current_window_handle<br \/>\nprint(&#034;\u539f\u59cb\u7a97\u53e3\u53e5\u67c4&#xff1a;&#034;, original_window)<\/p>\n<p># \u6253\u5f00\u65b0\u7a97\u53e3&#xff08;\u901a\u8fc7JavaScript&#xff09;<br \/>\ndriver.execute_script(&#034;window.open(&#039;https:\/\/www.bing.com&#039;);&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># \u83b7\u53d6\u6240\u6709\u7a97\u53e3\u53e5\u67c4<br \/>\nall_windows &#061; driver.window_handles<br \/>\nprint(&#034;\u6240\u6709\u7a97\u53e3\u53e5\u67c4&#xff1a;&#034;, all_windows)<\/p>\n<p># \u5207\u6362\u5230\u65b0\u7a97\u53e3<br \/>\nfor window in all_windows:<br \/>\n    if window !&#061; original_window:<br \/>\n        driver.switch_to.window(window)<br \/>\n        break<\/p>\n<p>print(&#034;\u5207\u6362\u540e\u7a97\u53e3URL&#xff1a;&#034;, driver.current_url)<br \/>\ntime.sleep(2)<\/p>\n<p># \u5728\u65b0\u7a97\u53e3\u4e2d\u6267\u884c\u64cd\u4f5c<br \/>\nsearch_box &#061; driver.find_element(By.NAME, &#034;q&#034;)<br \/>\nsearch_box.send_keys(&#034;Selenium&#034;)<br \/>\nsearch_box.submit()<br \/>\ntime.sleep(3)<\/p>\n<p># \u5207\u6362\u56de\u539f\u59cb\u7a97\u53e3<br \/>\ndriver.switch_to.window(original_window)<br \/>\nprint(&#034;\u5207\u6362\u56de\u539f\u59cb\u7a97\u53e3URL&#xff1a;&#034;, driver.current_url)<br \/>\ntime.sleep(2)<\/p>\n<p># \u5173\u95ed\u5f53\u524d\u7a97\u53e3&#xff08;\u65b0\u7a97\u53e3&#xff09;<br \/>\ndriver.switch_to.window(all_windows[1])<br \/>\ndriver.close()<br \/>\ntime.sleep(2)<\/p>\n<p># \u5207\u6362\u56de\u539f\u59cb\u7a97\u53e3\u5e76\u7ee7\u7eed\u64cd\u4f5c<br \/>\ndriver.switch_to.window(original_window)<br \/>\nprint(&#034;\u6700\u7ec8\u7a97\u53e3URL&#xff1a;&#034;, driver.current_url)<\/p>\n<p>driver.quit()<\/p>\n<h5>5.3.2 iframe \u5207\u6362<\/h5>\n<p>iframe \u662f\u5185\u5d4c\u5728\u9875\u9762\u4e2d\u7684\u53e6\u4e00\u4e2a HTML \u6587\u6863&#xff0c;\u9700\u8981\u5148\u5207\u6362\u5230 iframe \u624d\u80fd\u64cd\u4f5c\u5176\u4e2d\u7684\u5143\u7d20&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/html\/tryit.asp?filename&#061;tryhtml_iframe&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># 1. \u901a\u8fc7id\u6216name\u5207\u6362iframe<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># \u5728iframe\u4e2d\u64cd\u4f5c\u5143\u7d20<br \/>\nheading &#061; driver.find_element(By.TAG_NAME, &#034;h1&#034;)<br \/>\nprint(&#034;iframe\u4e2d\u7684\u6807\u9898&#xff1a;&#034;, heading.text)<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u5207\u6362\u5230\u5d4c\u5957\u7684iframe&#xff08;iframe\u4e2d\u7684iframe&#xff09;<br \/>\nnested_iframe &#061; driver.find_element(By.TAG_NAME, &#034;iframe&#034;)<br \/>\ndriver.switch_to.frame(nested_iframe)<br \/>\ntime.sleep(2)<\/p>\n<p># \u5728\u5d4c\u5957iframe\u4e2d\u64cd\u4f5c<br \/>\nnested_heading &#061; driver.find_element(By.TAG_NAME, &#034;h1&#034;)<br \/>\nprint(&#034;\u5d4c\u5957iframe\u4e2d\u7684\u6807\u9898&#xff1a;&#034;, nested_heading.text)<br \/>\ntime.sleep(2)<\/p>\n<p># 3. \u5207\u6362\u56de\u7236\u7ea7iframe<br \/>\ndriver.switch_to.parent_frame()<br \/>\ntime.sleep(2)<\/p>\n<p># 4. \u5207\u6362\u56de\u4e3b\u6587\u6863<br \/>\ndriver.switch_to.default_content()<br \/>\ntime.sleep(2)<\/p>\n<p># \u5728\u4e3b\u6587\u6863\u4e2d\u64cd\u4f5c<br \/>\nmenu_item &#061; driver.find_element(By.LINK_TEXT, &#034;HTML Tutorial&#034;)<br \/>\nprint(&#034;\u4e3b\u6587\u6863\u4e2d\u7684\u83dc\u5355&#xff1a;&#034;, menu_item.text)<\/p>\n<p>driver.quit()<\/p>\n<h4>5.4 \u5904\u7406 JavaScript \u5f39\u7a97<\/h4>\n<p>Web \u9875\u9762\u4e2d\u5e38\u89c1\u7684\u5f39\u7a97\u6709\u4e09\u79cd&#xff1a;alert&#xff08;\u8b66\u544a\u6846&#xff09;\u3001confirm&#xff08;\u786e\u8ba4\u6846&#xff09;\u3001prompt&#xff08;\u63d0\u793a\u6846&#xff09;&#xff0c;Selenium \u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u5904\u7406\u65b9\u6cd5&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<\/p>\n<p>driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/js\/tryit.asp?filename&#061;tryjs_alert&#034;)<br \/>\ntime.sleep(2)<\/p>\n<p># \u5207\u6362\u5230iframe<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<\/p>\n<p># 1. \u5904\u7406alert\u5f39\u7a97<br \/>\nalert_button &#061; driver.find_element(By.XPATH, &#039;\/\/button[text()&#061;&#034;Try it&#034;]&#039;)<br \/>\nalert_button.click()<br \/>\ntime.sleep(2)<\/p>\n<p># \u5207\u6362\u5230alert<br \/>\nalert &#061; driver.switch_to.alert<br \/>\nprint(&#034;Alert\u6587\u672c&#xff1a;&#034;, alert.text)<\/p>\n<p># \u63a5\u53d7alert&#xff08;\u70b9\u51fb\u786e\u5b9a&#xff09;<br \/>\nalert.accept()<br \/>\ntime.sleep(2)<\/p>\n<p># 2. \u5904\u7406confirm\u5f39\u7a97<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/js\/tryit.asp?filename&#061;tryjs_confirm&#034;)<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<br \/>\nconfirm_button &#061; driver.find_element(By.XPATH, &#039;\/\/button[text()&#061;&#034;Try it&#034;]&#039;)<br \/>\nconfirm_button.click()<br \/>\ntime.sleep(2)<\/p>\n<p>confirm &#061; driver.switch_to.alert<br \/>\nprint(&#034;Confirm\u6587\u672c&#xff1a;&#034;, confirm.text)<\/p>\n<p># \u53d6\u6d88confirm&#xff08;\u70b9\u51fb\u53d6\u6d88&#xff09;<br \/>\nconfirm.dismiss()<br \/>\ntime.sleep(2)<\/p>\n<p># 3. \u5904\u7406prompt\u5f39\u7a97<br \/>\ndriver.get(&#034;https:\/\/www.w3schools.com\/js\/tryit.asp?filename&#061;tryjs_prompt&#034;)<br \/>\ndriver.switch_to.frame(&#034;iframeResult&#034;)<br \/>\nprompt_button &#061; driver.find_element(By.XPATH, &#039;\/\/button[text()&#061;&#034;Try it&#034;]&#039;)<br \/>\nprompt_button.click()<br \/>\ntime.sleep(2)<\/p>\n<p>prompt &#061; driver.switch_to.alert<br \/>\nprint(&#034;Prompt\u6587\u672c&#xff1a;&#034;, prompt.text)<\/p>\n<p># \u5411prompt\u8f93\u5165\u6587\u672c\u5e76\u786e\u8ba4<br \/>\nprompt.send_keys(&#034;Selenium&#034;)<br \/>\nprompt.accept()<br \/>\ntime.sleep(2)<\/p>\n<p># \u9000\u51faiframe<br \/>\ndriver.switch_to.default_content()<\/p>\n<p>driver.quit()<\/p>\n<h3>\u516d\u3001\u7b49\u5f85\u673a\u5236&#xff1a;\u5904\u7406\u9875\u9762\u52a0\u8f7d\u4e0e\u52a8\u6001\u5185\u5bb9<\/h3>\n<p>\u5728 Web \u81ea\u52a8\u5316\u4e2d&#xff0c;\u9875\u9762\u5143\u7d20\u7684\u52a0\u8f7d\u5f80\u5f80\u4e0d\u662f\u77ac\u95f4\u5b8c\u6210\u7684&#xff0c;\u7279\u522b\u662f\u4f7f\u7528 AJAX \u6216 JavaScript \u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\u3002\u5982\u679c\u811a\u672c\u6267\u884c\u901f\u5ea6\u5feb\u4e8e\u5143\u7d20\u52a0\u8f7d\u901f\u5ea6&#xff0c;\u5c31\u4f1a\u5bfc\u81f4 &#034;\u5143\u7d20\u672a\u627e\u5230&#034; \u7684\u9519\u8bef\u3002Selenium \u63d0\u4f9b\u4e86\u4e09\u79cd\u7b49\u5f85\u673a\u5236\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h4>6.1 \u5f3a\u5236\u7b49\u5f85&#xff08;sleep&#xff09;<\/h4>\n<p>\u5f3a\u5236\u7b49\u5f85\u662f\u6700\u7b80\u5355\u7684\u7b49\u5f85\u65b9\u5f0f&#xff0c;\u4f7f\u7528time.sleep(seconds)\u8ba9\u811a\u672c\u6682\u505c\u6307\u5b9a\u7684\u79d2\u6570&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import time<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<\/p>\n<p>driver &#061; webdriver.Chrome()<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p># \u5f3a\u5236\u7b49\u5f852\u79d2<br \/>\ntime.sleep(2)<\/p>\n<p>search_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<br \/>\nsearch_box.send_keys(&#034;Selenium&#034;)<br \/>\nsearch_box.submit()<\/p>\n<p># \u5f3a\u5236\u7b49\u5f853\u79d2&#xff0c;\u7b49\u5f85\u641c\u7d22\u7ed3\u679c\u52a0\u8f7d<br \/>\ntime.sleep(3)<\/p>\n<p>result &#061; driver.find_element(By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;)<br \/>\nprint(result.text)<\/p>\n<p>driver.quit()<\/p>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u65e0\u8bba\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u52a0\u8f7d&#xff0c;\u90fd\u4f1a\u7b49\u5f85\u56fa\u5b9a\u65f6\u95f4&#xff0c;\u964d\u4f4e\u811a\u672c\u6267\u884c\u6548\u7387<\/li>\n<li>\u96be\u4ee5\u786e\u5b9a\u5408\u9002\u7684\u7b49\u5f85\u65f6\u95f4&#xff08;\u8bbe\u7f6e\u592a\u77ed\u53ef\u80fd\u4ecd\u4f1a\u51fa\u9519&#xff0c;\u8bbe\u7f6e\u592a\u957f\u5219\u6d6a\u8d39\u65f6\u95f4&#xff09;<\/li>\n<\/ul>\n<h4>6.2 \u9690\u6027\u7b49\u5f85&#xff08;Implicit Wait&#xff09;<\/h4>\n<p>\u9690\u6027\u7b49\u5f85\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u7684\u7b49\u5f85\u65f6\u95f4&#xff0c;\u5728\u8fd9\u4e2a\u65f6\u95f4\u5185 Selenium \u4f1a\u4e0d\u65ad\u5c1d\u8bd5\u67e5\u627e\u5143\u7d20&#xff0c;\u76f4\u5230\u5143\u7d20\u88ab\u627e\u5230\u6216\u8d85\u65f6&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<\/p>\n<p>driver &#061; webdriver.Chrome()<br \/>\n# \u8bbe\u7f6e\u9690\u6027\u7b49\u5f85\u65f6\u95f4\u4e3a10\u79d2<br \/>\ndriver.implicitly_wait(10)<\/p>\n<p>driver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p>search_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<br \/>\nsearch_box.send_keys(&#034;Selenium&#034;)<br \/>\nsearch_box.submit()<\/p>\n<p># \u6700\u591a\u7b49\u5f8510\u79d2&#xff0c;\u76f4\u5230\u5143\u7d20\u51fa\u73b0<br \/>\nresult &#061; driver.find_element(By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;)<br \/>\nprint(result.text)<\/p>\n<p>driver.quit()<\/p>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u53ea\u9700\u8bbe\u7f6e\u4e00\u6b21&#xff0c;\u5bf9\u6574\u4e2a driver \u751f\u547d\u5468\u671f\u6709\u6548<\/li>\n<li>\u7b49\u5f85\u5143\u7d20\u88ab\u627e\u5230&#xff0c;\u4f46\u4e0d\u7b49\u5f85\u5143\u7d20\u53ef\u4ea4\u4e92<\/li>\n<li>\u8d85\u65f6\u540e\u4f1a\u629b\u51faNoSuchElementException<\/li>\n<\/ul>\n<h4>6.3 \u663e\u6027\u7b49\u5f85&#xff08;Explicit Wait&#xff09;<\/h4>\n<p>\u663e\u6027\u7b49\u5f85\u662f\u6700\u7075\u6d3b\u3001\u6700\u63a8\u8350\u7684\u7b49\u5f85\u65b9\u5f0f&#xff0c;\u5b83\u5141\u8bb8\u6211\u4eec\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3&#xff08;\u5982\u5143\u7d20\u53ef\u89c1\u3001\u53ef\u70b9\u51fb\u7b49&#xff09;&#xff0c;\u5e76\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u548c\u8f6e\u8be2\u95f4\u9694&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<\/p>\n<p>driver &#061; webdriver.Chrome()<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p>search_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<br \/>\nsearch_box.send_keys(&#034;Selenium&#034;)<br \/>\nsearch_box.submit()<\/p>\n<p># \u521b\u5efaWebDriverWait\u5bf9\u8c61&#xff0c;\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a10\u79d2<br \/>\nwait &#061; WebDriverWait(driver, 10)<\/p>\n<p># \u7b49\u5f85\u5143\u7d20\u53ef\u89c1&#xff08;\u6700\u591a10\u79d2&#xff09;<br \/>\nresult &#061; wait.until(<br \/>\n    EC.visibility_of_element_located((By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;))<br \/>\n)<br \/>\nprint(result.text)<\/p>\n<p># \u5176\u4ed6\u5e38\u7528\u6761\u4ef6<br \/>\n# \u7b49\u5f85\u5143\u7d20\u53ef\u70b9\u51fb<br \/>\n# button &#061; wait.until(EC.element_to_be_clickable((By.ID, &#034;button_id&#034;)))<\/p>\n<p># \u7b49\u5f85\u5143\u7d20\u5b58\u5728\u4e8eDOM\u4e2d<br \/>\n# element &#061; wait.until(EC.presence_of_element_located((By.ID, &#034;element_id&#034;)))<\/p>\n<p># \u7b49\u5f85\u6807\u9898\u5305\u542b\u6307\u5b9a\u6587\u672c<br \/>\n# wait.until(EC.title_contains(&#034;Selenium&#034;))<\/p>\n<p>driver.quit()<\/p>\n<h5>6.3.1 \u5e38\u7528\u7684 Expected Conditions<\/h5>\n<p>expected_conditions\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u9884\u5b9a\u4e49\u7684\u6761\u4ef6&#xff0c;\u5e38\u7528\u7684\u6709&#xff1a;<\/p>\n<\/p>\n<table>\n<tr>\u6761\u4ef6\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>title_is(title)<\/td>\n<td>\u9875\u9762\u6807\u9898\u7b49\u4e8e\u6307\u5b9a\u6807\u9898<\/td>\n<\/tr>\n<tr>\n<td>title_contains(substring)<\/td>\n<td>\u9875\u9762\u6807\u9898\u5305\u542b\u6307\u5b9a\u5b50\u4e32<\/td>\n<\/tr>\n<tr>\n<td>presence_of_element_located(locator)<\/td>\n<td>\u5143\u7d20\u5b58\u5728\u4e8e DOM \u4e2d<\/td>\n<\/tr>\n<tr>\n<td>visibility_of_element_located(locator)<\/td>\n<td>\u5143\u7d20\u53ef\u89c1&#xff08;\u5b58\u5728\u4e14\u53ef\u89c1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>visibility_of(element)<\/td>\n<td>\u5df2\u77e5\u5143\u7d20\u53ef\u89c1<\/td>\n<\/tr>\n<tr>\n<td>element_to_be_clickable(locator)<\/td>\n<td>\u5143\u7d20\u53ef\u70b9\u51fb<\/td>\n<\/tr>\n<tr>\n<td>element_located_to_be_selected(locator)<\/td>\n<td>\u5143\u7d20\u88ab\u9009\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>text_to_be_present_in_element(locator, text)<\/td>\n<td>\u5143\u7d20\u5305\u542b\u6307\u5b9a\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td>text_to_be_present_in_element_value(locator, text)<\/td>\n<td>\u5143\u7d20\u7684 value \u5c5e\u6027\u5305\u542b\u6307\u5b9a\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td>frame_to_be_available_and_switch_to_it(locator)<\/td>\n<td>iframe \u53ef\u7528\u5e76\u5207\u6362\u5230\u8be5 iframe<\/td>\n<\/tr>\n<tr>\n<td>invisibility_of_element_located(locator)<\/td>\n<td>\u5143\u7d20\u4e0d\u53ef\u89c1<\/td>\n<\/tr>\n<tr>\n<td>alert_is_present()<\/td>\n<td>\u5b58\u5728 alert \u5f39\u7a97<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>6.3.2 \u81ea\u5b9a\u4e49\u7b49\u5f85\u6761\u4ef6<\/h5>\n<p>\u5982\u679c\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u7b49\u5f85\u6761\u4ef6&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>driver &#061; webdriver.Chrome()<br \/>\ndriver.get(&#034;https:\/\/www.baidu.com&#034;)<\/p>\n<p>search_box &#061; driver.find_element(By.ID, &#034;kw&#034;)<br \/>\nsearch_box.send_keys(&#034;Selenium&#034;)<br \/>\nsearch_box.submit()<\/p>\n<p># \u81ea\u5b9a\u4e49\u6761\u4ef6&#xff1a;\u7b49\u5f85\u5143\u7d20\u6587\u672c\u957f\u5ea6\u5927\u4e8e10<br \/>\ndef text_length_greater_than_10(driver):<br \/>\n    element &#061; driver.find_element(By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;)<br \/>\n    return len(element.text) &gt; 10<\/p>\n<p># \u5e94\u7528\u81ea\u5b9a\u4e49\u6761\u4ef6<br \/>\nwait &#061; WebDriverWait(driver, 10)<br \/>\nresult &#061; wait.until(text_length_greater_than_10)<br \/>\nprint(f&#034;\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u6587\u672c&#xff1a;{result.text}&#xff0c;\u957f\u5ea6&#xff1a;{len(result.text)}&#034;)<\/p>\n<p>driver.quit()<\/p>\n<h4>6.4 \u7b49\u5f85\u673a\u5236\u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\u4f18\u5148\u4f7f\u7528\u663e\u6027\u7b49\u5f85&#xff1a;\u7075\u6d3b\u6027\u9ad8&#xff0c;\u53ea\u5728\u9700\u8981\u7b49\u5f85\u7684\u5730\u65b9\u5e94\u7528&#xff0c;\u4e0d\u5f71\u54cd\u5176\u4ed6\u64cd\u4f5c<\/li>\n<li>\u5408\u7406\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff1a;\u6839\u636e\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u8bbe\u7f6e&#xff0c;\u4e00\u822c 3-10 \u79d2<\/li>\n<li>\u907f\u514d\u6df7\u5408\u4f7f\u7528\u9690\u6027\u7b49\u5f85\u548c\u663e\u6027\u7b49\u5f85&#xff1a;\u53ef\u80fd\u5bfc\u81f4\u4e0d\u53ef\u9884\u6d4b\u7684\u7b49\u5f85\u65f6\u95f4<\/li>\n<li>\u9488\u5bf9\u4e0d\u540c\u64cd\u4f5c\u9009\u62e9\u5408\u9002\u7684\u6761\u4ef6&#xff1a;\n<ul>\n<li>\u8bfb\u53d6\u6587\u672c&#xff1a;\u4f7f\u7528visibility_of_element_located&#xff08;\u786e\u4fdd\u5143\u7d20\u53ef\u89c1&#xff09;<\/li>\n<li>\u70b9\u51fb\u6309\u94ae&#xff1a;\u4f7f\u7528element_to_be_clickable&#xff08;\u786e\u4fdd\u5143\u7d20\u53ef\u70b9\u51fb&#xff09;<\/li>\n<li>\u5224\u65ad\u5143\u7d20\u5b58\u5728&#xff1a;\u4f7f\u7528presence_of_element_located&#xff08;\u53ea\u9700\u5b58\u5728\u4e8e DOM \u4e2d&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\u7ed3\u5408\u4f7f\u7528\u5f3a\u5236\u7b49\u5f85&#xff1a;\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b&#xff0c;\u5982\u9875\u9762\u8df3\u8f6c\u3001\u6587\u4ef6\u4e0b\u8f7d\u7b49&#xff0c;\u53ef\u914d\u5408\u4f7f\u7528<\/li>\n<h3>\u4e03\u3001Selenium \u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u96c6\u6210<\/h3>\n<p>Selenium \u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7f51\u9875\u722c\u53d6&#xff0c;\u66f4\u662f\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u5229\u5668\u3002\u5c06 Selenium \u4e0e Python \u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6&#xff08;\u5982 unittest \u6216 pytest&#xff09;\u7ed3\u5408&#xff0c;\u53ef\u4ee5\u6784\u5efa\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u5957\u4ef6\u3002<\/p>\n<h4>7.1 \u4e0e unittest \u96c6\u6210<\/h4>\n<p>unittest \u662f Python \u5185\u7f6e\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6&#xff0c;\u63d0\u4f9b\u4e86\u6d4b\u8bd5\u7528\u4f8b\u3001\u6d4b\u8bd5\u5957\u4ef6\u3001\u65ad\u8a00\u7b49\u529f\u80fd&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import unittest<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>class TestBaiduSearch(unittest.TestCase):<br \/>\n    # \u5728\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5\u6267\u884c\u524d\u8fd0\u884c<br \/>\n    def setUp(self):<br \/>\n        self.driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\n        self.driver.maximize_window()<br \/>\n        self.driver.get(&#034;https:\/\/www.baidu.com&#034;)<br \/>\n        self.wait &#061; WebDriverWait(self.driver, 10)<\/p>\n<p>    # \u5728\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5\u6267\u884c\u540e\u8fd0\u884c<br \/>\n    def tearDown(self):<br \/>\n        self.driver.quit()<\/p>\n<p>    # \u6d4b\u8bd5\u65b9\u6cd5\u5fc5\u987b\u4ee5test\u5f00\u5934<br \/>\n    def test_search_selenium(self):<br \/>\n        # \u5b9a\u4f4d\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5185\u5bb9<br \/>\n        search_box &#061; self.wait.until(<br \/>\n            EC.element_to_be_clickable((By.ID, &#034;kw&#034;))<br \/>\n        )<br \/>\n        search_box.clear()<br \/>\n        search_box.send_keys(&#034;Selenium&#034;)<\/p>\n<p>        # \u70b9\u51fb\u641c\u7d22\u6309\u94ae<br \/>\n        search_button &#061; self.driver.find_element(By.ID, &#034;su&#034;)<br \/>\n        search_button.click()<\/p>\n<p>        # \u9a8c\u8bc1\u641c\u7d22\u7ed3\u679c<br \/>\n        result &#061; self.wait.until(<br \/>\n            EC.visibility_of_element_located((By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;))<br \/>\n        )<br \/>\n        self.assertIn(&#034;Selenium&#034;, result.text, &#034;\u641c\u7d22\u7ed3\u679c\u4e0d\u5305\u542b\u9884\u671f\u6587\u672c&#034;)<\/p>\n<p>    def test_search_python(self):<br \/>\n        # \u5b9a\u4f4d\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5185\u5bb9<br \/>\n        search_box &#061; self.wait.until(<br \/>\n            EC.element_to_be_clickable((By.ID, &#034;kw&#034;))<br \/>\n        )<br \/>\n        search_box.clear()<br \/>\n        search_box.send_keys(&#034;Python&#034;)<\/p>\n<p>        # \u70b9\u51fb\u641c\u7d22\u6309\u94ae<br \/>\n        search_button &#061; self.driver.find_element(By.ID, &#034;su&#034;)<br \/>\n        search_button.click()<\/p>\n<p>        # \u9a8c\u8bc1\u641c\u7d22\u7ed3\u679c<br \/>\n        result &#061; self.wait.until(<br \/>\n            EC.visibility_of_element_located((By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;))<br \/>\n        )<br \/>\n        self.assertIn(&#034;Python&#034;, result.text, &#034;\u641c\u7d22\u7ed3\u679c\u4e0d\u5305\u542b\u9884\u671f\u6587\u672c&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    # \u8fd0\u884c\u6240\u6709\u6d4b\u8bd5<br \/>\n    unittest.main()<\/p>\n<h4>7.2 \u4e0e pytest \u96c6\u6210<\/h4>\n<p>pytest \u662f\u6bd4 unittest \u66f4\u7075\u6d3b\u7684\u6d4b\u8bd5\u6846\u67b6&#xff0c;\u652f\u6301\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4e30\u5bcc\u7684\u63d2\u4ef6&#xff1a;<\/p>\n<\/p>\n<p>\u9996\u5148\u5b89\u88c5 pytest&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>pip install pytest<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import pytest<br \/>\nfrom selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p># \u6d4b\u8bd5\u524d\u7684\u51c6\u5907\u5de5\u4f5c<br \/>\n&#064;pytest.fixture(scope&#061;&#034;module&#034;)<br \/>\ndef driver():<br \/>\n    driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\n    driver.maximize_window()<br \/>\n    driver.get(&#034;https:\/\/www.baidu.com&#034;)<br \/>\n    yield driver  # \u6d4b\u8bd5\u5b8c\u6210\u540e\u6267\u884c\u540e\u7eed\u4ee3\u7801<br \/>\n    driver.quit()<\/p>\n<p># \u6d4b\u8bd5\u641c\u7d22\u529f\u80fd<br \/>\ndef test_search_selenium(driver):<br \/>\n    wait &#061; WebDriverWait(driver, 10)<\/p>\n<p>    # \u8f93\u5165\u641c\u7d22\u5185\u5bb9<br \/>\n    search_box &#061; wait.until(<br \/>\n        EC.element_to_be_clickable((By.ID, &#034;kw&#034;))<br \/>\n    )<br \/>\n    search_box.clear()<br \/>\n    search_box.send_keys(&#034;Selenium&#034;)<\/p>\n<p>    # \u70b9\u51fb\u641c\u7d22<br \/>\n    driver.find_element(By.ID, &#034;su&#034;).click()<\/p>\n<p>    # \u9a8c\u8bc1\u7ed3\u679c<br \/>\n    result &#061; wait.until(<br \/>\n        EC.visibility_of_element_located((By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;))<br \/>\n    )<br \/>\n    assert &#034;Selenium&#034; in result.text, &#034;\u641c\u7d22\u7ed3\u679c\u4e0d\u5305\u542b\u9884\u671f\u6587\u672c&#034;<\/p>\n<p># \u53c2\u6570\u5316\u6d4b\u8bd5&#xff1a;\u6d4b\u8bd5\u591a\u4e2a\u641c\u7d22\u5173\u952e\u8bcd<br \/>\n&#064;pytest.mark.parametrize(&#034;keyword&#034;, [&#034;Python&#034;, &#034;Java&#034;, &#034;JavaScript&#034;])<br \/>\ndef test_search_parametrized(driver, keyword):<br \/>\n    wait &#061; WebDriverWait(driver, 10)<\/p>\n<p>    # \u8f93\u5165\u641c\u7d22\u5185\u5bb9<br \/>\n    search_box &#061; wait.until(<br \/>\n        EC.element_to_be_clickable((By.ID, &#034;kw&#034;))<br \/>\n    )<br \/>\n    search_box.clear()<br \/>\n    search_box.send_keys(keyword)<\/p>\n<p>    # \u70b9\u51fb\u641c\u7d22<br \/>\n    driver.find_element(By.ID, &#034;su&#034;).click()<\/p>\n<p>    # \u9a8c\u8bc1\u7ed3\u679c<br \/>\n    result &#061; wait.until(<br \/>\n        EC.visibility_of_element_located((By.XPATH, &#039;\/\/*[&#064;id&#061;&#034;1&#034;]\/h3\/a&#039;))<br \/>\n    )<br \/>\n    assert keyword in result.text, f&#034;\u641c\u7d22{keyword}\u7684\u7ed3\u679c\u4e0d\u5305\u542b\u9884\u671f\u6587\u672c&#034;<\/p>\n<p>\u8fd0\u884c\u6d4b\u8bd5&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>pytest test_baidu.py -v  # -v \u663e\u793a\u8be6\u7ec6\u4fe1\u606f<\/p>\n<h4>7.3 \u751f\u6210\u6d4b\u8bd5\u62a5\u544a<\/h4>\n<p>\u4f7f\u7528 pytest-html \u63d2\u4ef6\u53ef\u4ee5\u751f\u6210\u7f8e\u89c2\u7684 HTML \u6d4b\u8bd5\u62a5\u544a&#xff1a;<\/p>\n<\/p>\n<p>\u5b89\u88c5\u63d2\u4ef6&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>pip install pytest-html<\/p>\n<p>\u8fd0\u884c\u6d4b\u8bd5\u5e76\u751f\u6210\u62a5\u544a&#xff1a;<\/p>\n<\/p>\n<p>bash<\/p>\n<p>pytest test_baidu.py -v &#8211;html&#061;report.html<\/p>\n<p>\u8fd0\u884c\u5b8c\u6210\u540e&#xff0c;\u4f1a\u751f\u6210 report.html \u6587\u4ef6&#xff0c;\u5305\u542b\u6d4b\u8bd5\u7ed3\u679c\u3001\u6267\u884c\u65f6\u95f4\u3001\u9519\u8bef\u4fe1\u606f\u7b49\u8be6\u7ec6\u5185\u5bb9\u3002<\/p>\n<h3>\u516b\u3001\u7f51\u9875\u6570\u636e\u722c\u53d6\u5b9e\u6218<\/h3>\n<p>Selenium \u4e0d\u4ec5\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5&#xff0c;\u4e5f\u662f\u7f51\u9875\u722c\u53d6\u7684\u91cd\u8981\u5de5\u5177&#xff0c;\u7279\u522b\u9002\u5408\u5904\u7406 JavaScript \u52a8\u6001\u6e32\u67d3\u7684\u9875\u9762\u3002\u4e0b\u9762\u901a\u8fc7\u51e0\u4e2a\u5b9e\u6218\u6848\u4f8b\u5c55\u793a Selenium \u5728\u6570\u636e\u722c\u53d6\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<h4>8.1 \u722c\u53d6\u9759\u6001\u7f51\u9875\u6570\u636e<\/h4>\n<p>\u4ee5\u722c\u53d6\u8c46\u74e3\u7535\u5f71 Top250 \u4e3a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<br \/>\nimport csv<\/p>\n<p>def crawl_douban_top250():<br \/>\n    # \u521d\u59cb\u5316\u6d4f\u89c8\u5668<br \/>\n    driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\n    driver.maximize_window()<br \/>\n    wait &#061; WebDriverWait(driver, 10)<\/p>\n<p>    # \u5b58\u50a8\u7ed3\u679c<br \/>\n    movies &#061; []<\/p>\n<p>    try:<br \/>\n        # \u722c\u53d610\u9875\u6570\u636e<br \/>\n        for page in range(10):<br \/>\n            url &#061; f&#034;https:\/\/movie.douban.com\/top250?start&#061;{page*25}&amp;filter&#061;&#034;<br \/>\n            driver.get(url)<br \/>\n            print(f&#034;\u6b63\u5728\u722c\u53d6\u7b2c{page&#043;1}\u9875&#xff1a;{url}&#034;)<\/p>\n<p>            # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<br \/>\n            wait.until(<br \/>\n                EC.visibility_of_element_located((By.CSS_SELECTOR, &#034;.grid_view li&#034;))<br \/>\n            )<\/p>\n<p>            # \u83b7\u53d6\u5f53\u524d\u9875\u7684\u7535\u5f71\u5217\u8868<br \/>\n            movie_list &#061; driver.find_elements(By.CSS_SELECTOR, &#034;.grid_view li&#034;)<\/p>\n<p>            for movie in movie_list:<br \/>\n                # \u63d0\u53d6\u7535\u5f71\u4fe1\u606f<br \/>\n                title &#061; movie.find_element(By.CSS_SELECTOR, &#034;.title&#034;).text<br \/>\n                rating &#061; movie.find_element(By.CSS_SELECTOR, &#034;.rating_num&#034;).text<br \/>\n                info &#061; movie.find_element(By.CSS_SELECTOR, &#034;.bd p:first-child&#034;).text<br \/>\n                quote &#061; movie.find_element(By.CSS_SELECTOR, &#034;.inq&#034;).text if movie.find_elements(By.CSS_SELECTOR, &#034;.inq&#034;) else &#034;&#034;<\/p>\n<p>                movies.append({<br \/>\n                    &#034;\u6392\u540d&#034;: len(movies) &#043; 1,<br \/>\n                    &#034;\u6807\u9898&#034;: title,<br \/>\n                    &#034;\u8bc4\u5206&#034;: rating,<br \/>\n                    &#034;\u4fe1\u606f&#034;: info,<br \/>\n                    &#034;\u5f15\u8a00&#034;: quote<br \/>\n                })<\/p>\n<p>            time.sleep(2)  # \u907f\u514d\u722c\u53d6\u8fc7\u5feb\u88ab\u53cd\u722c<\/p>\n<p>        # \u4fdd\u5b58\u6570\u636e\u5230CSV\u6587\u4ef6<br \/>\n        with open(&#034;douban_top250.csv&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8-sig&#034;, newline&#061;&#034;&#034;) as f:<br \/>\n            fieldnames &#061; [&#034;\u6392\u540d&#034;, &#034;\u6807\u9898&#034;, &#034;\u8bc4\u5206&#034;, &#034;\u4fe1\u606f&#034;, &#034;\u5f15\u8a00&#034;]<br \/>\n            writer &#061; csv.DictWriter(f, fieldnames&#061;fieldnames)<br \/>\n            writer.writeheader()<br \/>\n            writer.writerows(movies)<\/p>\n<p>        print(f&#034;\u722c\u53d6\u5b8c\u6210&#xff0c;\u5171{len(movies)}\u90e8\u7535\u5f71&#xff0c;\u5df2\u4fdd\u5b58\u5230douban_top250.csv&#034;)<\/p>\n<p>    except Exception as e:<br \/>\n        print(f&#034;\u722c\u53d6\u8fc7\u7a0b\u4e2d\u51fa\u9519&#xff1a;{e}&#034;)<\/p>\n<p>    finally:<br \/>\n        driver.quit()<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    crawl_douban_top250()<\/p>\n<h4>8.2 \u722c\u53d6\u52a8\u6001\u52a0\u8f7d\u6570\u636e<\/h4>\n<p>\u5f88\u591a\u7f51\u7ad9\u91c7\u7528\u6eda\u52a8\u52a0\u8f7d\u6216\u70b9\u51fb\u52a0\u8f7d\u66f4\u591a\u7684\u65b9\u5f0f\u52a0\u8f7d\u6570\u636e&#xff0c;\u5982 Twitter\u3001\u77e5\u4e4e\u7b49\u3002\u4e0b\u9762\u4ee5\u722c\u53d6\u6eda\u52a8\u52a0\u8f7d\u7684\u9875\u9762\u4e3a\u4f8b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<br \/>\nimport csv<br \/>\nimport random<\/p>\n<p>def crawl_scroll_page():<br \/>\n    # \u521d\u59cb\u5316\u6d4f\u89c8\u5668<br \/>\n    driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\n    driver.maximize_window()<br \/>\n    wait &#061; WebDriverWait(driver, 10)<\/p>\n<p>    # \u8bbf\u95ee\u76ee\u6807\u9875\u9762&#xff08;\u793a\u4f8b&#xff1a;\u77e5\u4e4e\u8bdd\u9898\u4e0b\u7684\u56de\u7b54&#xff09;<br \/>\n    url &#061; &#034;https:\/\/www.zhihu.com\/topic\/19552832\/top-answers&#034;<br \/>\n    driver.get(url)<br \/>\n    print(f&#034;\u8bbf\u95ee\u9875\u9762&#xff1a;{url}&#034;)<\/p>\n<p>    # \u5b58\u50a8\u7ed3\u679c<br \/>\n    answers &#061; []<\/p>\n<p>    try:<br \/>\n        # \u7b49\u5f85\u9875\u9762\u52a0\u8f7d<br \/>\n        wait.until(<br \/>\n            EC.visibility_of_element_located((By.CSS_SELECTOR, &#034;.List-item&#034;))<br \/>\n        )<\/p>\n<p>        # \u6eda\u52a8\u6b21\u6570&#xff08;\u6839\u636e\u9700\u8981\u8c03\u6574&#xff09;<br \/>\n        scroll_count &#061; 10<br \/>\n        last_height &#061; driver.execute_script(&#034;return document.body.scrollHeight&#034;)<\/p>\n<p>        for i in range(scroll_count):<br \/>\n            print(f&#034;\u7b2c{i&#043;1}\u6b21\u6eda\u52a8&#034;)<\/p>\n<p>            # \u6eda\u52a8\u5230\u9875\u9762\u5e95\u90e8<br \/>\n            driver.execute_script(&#034;window.scrollTo(0, document.body.scrollHeight);&#034;)<\/p>\n<p>            # \u968f\u673a\u7b49\u5f852-5\u79d2&#xff0c;\u6a21\u62df\u4eba\u7c7b\u884c\u4e3a<br \/>\n            sleep_time &#061; random.uniform(2, 5)<br \/>\n            print(f&#034;\u7b49\u5f85{sleep_time:.2f}\u79d2&#034;)<br \/>\n            time.sleep(sleep_time)<\/p>\n<p>            # \u8ba1\u7b97\u65b0\u7684\u9875\u9762\u9ad8\u5ea6\u5e76\u4e0e\u4e0a\u4e00\u6b21\u6bd4\u8f83<br \/>\n            new_height &#061; driver.execute_script(&#034;return document.body.scrollHeight&#034;)<br \/>\n            if new_height &#061;&#061; last_height:<br \/>\n                print(&#034;\u9875\u9762\u5df2\u6eda\u52a8\u5230\u5e95\u90e8&#xff0c;\u6ca1\u6709\u66f4\u591a\u5185\u5bb9&#034;)<br \/>\n                break<br \/>\n            last_height &#061; new_height<\/p>\n<p>        # \u63d0\u53d6\u6240\u6709\u56de\u7b54<br \/>\n        answer_elements &#061; driver.find_elements(By.CSS_SELECTOR, &#034;.List-item&#034;)<br \/>\n        print(f&#034;\u5171\u627e\u5230{len(answer_elements)}\u4e2a\u56de\u7b54&#034;)<\/p>\n<p>        for idx, element in enumerate(answer_elements):<br \/>\n            try:<br \/>\n                # \u63d0\u53d6\u56de\u7b54\u8005<br \/>\n                author &#061; element.find_element(By.CSS_SELECTOR, &#034;.UserLink-link&#034;).text<\/p>\n<p>                # \u63d0\u53d6\u56de\u7b54\u5185\u5bb9&#xff08;\u7b80\u5316\u7248&#xff09;<br \/>\n                content &#061; element.find_element(By.CSS_SELECTOR, &#034;.RichText&#034;).text[:200]  # \u53ea\u53d6\u524d200\u5b57<\/p>\n<p>                # \u63d0\u53d6\u70b9\u8d5e\u6570<br \/>\n                vote &#061; element.find_element(By.CSS_SELECTOR, &#034;.VoteButton-count&#034;).text<\/p>\n<p>                answers.append({<br \/>\n                    &#034;\u5e8f\u53f7&#034;: idx &#043; 1,<br \/>\n                    &#034;\u4f5c\u8005&#034;: author,<br \/>\n                    &#034;\u70b9\u8d5e\u6570&#034;: vote,<br \/>\n                    &#034;\u5185\u5bb9&#034;: content<br \/>\n                })<br \/>\n            except Exception as e:<br \/>\n                print(f&#034;\u63d0\u53d6\u7b2c{idx&#043;1}\u4e2a\u56de\u7b54\u65f6\u51fa\u9519&#xff1a;{e}&#034;)<br \/>\n                continue<\/p>\n<p>        # \u4fdd\u5b58\u6570\u636e<br \/>\n        with open(&#034;zhihu_answers.csv&#034;, &#034;w&#034;, encoding&#061;&#034;utf-8-sig&#034;, newline&#061;&#034;&#034;) as f:<br \/>\n            fieldnames &#061; [&#034;\u5e8f\u53f7&#034;, &#034;\u4f5c\u8005&#034;, &#034;\u70b9\u8d5e\u6570&#034;, &#034;\u5185\u5bb9&#034;]<br \/>\n            writer &#061; csv.DictWriter(f, fieldnames&#061;fieldnames)<br \/>\n            writer.writeheader()<br \/>\n            writer.writerows(answers)<\/p>\n<p>        print(f&#034;\u722c\u53d6\u5b8c\u6210&#xff0c;\u5171{len(answers)}\u4e2a\u56de\u7b54&#xff0c;\u5df2\u4fdd\u5b58\u5230zhihu_answers.csv&#034;)<\/p>\n<p>    except Exception as e:<br \/>\n        print(f&#034;\u722c\u53d6\u8fc7\u7a0b\u4e2d\u51fa\u9519&#xff1a;{e}&#034;)<\/p>\n<p>    finally:<br \/>\n        driver.quit()<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    crawl_scroll_page()<\/p>\n<h4>8.3 \u5904\u7406\u767b\u5f55\u9a8c\u8bc1<\/h4>\n<p>\u5f88\u591a\u7f51\u7ad9\u9700\u8981\u767b\u5f55\u624d\u80fd\u8bbf\u95ee\u5b8c\u6574\u5185\u5bb9&#xff0c;Selenium \u53ef\u4ee5\u6a21\u62df\u767b\u5f55\u8fc7\u7a0b&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from selenium import webdriver<br \/>\nfrom selenium.webdriver.common.by import By<br \/>\nfrom selenium.webdriver.support.ui import WebDriverWait<br \/>\nfrom selenium.webdriver.support import expected_conditions as EC<br \/>\nfrom webdriver_manager.chrome import ChromeDriverManager<br \/>\nimport time<br \/>\nimport getpass  # \u7528\u4e8e\u5b89\u5168\u8f93\u5165\u5bc6\u7801<\/p>\n<p>def simulate_login():<br \/>\n    # \u521d\u59cb\u5316\u6d4f\u89c8\u5668<br \/>\n    driver &#061; webdriver.Chrome(ChromeDriverManager().install())<br \/>\n    driver.maximize_window()<br \/>\n    wait &#061; WebDriverWait(driver, 10)<\/p>\n<p>    try:<br \/>\n        # \u8bbf\u95ee\u767b\u5f55\u9875\u9762&#xff08;\u793a\u4f8b&#xff1a;GitHub\u767b\u5f55&#xff09;<br \/>\n        driver.get(&#034;https:\/\/github.com\/login&#034;)<br \/>\n        print(&#034;\u5df2\u6253\u5f00GitHub\u767b\u5f55\u9875\u9762&#034;)<\/p>\n<p>        # \u8f93\u5165\u7528\u6237\u540d<br \/>\n        username_input &#061; wait.until(<br \/>\n            EC.element_to_be_clickable((By.ID, &#034;login_field&#034;))<br \/>\n        )<br \/>\n        username &#061; input(&#034;\u8bf7\u8f93\u5165GitHub\u7528\u6237\u540d&#xff1a;&#034;)<br \/>\n        username_input.send_keys(username)<\/p>\n<p>        # \u8f93\u5165\u5bc6\u7801<br \/>\n        password_input &#061; driver.find_element(By.ID, &#034;password&#034;)<br \/>\n        password &#061; getpass.getpass(&#034;\u8bf7\u8f93\u5165GitHub\u5bc6\u7801&#xff1a;&#034;)  # \u5bc6\u7801\u4e0d\u663e\u793a\u5728\u7ec8\u7aef<br \/>\n        password_input.send_keys(password)<\/p>\n<p>        # \u70b9\u51fb\u767b\u5f55\u6309\u94ae<br \/>\n        login_button &#061; driver.find_element(By.NAME, &#034;commit&#034;)<br \/>\n        login_button.click()<br \/>\n        time.sleep(3)<\/p>\n<p>        # \u9a8c\u8bc1\u767b\u5f55\u662f\u5426\u6210\u529f&#xff08;\u68c0\u67e5\u662f\u5426\u8df3\u8f6c\u5230\u9996\u9875&#xff09;<br \/>\n        try:<br \/>\n            wait.until(EC.url_contains(&#034;github.com&#034;))<br \/>\n            print(&#034;\u767b\u5f55\u6210\u529f&#xff01;&#034;)<\/p>\n<p>            # \u767b\u5f55\u540e\u7684\u64cd\u4f5c&#xff08;\u4f8b\u5982\u8bbf\u95ee\u4e2a\u4eba\u4e3b\u9875&#xff09;<br \/>\n            driver.get(f&#034;https:\/\/github.com\/{username}&#034;)<br \/>\n            wait.until(EC.title_contains(username))<br \/>\n            print(f&#034;\u5df2\u8fdb\u5165{username}\u7684\u4e2a\u4eba\u4e3b\u9875&#034;)<br \/>\n            time.sleep(5)<\/p>\n<p>        except Exception as e:<br \/>\n            print(&#034;\u767b\u5f55\u5931\u8d25&#xff1a;&#034;, e)<\/p>\n<p>    except Exception as e:<br \/>\n        print(f&#034;\u767b\u5f55\u8fc7\u7a0b\u4e2d\u51fa\u9519&#xff1a;{e}&#034;)<\/p>\n<p>    finally:<br \/>\n        driver.quit()<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    simulate_login()<\/p>\n<p>\u6ce8\u610f&#xff1a;\u81ea\u52a8\u5316\u767b\u5f55\u53ef\u80fd\u8fdd\u53cd\u67d0\u4e9b\u7f51\u7ad9\u7684\u7528\u6237\u534f\u8bae&#xff0c;\u4e14\u53ef\u80fd\u89e6\u53d1\u9a8c\u8bc1\u7801\u7b49\u53cd\u722c\u673a\u5236\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u8bf7\u9075\u5b88\u7f51\u7ad9\u7684 robots \u534f\u8bae\u548c\u4f7f\u7528\u6761\u6b3e\u3002<\/p>\n<h3>\u4e5d\u3001Selenium \u6027\u80fd\u4f18\u5316\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>9.1 \u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h4>\n<li>\n<p>\u4f7f\u7528\u65e0\u5934\u6d4f\u89c8\u5668&#xff1a;\u5728\u4e0d\u9700\u8981\u53ef\u89c6\u5316\u754c\u9762\u7684\u573a\u666f&#xff08;\u5982\u670d\u52a1\u5668\u73af\u5883&#xff09;&#xff0c;\u4f7f\u7528\u65e0\u5934\u6a21\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p> from selenium import webdriver<br \/>\nfrom selenium.webdriver.chrome.options import Options<\/p>\n<p>chrome_options &#061; Options()<br \/>\nchrome_options.add_argument(&#034;&#8211;headless&#061;new&#034;)  # \u65e0\u5934\u6a21\u5f0f<br \/>\nchrome_options.add_argument(&#034;&#8211;disable-gpu&#034;)  # \u7981\u7528GPU\u52a0\u901f<br \/>\ndriver &#061; webdriver.Chrome(options&#061;chrome_options)\n <\/li>\n<li>\n<p>\u7981\u7528\u4e0d\u5fc5\u8981\u7684\u529f\u80fd&#xff1a;\u5173\u95ed\u56fe\u7247\u52a0\u8f7d\u3001JavaScript \u7b49\u975e\u5fc5\u8981\u529f\u80fd&#xff08;\u6839\u636e\u9700\u6c42&#xff09;<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p> chrome_options &#061; Options()<br \/>\n# \u7981\u7528\u56fe\u7247\u52a0\u8f7d<br \/>\nprefs &#061; {&#034;profile.managed_default_content_settings.images&#034;: 2}<br \/>\nchrome_options.add_experimental_option(&#034;prefs&#034;, prefs)<br \/>\n# \u7981\u7528JavaScript<br \/>\nchrome_options.add_argument(&#034;&#8211;disable-javascript&#034;)\n <\/li>\n<li>\n<p>\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4&#xff1a;\u5408\u7406\u8bbe\u7f6e\u7b49\u5f85\u65f6\u95f4&#xff0c;\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7b49\u5f85<\/p>\n<\/li>\n<li>\n<p>\u590d\u7528\u6d4f\u89c8\u5668\u5b9e\u4f8b&#xff1a;\u5728\u591a\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u6216\u722c\u53d6\u4efb\u52a1\u4e2d\u590d\u7528\u540c\u4e00\u4e2a\u6d4f\u89c8\u5668\u5b9e\u4f8b&#xff0c;\u51cf\u5c11\u542f\u52a8\u5f00\u9500<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u66f4\u5feb\u7684\u9009\u62e9\u5668&#xff1a;\u4f18\u5148\u4f7f\u7528 ID\u3001NAME \u7b49\u5feb\u901f\u5b9a\u4f4d\u65b9\u5f0f&#xff0c;CSS \u9009\u62e9\u5668\u6027\u80fd\u901a\u5e38\u4f18\u4e8e XPath<\/p>\n<\/li>\n<li>\n<p>\u6279\u91cf\u64cd\u4f5c&#xff1a;\u4f7f\u7528 JavaScript \u6267\u884c\u6279\u91cf\u64cd\u4f5c&#xff0c;\u51cf\u5c11\u4e0e\u6d4f\u89c8\u5668\u7684\u4ea4\u4e92\u6b21\u6570<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p> # \u6279\u91cf\u8bbe\u7f6e\u591a\u4e2a\u5143\u7d20\u7684\u6587\u672c<br \/>\njs_code &#061; &#034;&#034;&#034;<br \/>\nvar elements &#061; document.getElementsByClassName(&#039;item&#039;);<br \/>\nfor(var i&#061;0; i&lt;elements.length; i&#043;&#043;){<br \/>\n    elements[i].textContent &#061; &#039;new text&#039;;<br \/>\n}<br \/>\n&#034;&#034;&#034;<br \/>\ndriver.execute_script(js_code)\n <\/li>\n<h4>9.2 \u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\n<p>\u5143\u7d20\u5b9a\u4f4d\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u5148\u4f7f\u7528 ID&#xff0c;\u5176\u6b21\u662f NAME\u3001CLASS<\/li>\n<li>\u590d\u6742\u573a\u666f\u4f7f\u7528\u76f8\u5bf9 XPath \u6216 CSS \u9009\u62e9\u5668<\/li>\n<li>\u907f\u514d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u548c\u52a8\u6001\u751f\u6210\u7684\u5c5e\u6027<\/li>\n<li>\u4e3a\u6d4b\u8bd5\u73af\u5883\u7684\u5143\u7d20\u6dfb\u52a0\u7a33\u5b9a\u7684\u6d4b\u8bd5 ID<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4ee3\u7801\u7ec4\u7ec7&#xff1a;<\/p>\n<ul>\n<li>\u4f7f\u7528 Page Object \u6a21\u5f0f\u5c01\u88c5\u9875\u9762\u64cd\u4f5c&#xff0c;\u63d0\u9ad8\u4ee3\u7801\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u5c06\u914d\u7f6e\u4fe1\u606f&#xff08;\u5982 URL\u3001\u7b49\u5f85\u65f6\u95f4&#xff09;\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb<\/li>\n<li>\u5f02\u5e38\u5904\u7406&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38&#xff08;\u5982\u5143\u7d20\u672a\u627e\u5230\u3001\u8d85\u65f6\u7b49&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53cd\u722c\u4e0e\u5408\u89c4&#xff1a;<\/p>\n<ul>\n<li>\u9075\u5b88\u7f51\u7ad9\u7684 robots \u534f\u8bae\u548c\u4f7f\u7528\u6761\u6b3e<\/li>\n<li>\u6a21\u62df\u4eba\u7c7b\u884c\u4e3a&#xff08;\u5408\u7406\u7684\u7b49\u5f85\u65f6\u95f4\u3001\u968f\u673a\u64cd\u4f5c\u95f4\u9694&#xff09;<\/li>\n<li>\u907f\u514d\u9891\u7e41\u8bf7\u6c42&#xff0c;\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694<\/li>\n<li>\u5fc5\u8981\u65f6\u4f7f\u7528\u4ee3\u7406 IP \u548c User-Agent \u6c60<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u5b9e\u8df5&#xff1a;<\/p>\n<ul>\n<li>\u4fdd\u6301\u6d4b\u8bd5\u7528\u4f8b\u7684\u72ec\u7acb\u6027&#xff0c;\u6bcf\u4e2a\u7528\u4f8b\u5e94\u80fd\u5355\u72ec\u8fd0\u884c<\/li>\n<li>\u6d4b\u8bd5\u6570\u636e\u4e0e\u6d4b\u8bd5\u4ee3\u7801\u5206\u79bb<\/li>\n<li>\u5b9a\u671f\u7ef4\u62a4\u6d4b\u8bd5\u7528\u4f8b&#xff0c;\u9002\u5e94\u9875\u9762\u53d8\u5316<\/li>\n<li>\u7ed3\u5408 CI\/CD \u5de5\u5177&#xff08;\u5982 Jenkins&#xff09;\u5b9e\u73b0\u6301\u7eed\u6d4b\u8bd5<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8c03\u8bd5\u6280\u5de7&#xff1a;<\/p>\n<ul>\n<li>\u4f7f\u7528driver.save_screenshot()\u5728\u5173\u952e\u6b65\u9aa4\u622a\u56fe&#xff0c;\u4fbf\u4e8e\u8c03\u8bd5<\/li>\n<li>\u5229\u7528print()\u6216\u65e5\u5fd7\u8f93\u51fa\u5173\u952e\u4fe1\u606f<\/li>\n<li>\u4f7f\u7528\u6d4f\u89c8\u5668\u5f00\u53d1\u8005\u5de5\u5177\u5206\u6790\u9875\u9762\u7ed3\u6784\u548c\u7f51\u7edc\u8bf7\u6c42<\/li>\n<\/ul>\n<\/li>\n<h3>\u5341\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<p>Selenium \u4f5c\u4e3a\u4e00\u6b3e\u6210\u719f\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177&#xff0c;\u5df2\u7ecf\u6210\u4e3a\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u548c\u6570\u636e\u722c\u53d6\u7231\u597d\u8005\u7684\u5fc5\u5907\u6280\u80fd\u3002\u5b83\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u63a7\u5236\u80fd\u529b\u3001\u8de8\u5e73\u53f0\u8de8\u6d4f\u89c8\u5668\u7684\u7279\u6027\u4ee5\u53ca\u4e30\u5bcc\u7684 API&#xff0c;\u4f7f\u5176\u80fd\u591f\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684 Web \u81ea\u52a8\u5316\u573a\u666f\u3002<\/p>\n<\/p>\n<p>\u672c\u6587\u4ece Selenium \u7684\u57fa\u672c\u6982\u5ff5\u51fa\u53d1&#xff0c;\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u73af\u5883\u642d\u5efa\u3001\u5143\u7d20\u5b9a\u4f4d\u3001\u4ea4\u4e92\u64cd\u4f5c\u3001\u6d4f\u89c8\u5668\u63a7\u5236\u3001\u7b49\u5f85\u673a\u5236\u7b49\u6838\u5fc3\u5185\u5bb9&#xff0c;\u5e76\u901a\u8fc7\u5927\u91cf\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86 Selenium \u5728\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u7f51\u9875\u722c\u53d6\u4e2d\u7684\u5e94\u7528\u3002\u540c\u65f6&#xff0c;\u6211\u4eec\u4e5f\u63a2\u8ba8\u4e86\u6027\u80fd\u4f18\u5316\u548c\u6700\u4f73\u5b9e\u8df5&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u7f16\u5199\u66f4\u9ad8\u6548\u3001\u66f4\u7a33\u5b9a\u7684\u81ea\u52a8\u5316\u811a\u672c\u3002<\/p>\n<\/p>\n<p>\u968f\u7740 Web \u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55&#xff0c;Selenium \u4e5f\u5728\u6301\u7eed\u8fdb\u5316\u3002\u672a\u6765&#xff0c;Selenium \u5c06\u66f4\u597d\u5730\u652f\u6301\u73b0\u4ee3 Web \u6846\u67b6&#xff08;\u5982 React\u3001Vue&#xff09;\u548c\u65b0\u5174\u6d4f\u89c8\u5668\u7279\u6027&#xff0c;\u540c\u65f6\u5728\u6027\u80fd\u548c\u6613\u7528\u6027\u4e0a\u4e0d\u65ad\u63d0\u5347\u3002\u638c\u63e1 Selenium \u4e0d\u4ec5\u80fd\u591f\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387&#xff0c;\u66f4\u80fd\u4e3a\u4ece\u4e8b\u6d4b\u8bd5\u5f00\u53d1\u3001\u6570\u636e\u91c7\u96c6\u7b49\u9886\u57df\u7684\u5de5\u4f5c\u6253\u4e0b\u575a\u5b9e\u57fa\u7840\u3002<\/p>\n<\/p>\n<p>\u65e0\u8bba\u662f\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u3001\u6570\u636e\u5206\u6790\u5e08\u8fd8\u662f\u5f00\u53d1\u4eba\u5458&#xff0c;\u90fd\u53ef\u4ee5\u901a\u8fc7 Selenium \u6253\u5f00 Web \u81ea\u52a8\u5316\u7684\u5927\u95e8&#xff0c;\u63a2\u7d22\u66f4\u591a\u53ef\u80fd\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u5165\u95e8\u5e76\u7cbe\u901a Selenium&#xff0c;\u8ba9\u81ea\u52a8\u5316\u6280\u672f\u4e3a\u4f60\u7684\u5de5\u4f5c\u5e26\u6765\u66f4\u591a\u4ef7\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb938\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u5982\u679c\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u7b49\u5f85\u6761\u4ef6\uff1apython\u8fd0\u884c# \u81ea\u5b9a\u4e49\u6761\u4ef6\uff1a\u7b49\u5f85\u5143\u7d20\u6587\u672c\u957f\u5ea6\u5927\u4e8e10# \u5e94\u7528\u81ea\u5b9a\u4e49\u6761\u4ef6print(f&quot;\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u6587\u672c\uff1a{result.text}\uff0c\u957f\u5ea6\uff1a{len(result.text)}&quot;)Selenium \u4f5c\u4e3a\u4e00\u6b3e\u6210\u719f\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5df2\u7ecf\u6210\u4e3a\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u548c\u6570\u636e\u722c\u53d6\u7231\u597d\u8005\u7684\u5fc5\u5907\u6280\u80fd\u3002\u5b83\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u63a7\u5236\u80fd\u529b\u3001\u8de8\u5e73\u53f0\u8de8\u6d4f\u89c8\u5668\u7684\u7279\u6027\u4ee5\u53ca\u4e30\u5bcc\u7684 API\uff0c\u4f7f\u5176\u80fd\u591f\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684 Web \u81ea\u52a8\u5316\u573a\u666f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3460,137],"topic":[],"class_list":["post-52142","post","type-post","status-publish","format-standard","hentry","category-server","tag-selenium","tag-137"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/52142.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb938\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u5982\u679c\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u7b49\u5f85\u6761\u4ef6\uff1apython\u8fd0\u884c# \u81ea\u5b9a\u4e49\u6761\u4ef6\uff1a\u7b49\u5f85\u5143\u7d20\u6587\u672c\u957f\u5ea6\u5927\u4e8e10# \u5e94\u7528\u81ea\u5b9a\u4e49\u6761\u4ef6print(f&quot;\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u6587\u672c\uff1a{result.text}\uff0c\u957f\u5ea6\uff1a{len(result.text)}&quot;)Selenium \u4f5c\u4e3a\u4e00\u6b3e\u6210\u719f\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5df2\u7ecf\u6210\u4e3a\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u548c\u6570\u636e\u722c\u53d6\u7231\u597d\u8005\u7684\u5fc5\u5907\u6280\u80fd\u3002\u5b83\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u63a7\u5236\u80fd\u529b\u3001\u8de8\u5e73\u53f0\u8de8\u6d4f\u89c8\u5668\u7684\u7279\u6027\u4ee5\u53ca\u4e30\u5bcc\u7684 API\uff0c\u4f7f\u5176\u80fd\u591f\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684 Web \u81ea\u52a8\u5316\u573a\u666f\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/52142.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T02:37:16+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52142.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/52142.html\",\"name\":\"\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T02:37:16+00:00\",\"dateModified\":\"2025-08-11T02:37:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/52142.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/52142.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52142.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5168\u9762\u4e86\u89e3selenium\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/52142.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb938\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf14\u6b21\u3002\u5982\u679c\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u7b49\u5f85\u6761\u4ef6\uff1apython\u8fd0\u884c# \u81ea\u5b9a\u4e49\u6761\u4ef6\uff1a\u7b49\u5f85\u5143\u7d20\u6587\u672c\u957f\u5ea6\u5927\u4e8e10# \u5e94\u7528\u81ea\u5b9a\u4e49\u6761\u4ef6print(f&quot;\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u6587\u672c\uff1a{result.text}\uff0c\u957f\u5ea6\uff1a{len(result.text)}&quot;)Selenium \u4f5c\u4e3a\u4e00\u6b3e\u6210\u719f\u7684 Web \u81ea\u52a8\u5316\u5de5\u5177\uff0c\u5df2\u7ecf\u6210\u4e3a\u6d4b\u8bd5\u5de5\u7a0b\u5e08\u548c\u6570\u636e\u722c\u53d6\u7231\u597d\u8005\u7684\u5fc5\u5907\u6280\u80fd\u3002\u5b83\u5f3a\u5927\u7684\u6d4f\u89c8\u5668\u63a7\u5236\u80fd\u529b\u3001\u8de8\u5e73\u53f0\u8de8\u6d4f\u89c8\u5668\u7684\u7279\u6027\u4ee5\u53ca\u4e30\u5bcc\u7684 API\uff0c\u4f7f\u5176\u80fd\u591f\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684 Web \u81ea\u52a8\u5316\u573a\u666f\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/52142.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T02:37:16+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"22 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/52142.html","url":"https:\/\/www.wsisp.com\/helps\/52142.html","name":"\u5168\u9762\u4e86\u89e3selenium - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T02:37:16+00:00","dateModified":"2025-08-11T02:37:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/52142.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/52142.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/52142.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5168\u9762\u4e86\u89e3selenium"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=52142"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52142\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=52142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=52142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=52142"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=52142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}