selenium之web控件定位

find_element[python]

在python里面,webdriver提供find_element()/find_elements方法定位元素,前者返回一個(gè)元素,后者返回多個(gè)元素。其參數(shù)為(by,value),by是定位方式,value是對(duì)應(yīng)的值。

find_element源碼如下:

    def find_element(self, by=By.ID, value=None):
        if self.w3c:
            if by == By.ID:
                by = By.CSS_SELECTOR
                value = '[id="%s"]' % value
            elif by == By.TAG_NAME:
                by = By.CSS_SELECTOR
            elif by == By.CLASS_NAME:
                by = By.CSS_SELECTOR
                value = ".%s" % value
            elif by == By.NAME:
                by = By.CSS_SELECTOR
                value = '[name="%s"]' % value
        return self.execute(Command.FIND_ELEMENT, {
            'using': by,
            'value': value})['value']

By源碼如下:

    class By(object):
        """
        Set of supported locator strategies.
        """

        ID = "id"
        XPATH = "xpath"
        LINK_TEXT = "link text"
        PARTIAL_LINK_TEXT = "partial link text"
        NAME = "name"
        TAG_NAME = "tag name"
        CLASS_NAME = "class name"
        CSS_SELECTOR = "css selector"

8種定位方式

通過python源碼,我們可以分析出selenium提供的各種定位方式。
總共有8種定位方式:

方式名稱 對(duì)應(yīng)屬性 說明
ID id 元素的id屬性,一般都是唯一的
NAME name 元素的name屬性,一般都是唯一的
XPATH xpath 用頁面中元素的路徑進(jìn)行查找,幾乎能定位到所有元素,但是查找較慢
TAG_NAME tag name 通過元素的標(biāo)簽名稱進(jìn)行查找,一般會(huì)找到多個(gè),結(jié)合find_elements使用
CLASS_NAME class name 使用元素的css樣式的class name查找
CSS_SELECTOR css selector 通過css樣式選擇器進(jìn)行定位,這種定位非??焖?/td>
LINK_TEXT link text 通過鏈接的文本進(jìn)行定位,文本全匹配查找
PARTIAL_LINK_TEXT partial link text 通過鏈接的文本進(jìn)行定位,模糊查找

實(shí)例

以百度首頁為例:以下是頁面源碼
搜索輸入框:
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
百度一下的按鈕:
<input type="submit" id="su" value="百度一下" class="bg s_btn">
新聞的鏈接:
<a target="_blank" class="mnav c-font-normal c-color-t">新聞</a>
實(shí)例腳本代碼:

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

class TestLocat():
    def setup_class(self):
        self.driver=Chrome()
        self.driver.implicitly_wait(3)

    def setup(self):
        self.driver.get("https://www.baidu.com")

    def teardown_class(self):
        self.driver.quit()

    def test_id(self):
        self.driver.find_element(By.ID,"kw").send_keys("百度")
        self.driver.find_element_by_id("su").click()
    def test_name(self):
        self.driver.find_element(By.NAME,"wd").send_keys("百度")
    def test_xpath(self):
        self.driver.find_element(By.XPATH,"http://*[@name='wd']").send_keys("百度")
        self.driver.find_element_by_xpath("http://*[@id='su']").click()
    def test_tag_name(self):
        # 輸入框是input標(biāo)簽,按鈕也是,所以這里也會(huì)報(bào)錯(cuò)
        self.driver.find_element(By.TAG_NAME,"input").send_keys("百度")
    def test_link(self):
        self.driver.find_element(By.LINK_TEXT,"新聞").click()
    def test_plink(self):
        self.driver.find_element_by_partial_link_text("聞").click()
    def test_classname(self):
        # 新聞鏈接,百度首頁左上角的鏈接文字class name 都是相同的,所以此處會(huì)報(bào)錯(cuò)
        self.driver.find_element(By.CLASS_NAME,"mnav c-font-normal c-color-t").click()
    def test_cssselector(self):
        self.driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("百度")
        self.driver.find_element_by_css_selector("#su").click()

總結(jié)

通過find_element的源碼發(fā)現(xiàn):ID/NAME/TAG_NAME/CLASS_NAME都會(huì)被替換成CSS_SELECTOR方式實(shí)現(xiàn),因此基本上所有的定位都可以用xpath或css selector方式,只要學(xué)好如何使用xpath或css selector定位即可。

[created_at:2020-06-27 updated_at:2020-06-28]
[我的導(dǎo)航目錄]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容