<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # selenium定位方式 * [Xpath定位](http://brtc.imsam.cn/1377978#Xpath_41) * [CSS定位](http://brtc.imsam.cn/1377978#CSS_93) 在進行web自動化之前,需要先來了解一下web頁面的基本內容。 比如,如下是一張百度首頁 ![](https://img.kancloud.cn/6f/61/6f61b72faa96d04e90e9b36b1e47c725_1051x445.png) 那如果我們需要對百度頁面進行操作,那么我們就需要知道他的html代碼了。 我們可以鼠標選中需要操作的元素,`鼠標右鍵,檢查` ![](https://img.kancloud.cn/a1/65/a16560ef0afcb217a26894bacd3fdb19_908x506.png) 這一行代碼就是百度輸入框的代碼了 ![](https://img.kancloud.cn/5e/16/5e164213274dd5f2382d5f60ea463b93_1171x671.png) 來看看這行代碼 ~~~ <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> ~~~ 首先第一個,這個是一個html代碼,那html代碼中 這個代碼我們稱之為input標簽 那在這個input標簽中,有很多的屬性,其中`=`左邊的是屬性名稱,`=`右邊的是屬性值 所以這個html標簽有id、name、class、value、maxlength等等屬性 第二個,HTML標簽有自己的層級關系 如圖,span標簽就是input標簽的父標簽 ![](https://img.kancloud.cn/e3/d6/e3d6f385065d0a3be464280b742cbd12_1214x325.png) | 定位方式 | 定位表達式 | | --- | --- | | 根據id來定位 | d.find\_element\_by\_id('id的值') | | 根據name來定位 | d.find\_element\_by\_name('name的值') | | 根據class來定位 | d.find\_element\_by\_class\_name('class的值') | | 根據標簽名定位 | d.find\_element\_by\_tag\_name('標簽名') | | 根據文本鏈接定位 | d.find\_element\_by\_link\_text('完整的文本鏈接') | | 根據部分文本鏈接定位 | d.find\_element\_by\_partial\_link\_text('部分的文本鏈接') | | 根據css選擇器定位 | d.find\_element\_by\_css\_selector('css表達式') | | 根據xpath定位 | d.find\_element\_by\_xpath('xpath表達式') | > ### Xpath定位 * 根據絕對路徑定位 ~~~ /html/body/div/form/p/label/input /html/body/div/form/p[2]/label/input ~~~ * 路徑與屬性的結合 `//標簽名[@屬性名="屬性值"]` 比如說:想定位到 for="user\_pass" 的label標簽 `//label[@for="user_pass"]` * 如果想同時使用多個屬性去定位 語法: `//標簽名[@屬性名="屬性值" and @屬性名="屬性值"]` 想定位到:class="input" id="user\_pass" 的 input標簽 `//input[@class="input" and @id="user_pass"]` 比如:定位到 for="user\_pass" 的label標簽 的 input子標簽 `//label[@for="user_pass"]/input` 使用Xpath實現登錄blog ~~~ from selenium import webdriver from time import sleep d = webdriver.Chrome() d.get('http://os-201804081018/blog/wp-login.php') d.maximize_window() sleep(3) d.find_element_by_xpath('//label[@for="user_login"]/input').send_keys('test') sleep(1) d.find_element_by_xpath('/html/body/div/form/p[2]/label/input').send_keys('test123') sleep(1) d.find_element_by_xpath('//input[@class="button button-primary button-large"]').submit() sleep(1) d.close() d.quit() ~~~ > ### CSS定位 * css中用空格 表示下級標簽(可以是子標簽,也可是孫子級別標簽) * 路徑與屬性的結合 語法: `標簽名[屬性名="屬性值"]` 比如說:想定位到 for="user\_pass" 的label標簽 `label[for="user_pass"]` * 如果想同時使用多個屬性去定位 語法: 標簽名\[屬性名="屬性值"\]\[屬性名="屬性值"\] 想定位到:class="input" id="user\_pass" 的 input標簽 `input[class="input"][id="user_pass"]` 比如:定位到 for="user\_pass" 的label標簽 的 input子標簽 `label[for="user_pass"] input` * 只用屬性 想定位到 for="user\_pass" 的標簽 `[for="user_pass"]` * 關于id與class定位 在css定位中,id或者class定位有快捷鍵定位方式 id用#表示,class用.表示 想定位到:id="user\_pass" 的 input標簽 `input#user_pass` 想定位到:class="button-primary" 的input標簽 `input.button-primary` 想定位到:class="button-primary" id="wp-submit" 的input標簽 `input.button-primary#wp-submit` 使用css定位實現blog登錄 ~~~ from selenium import webdriver from time import sleep d = webdriver.Chrome() d.get('http://os-201804081018/blog/wp-login.php') d.maximize_window() sleep(3) d.find_element_by_css_selector('#user_login').send_keys('test') sleep(3) d.find_element_by_css_selector('input[type="password"]').send_keys('test123') sleep(3) d.find_element_by_css_selector('p.submit input#wp-submit').click() sleep(3) d.close() d.quit() ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看