{"id":1136794,"date":"2025-01-08T21:40:04","date_gmt":"2025-01-08T13:40:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136794.html"},"modified":"2025-01-08T21:40:04","modified_gmt":"2025-01-08T13:40:04","slug":"python%e4%b8%ad%e5%90%8c%e4%b8%80%e9%a1%b5%e9%9d%a2%e5%85%83%e7%b4%a0%e5%a6%82%e4%bd%95%e5%ae%9a%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136794.html","title":{"rendered":"python\u4e2d\u540c\u4e00\u9875\u9762\u5143\u7d20\u5982\u4f55\u5b9a\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100812\/282718f4-afe2-4dbf-8939-9e0480ef86f3.webp\" alt=\"python\u4e2d\u540c\u4e00\u9875\u9762\u5143\u7d20\u5982\u4f55\u5b9a\u4f4d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528ID\u3001\u7c7b\u540d\u3001\u6807\u7b7e\u540d\u3001XPath\u548cCSS\u9009\u62e9\u5668\u7b49\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u89c6\u5177\u4f53\u60c5\u51b5\u800c\u5b9a\u3002<strong>XPath<\/strong> \u662f\u5176\u4e2d\u6700\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7\u8def\u5f84\u548c\u5c5e\u6027\u6765\u7cbe\u786e\u5b9a\u4f4d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>XPath<\/strong> \u662f\u4e00\u79cd\u67e5\u8be2\u8bed\u8a00\uff0c\u7528\u4e8e\u5728XML\u548cHTML\u6587\u6863\u4e2d\u67e5\u627e\u4fe1\u606f\u3002\u4f7f\u7528XPath\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5143\u7d20\u7684\u5c42\u7ea7\u5173\u7cfb\u3001\u5c5e\u6027\u503c\u4ee5\u53ca\u6587\u672c\u5185\u5bb9\u6765\u5b9a\u4f4d\u5143\u7d20\u3002XPath\u7684\u8bed\u6cd5\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u5b83\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u529f\u80fd\u4f7f\u5176\u6210\u4e3a\u590d\u6742\u9875\u9762\u5143\u7d20\u5b9a\u4f4d\u7684\u9996\u9009\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001XPath\u7684\u57fa\u672c\u8bed\u6cd5\u548c\u7528\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001XPath\u57fa\u7840<\/h3>\n<\/p>\n<p><p>XPath\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\u7edd\u5bf9\u8def\u5f84\u548c\u76f8\u5bf9\u8def\u5f84\u3002\u7edd\u5bf9\u8def\u5f84\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4f7f\u7528\u5355\u659c\u6760\uff08\/\uff09\u8868\u793a\uff1b\u76f8\u5bf9\u8def\u5f84\u4ece\u5f53\u524d\u8282\u70b9\u5f00\u59cb\uff0c\u4f7f\u7528\u53cc\u659c\u6760\uff08\/\/\uff09\u8868\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7edd\u5bf9\u8def\u5f84\u793a\u4f8b<\/p>\n<p>\/html\/body\/div[1]\/div[2]\/button<\/p>\n<h2><strong>\u76f8\u5bf9\u8def\u5f84\u793a\u4f8b<\/strong><\/h2>\n<p>\/\/div[@class=&#39;example-class&#39;]\/button<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5c5e\u6027\u5b9a\u4f4d<\/h3>\n<\/p>\n<p><p>XPath\u5141\u8bb8\u4f60\u4f7f\u7528\u5143\u7d20\u7684\u5c5e\u6027\u6765\u5b9a\u4f4d\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u5143\u7d20\u7684ID\u3001\u7c7b\u540d\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7ID\u5c5e\u6027\u5b9a\u4f4d<\/p>\n<p>\/\/*[@id=&#39;unique-id&#39;]<\/p>\n<h2><strong>\u901a\u8fc7\u7c7b\u540d\u5b9a\u4f4d<\/strong><\/h2>\n<p>\/\/*[@class=&#39;example-class&#39;]<\/p>\n<h2><strong>\u901a\u8fc7\u81ea\u5b9a\u4e49\u5c5e\u6027\u5b9a\u4f4d<\/strong><\/h2>\n<p>\/\/*[@data-custom=&#39;custom-value&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6587\u672c\u5185\u5bb9\u5b9a\u4f4d<\/h3>\n<\/p>\n<p><p>XPath\u8fd8\u652f\u6301\u901a\u8fc7\u5143\u7d20\u7684\u6587\u672c\u5185\u5bb9\u6765\u5b9a\u4f4d\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">\/\/button[text()=&#39;Submit&#39;]<\/p>\n<p>\/\/div[cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns(text(), &#39;Welcome&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528XPath<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528Selenium\u5e93<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u5b83\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u5305\u62ecPython\u3002\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u52a0\u8f7d\u7f51\u9875\u5e76\u4f7f\u7528XPath\u5b9a\u4f4d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Selenium\u52a0\u8f7d\u7f51\u9875\u5e76\u5b9a\u4f4d\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u4f7f\u7528XPath\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element_by_xpath(&#39;\/\/*[@id=&quot;unique-id&quot;]&#39;)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20<\/strong><\/h2>\n<p>element.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528BeautifulSoup\u548clxml\u5e93<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u548clxml\u662f\u4e24\u4e2a\u5e38\u7528\u7684HTML\u89e3\u6790\u5e93\uff0c\u9002\u7528\u4e8e\u9759\u6001\u7f51\u9875\u7684\u89e3\u6790\u548c\u5143\u7d20\u5b9a\u4f4d\u3002\u9996\u5148\uff0c\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528BeautifulSoup\u548clxml\u52a0\u8f7d\u7f51\u9875\u5e76\u4f7f\u7528XPath\u5b9a\u4f4d\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>import requests<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>web_content = response.content<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(web_content, &#39;lxml&#39;)<\/p>\n<h2><strong>\u4f7f\u7528XPath\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = soup.select_one(&#39;\/\/*[@id=&quot;unique-id&quot;]&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5143\u7d20\u6587\u672c<\/strong><\/h2>\n<p>print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001CSS\u9009\u62e9\u5668\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u9664\u4e86XPath\uff0cCSS\u9009\u62e9\u5668\u4e5f\u662f\u5b9a\u4f4d\u9875\u9762\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u3002CSS\u9009\u62e9\u5668\u8bed\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u5b9a\u4f4d\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>CSS\u9009\u62e9\u5668\u7684\u57fa\u672c\u8bed\u6cd5\u5305\u62ec\u5143\u7d20\u9009\u62e9\u3001ID\u9009\u62e9\u3001\u7c7b\u9009\u62e9\u3001\u5c5e\u6027\u9009\u62e9\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7d20\u9009\u62e9<\/p>\n<p>div<\/p>\n<h2><strong>ID\u9009\u62e9<\/strong><\/h2>\n<p>#unique-id<\/p>\n<h2><strong>\u7c7b\u9009\u62e9<\/strong><\/h2>\n<p>.example-class<\/p>\n<h2><strong>\u5c5e\u6027\u9009\u62e9<\/strong><\/h2>\n<p>div[data-custom=&#39;custom-value&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7ec4\u5408\u9009\u62e9\u5668<\/h3>\n<\/p>\n<p><p>CSS\u9009\u62e9\u5668\u8fd8\u652f\u6301\u7ec4\u5408\u9009\u62e9\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u6761\u4ef6\u7ec4\u5408\u6765\u7cbe\u786e\u5b9a\u4f4d\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b50\u5143\u7d20\u9009\u62e9<\/p>\n<p>div &gt; button<\/p>\n<h2><strong>\u540e\u4ee3\u5143\u7d20\u9009\u62e9<\/strong><\/h2>\n<p>div button<\/p>\n<h2><strong>\u5144\u5f1f\u5143\u7d20\u9009\u62e9<\/strong><\/h2>\n<p>div + p<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528Selenium\u548cBeautifulSoup\u5b9a\u4f4d<\/h3>\n<\/p>\n<p><p>\u5728Selenium\u548cBeautifulSoup\u4e2d\uff0c\u4f7f\u7528CSS\u9009\u62e9\u5668\u6765\u5b9a\u4f4d\u5143\u7d20\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u4f7f\u7528CSS\u9009\u62e9\u5668\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element_by_css_selector(&#39;#unique-id&#39;)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20<\/strong><\/h2>\n<p>element.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>import requests<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p>web_content = response.content<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(web_content, &#39;lxml&#39;)<\/p>\n<h2><strong>\u4f7f\u7528CSS\u9009\u62e9\u5668\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = soup.select_one(&#39;#unique-id&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5143\u7d20\u6587\u672c<\/strong><\/h2>\n<p>print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b9e\u4f8b\u89e3\u6790<\/h2>\n<\/p>\n<p><h3>1\u3001\u52a8\u6001\u52a0\u8f7d\u9875\u9762\u7684\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u9875\u9762\uff0c\u901a\u5e38\u9700\u8981\u7b49\u5f85\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u64cd\u4f5c\u3002Selenium\u63d0\u4f9b\u4e86\u663e\u5f0f\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85\u4e24\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u663e\u5f0f\u7b49\u5f85<\/h4>\n<\/p>\n<p><p>\u663e\u5f0f\u7b49\u5f85\u4f1a\u7b49\u5f85\u7279\u5b9a\u7684\u6761\u4ef6\u53d1\u751f\uff0c\u7136\u540e\u518d\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u7b49\u5f85\u5143\u7d20\u53ef\u70b9\u51fb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u7b49\u5f85\u5143\u7d20\u53ef\u70b9\u51fb<\/strong><\/h2>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.element_to_be_clickable((By.ID, &#39;unique-id&#39;))<\/p>\n<p>)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20<\/strong><\/h2>\n<p>element.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9690\u5f0f\u7b49\u5f85<\/h4>\n<\/p>\n<p><p>\u9690\u5f0f\u7b49\u5f85\u4f1a\u5728\u6574\u4e2a\u4f1a\u8bdd\u671f\u95f4\u7b49\u5f85\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u65f6\u95f4<\/p>\n<p>driver.implicitly_wait(10)<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element_by_id(&#39;unique-id&#39;)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20<\/strong><\/h2>\n<p>element.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u5d4c\u5957\u7684iframe<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u4f7f\u7528iframe\u5d4c\u5957\u5176\u4ed6\u9875\u9762\u5185\u5bb9\u3002\u4e3a\u4e86\u5b9a\u4f4diframe\u4e2d\u7684\u5143\u7d20\uff0c\u9700\u8981\u5148\u5207\u6362\u5230iframe\u518d\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5207\u6362\u5230iframe<\/p>\n<p>driver.switch_to.frame(&#39;iframe-name&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4diframe\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element_by_xpath(&#39;\/\/*[@id=&quot;unique-id&quot;]&#39;)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20<\/strong><\/h2>\n<p>element.click()<\/p>\n<h2><strong>\u5207\u6362\u56de\u4e3b\u6587\u6863<\/strong><\/h2>\n<p>driver.switch_to.default_content()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>ID\u3001\u7c7b\u540d\u3001\u6807\u7b7e\u540d\u3001XPath<\/strong>\u548c<strong>CSS\u9009\u62e9\u5668<\/strong>\u3002\u5176\u4e2d\uff0c<strong>XPath<\/strong> \u662f\u6700\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u590d\u6742\u9875\u9762\u5143\u7d20\u7684\u7cbe\u786e\u5b9a\u4f4d\uff1b\u800c<strong>CSS\u9009\u62e9\u5668<\/strong> \u5219\u8bed\u6cd5\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u5b9a\u4f4d\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4f7f\u7528Selenium\u8fdb\u884c\u52a8\u6001\u9875\u9762\u64cd\u4f5c\uff0c\u8fd8\u662f\u4f7f\u7528BeautifulSoup\u89e3\u6790\u9759\u6001\u7f51\u9875\uff0c\u9009\u62e9\u5408\u9002\u7684\u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5\u90fd\u80fd\u6781\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Selenium\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u7684\u591a\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u4f7f\u7528<code>find_elements<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5143\u7d20\u5217\u8868\uff0c\u8fdb\u800c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">elements = driver.find_elements(By.CLASS_NAME, &#39;your-class-name&#39;)\nfor element in elements:\n    print(element.text)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\u540e\u518d\u6267\u884c\u5b9a\u4f4d\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u663e\u5f0f\u7b49\u5f85\u6765\u63d0\u9ad8\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528XPath\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u7684\u5143\u7d20\u6709\u54ea\u4e9b\u6280\u5de7\uff1f<\/strong><br \/>XPath\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b9a\u4f4d\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u7279\u5b9a\u7684\u8def\u5f84\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u7684\u591a\u4e2a\u5143\u7d20\u3002\u4f7f\u7528<code>\/\/tagname[@attribute=&#39;value&#39;]<\/code>\u7684\u683c\u5f0f\u53ef\u4ee5\u9009\u62e9\u76f8\u540c\u5c5e\u6027\u7684\u591a\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>\/\/div[@class=&#39;example&#39;]<\/code>\u5c06\u4f1a\u9009\u62e9\u6240\u6709class\u4e3aexample\u7684div\u5143\u7d20\u3002\u7ed3\u5408\u4f7f\u7528<code>find_elements<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728\u5b9a\u4f4d\u5143\u7d20\u65f6\uff0c\u5982\u4f55\u907f\u514d\u56e0\u52a8\u6001\u52a0\u8f7d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u52a8\u6001\u52a0\u8f7d\u7684\u9875\u9762\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5143\u7d20\u5728\u5b9a\u4f4d\u65f6\u5c1a\u672a\u51fa\u73b0\u3002\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u7b49\u5f85\uff08WebDriverWait\uff09\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u8bbe\u7f6e\u7b49\u5f85\u6761\u4ef6\uff0c\u786e\u4fdd\u5143\u7d20\u5728\u64cd\u4f5c\u524d\u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6210\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\n\nelements = WebDriverWait(driver, 10).until(\n    EC.presence_of_all_elements_located((By.CLASS_NAME, &#39;your-class-name&#39;))\n)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u5b9a\u4f4d\u7684\u6210\u529f\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4f4d\u540c\u4e00\u9875\u9762\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528ID\u3001\u7c7b\u540d\u3001\u6807\u7b7e\u540d\u3001XPath\u548cCSS\u9009\u62e9\u5668\u7b49\u3002 \u8fd9\u4e9b\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136794"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1136794"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136794\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}