{"id":753,"date":"2023-02-26T16:05:19","date_gmt":"2023-02-26T08:05:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=753"},"modified":"2023-04-29T17:01:06","modified_gmt":"2023-04-29T09:01:06","slug":"python-selenium-webdriver-common-methods","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-webdriver-common-methods\/","title":{"rendered":"Python Selenium\u4e4bWebDriver\u5e38\u7528\u65b9\u6cd5"},"content":{"rendered":"<h2>\u70b9\u51fb\u548c\u8f93\u5165<\/h2>\n<p>\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5b9a\u4f4d\u5143\u7d20\uff0c\u5b9a\u4f4d\u53ea\u662f\u7b2c\u4e00\u6b65\uff0c\u5b9a\u4f4d\u4e4b\u540e\u9700\u8981\u5bf9\u8fd9\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u5355\u51fb\uff08\u6309\u94ae\uff09\u6216\u8f93\u5165\uff08\u8f93\u5165\u6846\uff09\uff0c\u4e0b\u9762\u5c31\u6765\u8ba4\u8bc6 WebDriver \u4e2d\u6700\u5e38\u7528\u7684\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>clear()\uff1a\u6e05\u9664\u6587\u672c<\/li>\n<li>send_keys (value)\uff1a\u6a21\u62df\u6309\u952e\u8f93\u5165<\/li>\n<li>click()\uff1a\u5355\u51fb\u5143\u7d20<\/li>\n<\/ul>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;https:\/\/www.baidu.com&quot;)\n\ndriver.find_element_by_id(&quot;kw&quot;).clear()\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(&quot;selenium&quot;)\ndriver.find_element_by_id(&quot;su&quot;).click()\n\ndriver.quit()<\/code><\/pre>\n<h2>\u63d0\u4ea4<\/h2>\n<ul>\n<li>submit()<\/li>\n<\/ul>\n<p>submit()\u65b9\u6cd5\u7528\u4e8e\u63d0\u4ea4\u8868\u5355\u3002\u4f8b\u5982\uff0c\u5728\u641c\u7d22\u6846\u8f93\u5165\u5173\u952e\u5b57\u4e4b\u540e\u7684\u201c\u56de\u8f66\u201d\u64cd\u4f5c\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u65b9\u6cd5\u6a21\u62df\u3002<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;https:\/\/www.baidu.com&quot;)\n\nsearch_text = driver.find_element_by_id(&#039;kw&#039;)\nsearch_text.send_keys(&#039;selenium&#039;)\nsearch_text.submit()\n\ndriver.quit()<\/code><\/pre>\n<p>\u6709\u65f6\u5019 submit()\u53ef\u4ee5\u4e0e click()\u65b9\u6cd5\u4e92\u6362\u6765\u4f7f\u7528\uff0csubmit()\u540c\u6837\u53ef\u4ee5\u63d0\u4ea4\u4e00\u4e2a\u6309\u94ae\uff0c\u4f46 submit()\u7684\u5e94\u7528\u8303\u56f4\u8fdc\u4e0d\u53ca click()\u5e7f\u6cdb\u3002<\/p>\n<h2>\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5<\/h2>\n<ul>\n<li>size\uff1a\u8fd4\u56de\u5143\u7d20\u7684\u5c3a\u5bf8<\/li>\n<li>text\uff1a\u83b7\u53d6\u5143\u7d20\u7684\u6587\u672c<\/li>\n<li>get_attribute(name)\uff1a\u83b7\u5f97\u5c5e\u6027\u503c<\/li>\n<li>is_displayed()\uff1a\u8bbe\u7f6e\u8be5\u5143\u7d20\u662f\u5426\u7528\u6237\u53ef\u89c1<\/li>\n<\/ul>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;http:\/\/www.baidu.com&quot;)\n\n# \u83b7\u5f97\u8f93\u5165\u6846\u7684\u5c3a\u5bf8\nsize = driver.find_element_by_id(&#039;kw&#039;).size\nprint(size)\n\n# \u8fd4\u56de\u767e\u5ea6\u9875\u9762\u5e95\u90e8\u5907\u6848\u4fe1\u606f\ntext = driver.find_element_by_id(&quot;cp&quot;).text\nprint(text)\n\n# \u8fd4\u56de\u5143\u7d20\u7684\u5c5e\u6027\u503c\uff0c\u53ef\u4ee5\u662f id\u3001 name\u3001 type \u6216\u5176\u4ed6\u4efb\u610f\u5c5e\u6027\nattribute = driver.find_element_by_id(&quot;kw&quot;).get_attribute(&#039;type&#039;)\nprint(attribute)\n\n# \u8fd4\u56de\u5143\u7d20\u7684\u7ed3\u679c\u662f\u5426\u53ef\u89c1\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a True \u6216 False\nresult = driver.find_element_by_id(&quot;kw&quot;).is_displayed()\nprint(result)\n\ndriver.quit()<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>{&#039;width&#039;: 500, &#039;height&#039;: 22}\n\u00a92015 Baidu \u4f7f\u7528\u767e\u5ea6\u524d\u5fc5\u8bfb \u610f\u89c1\u53cd\u9988 \u4eac ICP \u8bc1 030173 \u53f7\ntext\nTrue<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u70b9\u51fb\u548c\u8f93\u5165 \u524d\u9762\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u5b9a\u4f4d\u5143\u7d20\uff0c\u5b9a\u4f4d\u53ea\u662f\u7b2c\u4e00\u6b65\uff0c\u5b9a\u4f4d\u4e4b\u540e\u9700\u8981\u5bf9\u8fd9\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u6216\u5355\u51fb\uff08\u6309\u94ae\uff09\u6216\u8f93\u5165\uff08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}