1110Selenium web自动化测试经验分享-Xpath定位总结

刚分享过App的实战,这次分享下Web如何运用Xpath定位。本期的整体思路和App那期是一样的。

1. 根据 元素属性名和属性值 来定位

    def test_16(self):
        """xpath 根据元素属性名和属性值来定位"""
        # 通过元素的id、name、class这属性定位
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        # xpath 通过id属性来定位
        driver.find_element_by_xpath('//*[@id="kw"]').send_keys('python')

        # xpath 通过name属性来定位
        driver.find_element_by_xpath('//*[@name="wd"]').send_keys('python')

        # xpath 通过class属性来定位
        driver.find_element_by_xpath('//*[@class="s_ipt"]').send_keys('python')

        # xpath 也可以通过 其他属性 定位
        driver.find_element_by_xpath('//*[@autocomplete="off"]').send_keys('python')

        time.sleep(1.5)
        driver.quit()
    def test_16b(self):
        """xpath 根据元素属性名和属性值来定位"""
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")

        # <input type="text" id="input1" value="" class="input-text" onkeydown="check_enter(event)">
        self.xin_find_element(self.driver, By.XPATH, '//*[@id="input1"]').send_keys('python')
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//*[@class="input-text"]').clear()

        # <input type="password" id="input2" value="" class="input-text" onkeydown="check_enter(event)">
        self.xin_find_element(self.driver, By.XPATH, '//*[@type="password"]').send_keys('python')
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//*[@id="input2"]').clear()

        time.sleep(1)
        self.driver.quit()

2.根据 标签 + 元素属性名和值 来定位

    def test_18a(self):
        """xpath 标签+ 元素属性名和属性值 组合定位"""
        #     1.有时候同一个属性,同名的比较多,这时候可以通过标签筛选下,定位更准一点
        #     2.如果不想制定标签名称,可以用*号表示任意标签
        #     3.如果想制定具体某个标签,就可以直接写标签名称
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        driver.find_element_by_xpath('//input[@autocomplete="off"]').send_keys('python')
        driver.find_element_by_xpath('//input[@id="kw"]').send_keys('python')
        driver.find_element_by_xpath('//input[@name="wd"]').send_keys('python')

        time.sleep(1.5)
        driver.quit()
    def test_18b(self):
        """xpath 标签 + 元素属性名和属性值 组合定位"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(10)
        driver.get("https://passport.csdn.net/account/login")
        driver.find_element_by_link_text('账号登录').click()
        time.sleep(2)
        # <input id="username" name="username" tabindex="1" placeholder="输入用户名/邮箱/手机号" class="user-name" type="text" value="">
        driver.find_element_by_xpath('//input[@placeholder="输入用户名/邮箱/手机号"]').send_keys('python')
        time.sleep(0.5)
        driver.find_element_by_xpath('//input[@name="username"]').send_keys('6666')

        # <input id="password" name="password" tabindex="2" placeholder="输入密码" class="pass-word" type="password" value="" autocomplete="off">
        driver.find_element_by_xpath('//input[@placeholder="输入密码"]').send_keys('python')
        time.sleep(0.5)
        driver.find_element_by_xpath('//input[@class="pass-word"]').clear()

        time.sleep(2)
        driver.quit()

3.根据 层级关系+索引 来定位

    def test_19a(self):
        """xpath 层级-父定位子"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(10)
        driver.get("https://www.baidu.com")
        # 它爷爷来定位孙子
        driver.find_element_by_xpath('//form[@id="form"]/span/input').send_keys('python')
        
        driver.find_element_by_xpath('//input[@id="su"]').click()
        time.sleep(2)
        driver.quit()
    def test_19b(self):
        """xpath 层级关系、 索引"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(10)
        driver.get("https://passport.csdn.net/account/login")
        driver.find_element_by_link_text('账号登录').click()
        time.sleep(2)

        # 它爷爷来定位孙子、 索引
        driver.find_element_by_xpath('//div[@class="user-pass"]/form/input[2]').send_keys('python')
        time.sleep(0.5)

        # 父定位子、 索引
        driver.find_element_by_xpath('//form[@id="fm1"]/input[3]').send_keys('python')

        time.sleep(2)
        driver.quit()
    def test_19c(self):
        """xpath 层级关系、 索引"""
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")

        # 用户名
        self.xin_find_element(self.driver, By.XPATH, '//div[@class="block"]/input').send_keys('python')     # 父定位子
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//form[@method="post"]/div[2]/input').send_keys('python')     # 它爷爷来定位、 索引

        # 密码
        self.xin_find_element(self.driver, By.XPATH, '//form[@method="post"]/div[3]/input').send_keys('python')     # 它爷爷来定位、 索引
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//div[@class="block"][2]/input').send_keys('python')      # 父定位子、 索引

        time.sleep(1)
        self.driver.quit()

4.根据 兄弟节点 来定位

    def test_20(self):
        """xpath-兄弟节点"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")

        # 兄弟来定位
        driver.find_element_by_xpath('//a[@id="quickdelete"]/../input').send_keys('python')
        time.sleep(0.5)
        driver.find_element_by_xpath('//a[@title="清空" and @class="quickdelete"]/parent::*/input').send_keys('6666')
        time.sleep(0.5)
        driver.find_element_by_xpath('//a[@id="quickdelete"]/parent::span/input').clear()

        time.sleep(1.5)
        driver.quit()
    def test_20b(self):
        """xpath-兄弟节点"""
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")

        # 用户名
        self.xin_find_element(self.driver, By.XPATH, '//span[@id="tip_input1"]/../input').send_keys('python')  # 同一父类--兄弟节点
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//div[@id="Heading"]/parent::form/div[2]/input').clear()      # 同一爷爷类--兄弟节点

        # 密码
        self.xin_find_element(self.driver, By.XPATH, '//label[@class="label-line"]/parent::*/input[@id="input2"]').send_keys('python')  # 同一父类--兄弟节点
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//div[@id="checkWay"]/../div[3]/input').clear()   # 同一爷爷类--兄弟节点

        time.sleep(1)
        self.driver.quit()
    def test_20c(self):
        """xpath-兄弟节点"""
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.driver.get("https://passport.csdn.net/account/login")
        self.xin_find_element(self.driver, By.LINK_TEXT, "账号登录").click()
        time.sleep(2)

        # 用户名
        self.xin_find_element(self.driver, By.XPATH, '//input[@id="gps"]/../input[2]').send_keys('python')
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//div[@class="mobile-auth"]/parent::form/input[2]').clear()

        # 密码
        self.xin_find_element(self.driver, By.XPATH, '//input[@id="fkid"]/parent::*/input[3]').send_keys('python')
        time.sleep(0.5)
        self.xin_find_element(self.driver, By.XPATH, '//input[@id="iframe"]/../input[3]').clear()

        # 登录按钮
        self.xin_find_element(self.driver, By.XPATH, '//form[@id="fm1"]/input[9]').click()
        time.sleep(1)
        self.driver.quit()

5. 根据 逻辑运算 and or not 组合定位

    def test_21(self):
        """xpath 支持逻辑运算 and or not"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        driver.find_element_by_xpath('//*[@id="kw" and @name="wd"]').send_keys('python')
        driver.find_element_by_xpath('//*[@class="s_ipt" and @name="wd"]').send_keys('python')

        time.sleep(1)
        driver.quit()
    def test_21b(self):
        """xpath 支持逻辑运算 and or not"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        driver.find_element_by_xpath('//*[@id="kw" or @name="wd"]').send_keys('python')
        driver.find_element_by_xpath('//*[@class="s_ipt" or @name="wd"]').send_keys('python')

        time.sleep(1)
        driver.quit()

6.根据 模糊定位contains、starts-with

    def test_22a(self):
        """xpath 模糊匹配 contains"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")

        # <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
        driver.find_element_by_xpath('//*[contains(@name,"tj_trhao")]').click()
        driver.back()

        # <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
        driver.find_element_by_xpath('//*[contains(@href,"https://www.hao123")]').click()
        driver.back()

        # <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
        driver.find_element_by_xpath('//*[contains(@class,"mna")]').click()

        time.sleep(1)
        driver.quit()
    def test_22b(self):
        """xpath 模糊匹配 contains"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        driver.find_element_by_xpath('//input[contains(@id,"w")]').send_keys('python')
        driver.find_element_by_xpath('//input[contains(@name,"w")]').send_keys('python')
        driver.find_element_by_xpath('//input[contains(@class,"ipt")]').send_keys('python')
        driver.find_element_by_xpath('//input[contains(@autocomplete,"ff")]').send_keys('python')

        time.sleep(1)
        driver.quit()
    def test_23a(self):
        """xpath 模糊匹配 starts-with"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")

        # <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
        driver.find_element_by_xpath('//*[starts-with(@class, "mn")]').click()
        driver.back()

        # <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
        driver.find_element_by_xpath('//*[starts-with(@name, "tj_trhao")]').click()
        driver.back()

        # <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
        driver.find_element_by_xpath('//*[starts-with(@href, "https://www.hao123")]').click()

        time.sleep(1)
        driver.quit()
    def test_23b(self):
        """xpath 模糊匹配 starts-with"""
        driver = webdriver.Chrome()
        driver.implicitly_wait(5)
        driver.get("https://www.baidu.com")
        # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">

        driver.find_element_by_xpath('//input[starts-with(@autocomplete,"o")]').send_keys('python')
        driver.find_element_by_xpath('//input[starts-with(@id,"k")]').send_keys('python')
        driver.find_element_by_xpath('//input[starts-with(@name,"w")]').send_keys('python')
        driver.find_element_by_xpath('//input[starts-with(@class,"s_i")]').send_keys('python')

        time.sleep(1)
        driver.quit()

ends-with是xpath2.0的语法,可能我的浏览器还只支持1.0的语法,所以就放弃整理那部分。

交流技术 欢迎+QQ 153132336 zy
欢迎关注 微信公众号:紫云小站

猜你喜欢

转载自blog.csdn.net/zyooooxie/article/details/83856854