• selenium 元素定位


    一、八种定位方法

    前八种是大家都熟悉的,经常会用到的

    1.id定位:find_element_by_id(self, id_)
    2.name定位:find_element_by_name(self, name)
    3.class定位:find_element_by_class_name(self, name)
    4.tag定位:find_element_by_tag_name(self, name)
    5.link定位:find_element_by_link_text(self, link_text)
    6.partial_link定位find_element_by_partial_link_text(self, link_text)
    7.xpath定位:find_element_by_xpath(self, xpath)
    8.css定位:find_element_by_css_selector(self, css_selector)

    这八种是复数形式
    9.id复数定位 find_elements_by_id(self, id_)
    10.name复数定位 find_elements_by_name(self, name)
    11.class复数定位 find_elements_by_class_name(self, name)
    12.tag复数定位 find_elements_by_tag_name(self, name)
    13.link复数定位 find_elements_by_link_text(self, text)
    14.partial_link复数定位 find_elements_by_partial_link_text(self, link_text)
    15.xpath复数定位 find_elements_by_xpath(self, xpath)
    16.css复数定位 find_elements_by_css_selector(self, css_selector)


    xpath是宝刀屠龙,css是倚天剑。

    但是现在一般都不用上面的定位方式了,用以下这2个方法:

    官方的源码:

        def find_element(self, by=By.ID, value=None):
            """
            Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
            possible.
    
            :Usage:
                element = driver.find_element(By.ID, 'foo')
    
            :rtype: WebElement
            """
            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']
    
        def find_elements(self, by=By.ID, value=None):
            """
            Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when
            possible.
    
            :Usage:
                elements = driver.find_elements(By.CLASS_NAME, 'foo')
    
            :rtype: list of WebElement
            """
            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 empty list if driver returns null
            # See https://github.com/SeleniumHQ/selenium/issues/4555
            return self.execute(Command.FIND_ELEMENTS, {
                'using': by,
                'value': value})['value'] or []

     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"

    案例如下:

    
    
    from  time import  sleep
    from selenium import webdriver


    #实例化
    driver=webdriver.Chrome()
    # driver.get("https://news.163.com/")
    driver.get("http://www.baidu.com")
    driver.find_element('id','kw').send_keys("虚竹")
    driver.find_element('id','su').click()
    driver.find_element()
    sleep(3)
    driver.quit()
     

    以后就记住find_element()一个方法即可。

  • 相关阅读:
    Java8新特性 -- Lambda基础语法
    全角和半角的区别
    java super关键字
    数字格式化
    随机抽奖小程序
    获取指定范围的不重复的随机数
    Java Math类学习
    Java 随机数的产生
    JAVA Color类
    Static和static block(静态块)的用法
  • 原文地址:https://www.cnblogs.com/eosclover/p/15885143.html
Copyright © 2020-2023  润新知