{"id":751,"date":"2023-02-26T16:03:48","date_gmt":"2023-02-26T08:03:48","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=751"},"modified":"2023-04-29T17:01:30","modified_gmt":"2023-04-29T09:01:30","slug":"python-selenium-element-positioning","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-element-positioning\/","title":{"rendered":"Python Selenium\u5143\u7d20\u5b9a\u4f4d"},"content":{"rendered":"<h2>Selenium\u5b9a\u4f4d\u65b9\u6cd5<\/h2>\n<p>Selenium\u63d0\u4f9b\u4e868\u79cd\u5b9a\u4f4d\u65b9\u5f0f\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>id<\/li>\n<li>name<\/li>\n<li>class name<\/li>\n<li>tag name<\/li>\n<li>link text<\/li>\n<li>partial link text<\/li>\n<li>xpath<\/li>\n<li>css selector<\/li>\n<\/ul>\n<p>\u8fd98\u79cd\u5b9a\u4f4d\u65b9\u5f0f\u5728Python Selenium\u4e2d\u6240\u5bf9\u5e94\u7684\u65b9\u6cd5\u4e3a\uff1a<\/p>\n<ul>\n<li>find_element_by_id()<\/li>\n<li>find_element_by_name()<\/li>\n<li>find_element_by_class_name()<\/li>\n<li>find_element_by_tag_name()<\/li>\n<li>find_element_by_link_text()<\/li>\n<li>find_element_by_partial_link_text()<\/li>\n<li>find_element_by_xpath()<\/li>\n<li>find_element_by_css_selector()<\/li>\n<\/ul>\n<h2>\u5b9a\u4f4d\u65b9\u6cd5\u7528\u6cd5<\/h2>\n<p>\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2aWeb\u9875\u9762\uff0c\u901a\u8fc7\u524d\u7aef\u5de5\u5177\uff08\u5982\uff0cFirebug\uff09\u67e5\u770b\u5230\u4e00\u4e2a\u5143\u7d20\u7684\u5c5e\u6027\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<pre><code class=\"language-html\">&lt;html&gt;\n  &lt;head&gt;\n  &lt;body link=&quot;#0000cc&quot;&gt;\n    &lt;a id=&quot;result_logo&quot; href=&quot;\/&quot; onmousedown=&quot;return c({&#039;fm&#039;:&#039;tab&#039;,&#039;tab&#039;:&#039;logo&#039;})&quot;&gt;\n    &lt;form id=&quot;form&quot; class=&quot;fm&quot; name=&quot;f&quot; action=&quot;\/s&quot;&gt;\n      &lt;span class=&quot;soutu-btn&quot;&gt;&lt;\/span&gt;\n        &lt;input id=&quot;kw&quot; class=&quot;s_ipt&quot; name=&quot;wd&quot; value=&quot;&quot; maxlength=&quot;255&quot; autocomplete=&quot;off&quot;&gt;<\/code><\/pre>\n<p>\u6211\u4eec\u7684\u76ee\u7684\u662f\u8981\u5b9a\u4f4dinput\u6807\u7b7e\u7684\u8f93\u5165\u6846\u3002<\/p>\n<h3>\u901a\u8fc7id\u5b9a\u4f4d<\/h3>\n<pre><code class=\"language-python\">driver.find_element_by_id(&quot;kw&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7name\u5b9a\u4f4d<\/h3>\n<pre><code class=\"language-python\">driver.find_element_by_name(&quot;wd&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7class name\u5b9a\u4f4d<\/h3>\n<pre><code class=\"language-python\">driver.find_element_by_class_name(&quot;s_ipt&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7tag name\u5b9a\u4f4d<\/h3>\n<pre><code class=\"language-python\">driver.find_element_by_tag_name(&quot;input&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7xpath\u5b9a\u4f4d<\/h3>\n<p>xpath\u5b9a\u4f4d\u6709N\u79cd\u5199\u6cd5\uff0c\u8fd9\u91cc\u5217\u51e0\u4e2a\u5e38\u7528\u5199\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">driver.find_element_by_xpath(&quot;\/\/*[@id=&#039;kw&#039;]&quot;)\ndriver.find_element_by_xpath(&quot;\/\/*[@name=&#039;wd&#039;]&quot;)\ndriver.find_element_by_xpath(&quot;\/\/input[@class=&#039;s_ipt&#039;]&quot;)\ndriver.find_element_by_xpath(&quot;\/html\/body\/form\/span\/input&quot;)\ndriver.find_element_by_xpath(&quot;\/\/span[@class=&#039;soutu-btn&#039;]\/input&quot;)\ndriver.find_element_by_xpath(&quot;\/\/form[@id=&#039;form&#039;]\/span\/input&quot;)\ndriver.find_element_by_xpath(&quot;\/\/input[@id=&#039;kw&#039; and @name=&#039;wd&#039;]&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7css\u5b9a\u4f4d<\/h3>\n<p>css\u5b9a\u4f4d\u6709N\u79cd\u5199\u6cd5\uff0c\u8fd9\u91cc\u5217\u51e0\u4e2a\u5e38\u7528\u5199\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">driver.find_element_by_css_selector(&quot;#kw&quot;)\ndriver.find_element_by_css_selector(&quot;[name=wd]&quot;)\ndriver.find_element_by_css_selector(&quot;.s_ipt&quot;)\ndriver.find_element_by_css_selector(&quot;html &gt; body &gt; form &gt; span &gt; input&quot;)\ndriver.find_element_by_css_selector(&quot;span.soutu-btn&gt; input#kw&quot;)\ndriver.find_element_by_css_selector(&quot;form#form &gt; span &gt; input&quot;)<\/code><\/pre>\n<h3>\u901a\u8fc7link text\u5b9a\u4f4d<\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7684\u9875\u9762\u4e0a\u6709\u4e00\u7ec4\u6587\u672c\u94fe\u63a5\u3002<\/p>\n<pre><code class=\"language-html\">&lt;a class=&quot;mnav&quot; href=&quot;http:\/\/news.baidu.com&quot; name=&quot;tj_trnews&quot;&gt;\u65b0\u95fb&lt;\/a&gt;\n&lt;a class=&quot;mnav&quot; href=&quot;http:\/\/www.hao123.com&quot; name=&quot;tj_trhao123&quot;&gt;hao123&lt;\/a&gt;<\/code><\/pre>\n<p>\u901a\u8fc7link text\u5b9a\u4f4d\uff1a<\/p>\n<pre><code class=\"language-python\">driver.find_element_by_link_text(&quot;\u65b0\u95fb&quot;)\ndriver.find_element_by_link_text(&quot;hao123&quot;)\n\ndriver.find_element_by_partial_link_text(&quot;\u65b0&quot;)\ndriver.find_element_by_partial_link_text(&quot;hao&quot;)\ndriver.find_element_by_partial_link_text(&quot;123&quot;)<\/code><\/pre>\n<p>\u5173\u4e8expaht\u548ccss\u7684\u5b9a\u4f4d\u6bd4\u8f83\u590d\u6742\uff0c\u8bf7\u53c2\u8003\uff1a [xpath\u8bed\u6cd5](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.w3school.com.cn\/xpath\/xpath_syntax.asp\">http:\/\/www.w3school.com.cn\/xpath\/xpath_syntax.asp<\/a> xpath\u8bed\u6cd5)\u3001 [css\u9009\u62e9\u5668](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.w3school.com.cn\/cssref\/css_selectors.asp\">http:\/\/www.w3school.com.cn\/cssref\/css_selectors.asp<\/a> css\u9009\u62e9\u5668)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selenium\u5b9a\u4f4d\u65b9\u6cd5 Selenium\u63d0\u4f9b\u4e868\u79cd\u5b9a\u4f4d\u65b9\u5f0f\uff1a id name class name tag  [&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-751","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/751","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=751"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/751\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}